@soga/part-uploader 0.3.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.d.mts +175 -0
- package/dist/main.d.ts +170 -6
- package/dist/main.js +1 -1
- package/dist/main.mjs +1 -0
- package/package.json +20 -16
- package/README.md +0 -1
- package/dist/types/main.d.ts +0 -19
- package/dist/types/main.js +0 -1
- package/dist/uploader/ali-helper.d.ts +0 -35
- package/dist/uploader/ali-helper.js +0 -1
- package/dist/uploader/ali-uploader.d.ts +0 -29
- package/dist/uploader/ali-uploader.js +0 -1
- package/dist/uploader/baidu-helper.d.ts +0 -16
- package/dist/uploader/baidu-helper.js +0 -1
- package/dist/uploader/baidu-uploader.d.ts +0 -23
- package/dist/uploader/baidu-uploader.js +0 -1
- package/dist/uploader/base-uploader.d.ts +0 -43
- package/dist/uploader/base-uploader.js +0 -1
- package/dist/utils/chunk.d.ts +0 -14
- package/dist/utils/chunk.js +0 -1
package/dist/main.d.mts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { PartItem, UploadProgress, UploadWorkerMessage } from '@soga/node-types';
|
|
2
|
+
import { MessagePort } from 'node:worker_threads';
|
|
3
|
+
import { HostType } from '@soga/types';
|
|
4
|
+
import { Sdk } from '@soga/sdk';
|
|
5
|
+
import { LowType } from '@soga/lowdb';
|
|
6
|
+
import { AxiosInstance } from 'axios';
|
|
7
|
+
|
|
8
|
+
type CommonPartUploaderParams = {
|
|
9
|
+
file_id: number;
|
|
10
|
+
host_id: number;
|
|
11
|
+
part_id: number;
|
|
12
|
+
output_root: string;
|
|
13
|
+
part: PartItem;
|
|
14
|
+
sdk_domain: string;
|
|
15
|
+
port?: MessagePort;
|
|
16
|
+
};
|
|
17
|
+
type UploadBaiduParams = CommonPartUploaderParams & {
|
|
18
|
+
cloud_folder_path: string;
|
|
19
|
+
appid: number;
|
|
20
|
+
ua?: string;
|
|
21
|
+
};
|
|
22
|
+
type UploadAliParams = CommonPartUploaderParams & {
|
|
23
|
+
cloud_folder_id: string;
|
|
24
|
+
ua?: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type GroupItem = {
|
|
28
|
+
index: number;
|
|
29
|
+
file: string;
|
|
30
|
+
start: number;
|
|
31
|
+
end: number;
|
|
32
|
+
finish: boolean;
|
|
33
|
+
};
|
|
34
|
+
type CompleteGroupItem = GroupItem & {
|
|
35
|
+
size: number;
|
|
36
|
+
};
|
|
37
|
+
type CompleteGroupList = CompleteGroupItem[];
|
|
38
|
+
|
|
39
|
+
declare class BaseUploader {
|
|
40
|
+
protected port: MessagePort | null;
|
|
41
|
+
protected GROUP_LIST_KEY: string;
|
|
42
|
+
protected chunk_size: number;
|
|
43
|
+
protected group_list: CompleteGroupList;
|
|
44
|
+
protected host_type: HostType;
|
|
45
|
+
protected is_stop: boolean;
|
|
46
|
+
db: LowType<{
|
|
47
|
+
group_list?: CompleteGroupList;
|
|
48
|
+
baidu_upload_id?: string;
|
|
49
|
+
baidu_md5_list?: string[];
|
|
50
|
+
ali_upload_id?: string;
|
|
51
|
+
ali_file_id?: string;
|
|
52
|
+
ali_part_info_list?: {
|
|
53
|
+
part_number: number;
|
|
54
|
+
upload_url: string;
|
|
55
|
+
expired_at: number;
|
|
56
|
+
}[];
|
|
57
|
+
}>;
|
|
58
|
+
protected sdk: Sdk;
|
|
59
|
+
protected baseParams: CommonPartUploaderParams;
|
|
60
|
+
constructor(params: CommonPartUploaderParams, host_type: HostType);
|
|
61
|
+
protected init(): Promise<void>;
|
|
62
|
+
protected initGroupList(): Promise<void>;
|
|
63
|
+
private initDb;
|
|
64
|
+
postProgress(progress: {
|
|
65
|
+
type: UploadProgress;
|
|
66
|
+
percent: number;
|
|
67
|
+
host_type: HostType;
|
|
68
|
+
}): Promise<void>;
|
|
69
|
+
protected postMessage(data: UploadWorkerMessage): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
declare class BaiduHelper extends BaseUploader {
|
|
73
|
+
protected axios: AxiosInstance;
|
|
74
|
+
protected chunk_size: number;
|
|
75
|
+
protected group_list: CompleteGroupList;
|
|
76
|
+
protected md5_list: string[];
|
|
77
|
+
protected params: UploadBaiduParams;
|
|
78
|
+
constructor(params: UploadBaiduParams);
|
|
79
|
+
protected init(): Promise<void>;
|
|
80
|
+
private initAxios;
|
|
81
|
+
protected initMd5List(): Promise<void>;
|
|
82
|
+
protected getSearchParams(payload: Record<string, string>): string;
|
|
83
|
+
protected getUploadId(): Promise<any>;
|
|
84
|
+
protected cacheUploadId(upload_id: string): Promise<void>;
|
|
85
|
+
protected clearCache(): Promise<void>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
declare class BaiduUploader extends BaiduHelper {
|
|
89
|
+
private queue_list;
|
|
90
|
+
private get ua();
|
|
91
|
+
protected get cloudPath(): string;
|
|
92
|
+
private upload_server;
|
|
93
|
+
initUploadUrl(): Promise<void>;
|
|
94
|
+
start(): Promise<{
|
|
95
|
+
fs_id: number;
|
|
96
|
+
}>;
|
|
97
|
+
protected postcreate(): Promise<{
|
|
98
|
+
fs_id: number;
|
|
99
|
+
}>;
|
|
100
|
+
protected uploadChunks(): Promise<void>;
|
|
101
|
+
protected precreate(): Promise<{
|
|
102
|
+
need_upload: boolean;
|
|
103
|
+
data?: undefined;
|
|
104
|
+
} | {
|
|
105
|
+
need_upload: boolean;
|
|
106
|
+
data: {
|
|
107
|
+
fs_id: number;
|
|
108
|
+
};
|
|
109
|
+
}>;
|
|
110
|
+
private uploadOneChunk;
|
|
111
|
+
private getChunkInfo;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
declare class AliHelper extends BaseUploader {
|
|
115
|
+
params: UploadAliParams;
|
|
116
|
+
protected drive_id: string;
|
|
117
|
+
protected axios: AxiosInstance;
|
|
118
|
+
protected domain: string;
|
|
119
|
+
protected chunk_size: number;
|
|
120
|
+
protected group_list: CompleteGroupList;
|
|
121
|
+
constructor(params: UploadAliParams);
|
|
122
|
+
protected getExpiredAt(): number;
|
|
123
|
+
private initAxios;
|
|
124
|
+
protected init(): Promise<void>;
|
|
125
|
+
protected getUploadId(): Promise<any>;
|
|
126
|
+
protected cacheUploadId(upload_id: string): Promise<void>;
|
|
127
|
+
protected clearCache(): Promise<void>;
|
|
128
|
+
protected getFileId(): Promise<any>;
|
|
129
|
+
protected cacheFileId(file_id: string): Promise<void>;
|
|
130
|
+
protected getPartInfoList(): Promise<any>;
|
|
131
|
+
protected cachePartInfoList(part_info_list: {
|
|
132
|
+
part_number: number;
|
|
133
|
+
upload_url: string;
|
|
134
|
+
}[]): Promise<any>;
|
|
135
|
+
protected getProofCode(access_token: string): Promise<string>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
declare class AliUploader extends AliHelper {
|
|
139
|
+
private queue_list;
|
|
140
|
+
private onUploadSuccess;
|
|
141
|
+
start(): Promise<{
|
|
142
|
+
file_id: string;
|
|
143
|
+
drive_id: string;
|
|
144
|
+
}>;
|
|
145
|
+
private coreStart;
|
|
146
|
+
protected postcreate(): Promise<{
|
|
147
|
+
file_id: string;
|
|
148
|
+
}>;
|
|
149
|
+
protected uploadChunks(): Promise<void>;
|
|
150
|
+
protected uploadOneChunk({ upload_url, part_number, }: {
|
|
151
|
+
upload_url: string;
|
|
152
|
+
part_number: number;
|
|
153
|
+
}): Promise<void>;
|
|
154
|
+
protected precreate(): Promise<{
|
|
155
|
+
need_upload: boolean;
|
|
156
|
+
data: {
|
|
157
|
+
file_id: string;
|
|
158
|
+
};
|
|
159
|
+
} | {
|
|
160
|
+
need_upload: boolean;
|
|
161
|
+
data?: undefined;
|
|
162
|
+
}>;
|
|
163
|
+
protected correctUploaded(upload_id: string): Promise<boolean>;
|
|
164
|
+
protected autoRefreshUploadUrl(): Promise<void>;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
declare const uploadAli: (params: UploadAliParams) => Promise<{
|
|
168
|
+
file_id: string;
|
|
169
|
+
drive_id: string;
|
|
170
|
+
}>;
|
|
171
|
+
declare const uploadBaidu: (params: UploadBaiduParams) => Promise<{
|
|
172
|
+
fs_id: number;
|
|
173
|
+
}>;
|
|
174
|
+
|
|
175
|
+
export { AliUploader, BaiduUploader, type CommonPartUploaderParams, type UploadAliParams, type UploadBaiduParams, uploadAli, uploadBaidu };
|
package/dist/main.d.ts
CHANGED
|
@@ -1,11 +1,175 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { PartItem, UploadProgress, UploadWorkerMessage } from '@soga/node-types';
|
|
2
|
+
import { MessagePort } from 'node:worker_threads';
|
|
3
|
+
import { HostType } from '@soga/types';
|
|
4
|
+
import { Sdk } from '@soga/sdk';
|
|
5
|
+
import { LowType } from '@soga/lowdb';
|
|
6
|
+
import { AxiosInstance } from 'axios';
|
|
7
|
+
|
|
8
|
+
type CommonPartUploaderParams = {
|
|
9
|
+
file_id: number;
|
|
10
|
+
host_id: number;
|
|
11
|
+
part_id: number;
|
|
12
|
+
output_root: string;
|
|
13
|
+
part: PartItem;
|
|
14
|
+
sdk_domain: string;
|
|
15
|
+
port?: MessagePort;
|
|
16
|
+
};
|
|
17
|
+
type UploadBaiduParams = CommonPartUploaderParams & {
|
|
18
|
+
cloud_folder_path: string;
|
|
19
|
+
appid: number;
|
|
20
|
+
ua?: string;
|
|
21
|
+
};
|
|
22
|
+
type UploadAliParams = CommonPartUploaderParams & {
|
|
23
|
+
cloud_folder_id: string;
|
|
24
|
+
ua?: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type GroupItem = {
|
|
28
|
+
index: number;
|
|
29
|
+
file: string;
|
|
30
|
+
start: number;
|
|
31
|
+
end: number;
|
|
32
|
+
finish: boolean;
|
|
33
|
+
};
|
|
34
|
+
type CompleteGroupItem = GroupItem & {
|
|
35
|
+
size: number;
|
|
36
|
+
};
|
|
37
|
+
type CompleteGroupList = CompleteGroupItem[];
|
|
38
|
+
|
|
39
|
+
declare class BaseUploader {
|
|
40
|
+
protected port: MessagePort | null;
|
|
41
|
+
protected GROUP_LIST_KEY: string;
|
|
42
|
+
protected chunk_size: number;
|
|
43
|
+
protected group_list: CompleteGroupList;
|
|
44
|
+
protected host_type: HostType;
|
|
45
|
+
protected is_stop: boolean;
|
|
46
|
+
db: LowType<{
|
|
47
|
+
group_list?: CompleteGroupList;
|
|
48
|
+
baidu_upload_id?: string;
|
|
49
|
+
baidu_md5_list?: string[];
|
|
50
|
+
ali_upload_id?: string;
|
|
51
|
+
ali_file_id?: string;
|
|
52
|
+
ali_part_info_list?: {
|
|
53
|
+
part_number: number;
|
|
54
|
+
upload_url: string;
|
|
55
|
+
expired_at: number;
|
|
56
|
+
}[];
|
|
57
|
+
}>;
|
|
58
|
+
protected sdk: Sdk;
|
|
59
|
+
protected baseParams: CommonPartUploaderParams;
|
|
60
|
+
constructor(params: CommonPartUploaderParams, host_type: HostType);
|
|
61
|
+
protected init(): Promise<void>;
|
|
62
|
+
protected initGroupList(): Promise<void>;
|
|
63
|
+
private initDb;
|
|
64
|
+
postProgress(progress: {
|
|
65
|
+
type: UploadProgress;
|
|
66
|
+
percent: number;
|
|
67
|
+
host_type: HostType;
|
|
68
|
+
}): Promise<void>;
|
|
69
|
+
protected postMessage(data: UploadWorkerMessage): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
declare class BaiduHelper extends BaseUploader {
|
|
73
|
+
protected axios: AxiosInstance;
|
|
74
|
+
protected chunk_size: number;
|
|
75
|
+
protected group_list: CompleteGroupList;
|
|
76
|
+
protected md5_list: string[];
|
|
77
|
+
protected params: UploadBaiduParams;
|
|
78
|
+
constructor(params: UploadBaiduParams);
|
|
79
|
+
protected init(): Promise<void>;
|
|
80
|
+
private initAxios;
|
|
81
|
+
protected initMd5List(): Promise<void>;
|
|
82
|
+
protected getSearchParams(payload: Record<string, string>): string;
|
|
83
|
+
protected getUploadId(): Promise<any>;
|
|
84
|
+
protected cacheUploadId(upload_id: string): Promise<void>;
|
|
85
|
+
protected clearCache(): Promise<void>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
declare class BaiduUploader extends BaiduHelper {
|
|
89
|
+
private queue_list;
|
|
90
|
+
private get ua();
|
|
91
|
+
protected get cloudPath(): string;
|
|
92
|
+
private upload_server;
|
|
93
|
+
initUploadUrl(): Promise<void>;
|
|
94
|
+
start(): Promise<{
|
|
95
|
+
fs_id: number;
|
|
96
|
+
}>;
|
|
97
|
+
protected postcreate(): Promise<{
|
|
98
|
+
fs_id: number;
|
|
99
|
+
}>;
|
|
100
|
+
protected uploadChunks(): Promise<void>;
|
|
101
|
+
protected precreate(): Promise<{
|
|
102
|
+
need_upload: boolean;
|
|
103
|
+
data?: undefined;
|
|
104
|
+
} | {
|
|
105
|
+
need_upload: boolean;
|
|
106
|
+
data: {
|
|
107
|
+
fs_id: number;
|
|
108
|
+
};
|
|
109
|
+
}>;
|
|
110
|
+
private uploadOneChunk;
|
|
111
|
+
private getChunkInfo;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
declare class AliHelper extends BaseUploader {
|
|
115
|
+
params: UploadAliParams;
|
|
116
|
+
protected drive_id: string;
|
|
117
|
+
protected axios: AxiosInstance;
|
|
118
|
+
protected domain: string;
|
|
119
|
+
protected chunk_size: number;
|
|
120
|
+
protected group_list: CompleteGroupList;
|
|
121
|
+
constructor(params: UploadAliParams);
|
|
122
|
+
protected getExpiredAt(): number;
|
|
123
|
+
private initAxios;
|
|
124
|
+
protected init(): Promise<void>;
|
|
125
|
+
protected getUploadId(): Promise<any>;
|
|
126
|
+
protected cacheUploadId(upload_id: string): Promise<void>;
|
|
127
|
+
protected clearCache(): Promise<void>;
|
|
128
|
+
protected getFileId(): Promise<any>;
|
|
129
|
+
protected cacheFileId(file_id: string): Promise<void>;
|
|
130
|
+
protected getPartInfoList(): Promise<any>;
|
|
131
|
+
protected cachePartInfoList(part_info_list: {
|
|
132
|
+
part_number: number;
|
|
133
|
+
upload_url: string;
|
|
134
|
+
}[]): Promise<any>;
|
|
135
|
+
protected getProofCode(access_token: string): Promise<string>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
declare class AliUploader extends AliHelper {
|
|
139
|
+
private queue_list;
|
|
140
|
+
private onUploadSuccess;
|
|
141
|
+
start(): Promise<{
|
|
142
|
+
file_id: string;
|
|
143
|
+
drive_id: string;
|
|
144
|
+
}>;
|
|
145
|
+
private coreStart;
|
|
146
|
+
protected postcreate(): Promise<{
|
|
147
|
+
file_id: string;
|
|
148
|
+
}>;
|
|
149
|
+
protected uploadChunks(): Promise<void>;
|
|
150
|
+
protected uploadOneChunk({ upload_url, part_number, }: {
|
|
151
|
+
upload_url: string;
|
|
152
|
+
part_number: number;
|
|
153
|
+
}): Promise<void>;
|
|
154
|
+
protected precreate(): Promise<{
|
|
155
|
+
need_upload: boolean;
|
|
156
|
+
data: {
|
|
157
|
+
file_id: string;
|
|
158
|
+
};
|
|
159
|
+
} | {
|
|
160
|
+
need_upload: boolean;
|
|
161
|
+
data?: undefined;
|
|
162
|
+
}>;
|
|
163
|
+
protected correctUploaded(upload_id: string): Promise<boolean>;
|
|
164
|
+
protected autoRefreshUploadUrl(): Promise<void>;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
declare const uploadAli: (params: UploadAliParams) => Promise<{
|
|
5
168
|
file_id: string;
|
|
6
169
|
drive_id: string;
|
|
7
170
|
}>;
|
|
8
|
-
|
|
171
|
+
declare const uploadBaidu: (params: UploadBaiduParams) => Promise<{
|
|
9
172
|
fs_id: number;
|
|
10
173
|
}>;
|
|
11
|
-
|
|
174
|
+
|
|
175
|
+
export { AliUploader, BaiduUploader, type CommonPartUploaderParams, type UploadAliParams, type UploadBaiduParams, uploadAli, uploadBaidu };
|
package/dist/main.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,r,a,o){void 0===o&&(o=a);var t=Object.getOwnPropertyDescriptor(r,a);t&&!("get"in t?!r.__esModule:t.writable||t.configurable)||(t={enumerable:!0,get:function(){return r[a]}}),Object.defineProperty(e,o,t)}:function(e,r,a,o){void 0===o&&(o=a),e[o]=r[a]}),__exportStar=this&&this.__exportStar||function(e,r){for(var a in e)"default"===a||Object.prototype.hasOwnProperty.call(r,a)||__createBinding(r,e,a)};Object.defineProperty(exports,"__esModule",{value:!0}),exports.uploadBaidu=exports.uploadAli=exports.AliUploader=exports.BaiduUploader=void 0;const ali_uploader_1=require("./uploader/ali-uploader"),baidu_uploader_1=require("./uploader/baidu-uploader");var baidu_uploader_2=require("./uploader/baidu-uploader");Object.defineProperty(exports,"BaiduUploader",{enumerable:!0,get:function(){return baidu_uploader_2.BaiduUploader}});var ali_uploader_2=require("./uploader/ali-uploader");Object.defineProperty(exports,"AliUploader",{enumerable:!0,get:function(){return ali_uploader_2.AliUploader}});const uploadAli=async e=>{const r=new ali_uploader_1.AliUploader(e);return await r.start()};exports.uploadAli=uploadAli;const uploadBaidu=async e=>{const r=new baidu_uploader_1.BaiduUploader(e);return await r.start()};exports.uploadBaidu=uploadBaidu,__exportStar(require("./types/main"),exports);
|
|
1
|
+
var t,a=Object.create,e=Object.defineProperty,i=Object.getOwnPropertyDescriptor,s=Object.getOwnPropertyNames,r=Object.getPrototypeOf,o=Object.prototype.hasOwnProperty,d=(t,a,r,d)=>{if(a&&"object"==typeof a||"function"==typeof a)for(let n of s(a))o.call(t,n)||n===r||e(t,n,{get:()=>a[n],enumerable:!(d=i(a,n))||d.enumerable});return t},n=(t,i,s)=>(s=null!=t?a(r(t)):{},d(!i&&t&&t.__esModule?s:e(s,"default",{value:t,enumerable:!0}),t)),h={};((t,a)=>{for(var i in a)e(t,i,{get:a[i],enumerable:!0})})(h,{AliUploader:()=>q,BaiduUploader:()=>E,uploadAli:()=>H,uploadBaidu:()=>F}),module.exports=(t=h,d(e({},"__esModule",{value:!0}),t));var p=require("fs"),l=n(require("axios")),u=require("path"),c=require("@soga/sdk");var _=require("@soga/lowdb"),f=class{port=null;GROUP_LIST_KEY="group_list";chunk_size=4194304;group_list=[];host_type;is_stop=!1;db;sdk;baseParams;constructor(t,a){this.host_type=a,this.baseParams=t,this.port=t.port??null}async init(){await this.initDb(),this.sdk=(0,c.getSdk)(this.baseParams.sdk_domain),await this.initGroupList()}async initGroupList(){if(this.db.data.group_list)return void(this.group_list=this.db.data.group_list);const t=function(t,a=4194304){const e=[],i=t.size;if(i<=a)e.push({file:t.file,start:t.start,end:t.end,finish:!0});else{const s=i/a,r=Math.floor(s),o=s===r;for(let i=0;i<r;i++)e.push({file:t.file,start:t.start+i*a,end:t.start+(i+1)*a-1,finish:!!o&&i===r});if(!o){const i=t.start+r*a;e.push({file:t.file,start:i,end:t.end,finish:!0})}}return e.map((t,a)=>({...t,size:t.end-t.start+1,index:a}))}(this.baseParams.part,this.chunk_size);this.group_list=t,this.db.data.group_list=t,await this.db.write()}async initDb(){const t=`${this.host_type}_${this.baseParams.part.md5}.json`,a=(0,u.resolve)(this.baseParams.output_root,t),e=await(0,_.getDb)(a,{_timestamp:Date.now()});return await e.write(),this.db=e,e}async postProgress(t){await this.postMessage({id:this.baseParams.file_id,type:"percent",data:{part_id:this.baseParams.part_id,host_type:this.host_type,percent:t.percent}})}async postMessage(t){this.port&&this.port.postMessage(t)}},m=require("crypto"),w=require("@soga/utils"),g=require("@soga/types"),b=require("@soga/error"),y=class extends f{params;drive_id;axios;domain="https://openapi.alipan.com";chunk_size=4194304;group_list=[];constructor(t){super(t,g.HostType.ALI),this.params=t,this.initAxios()}getExpiredAt(){return Date.now()+348e4}initAxios(){this.axios=l.default.create({baseURL:this.domain}),this.axios.interceptors.request.use(async t=>{const{access_token:a}=await this.sdk.getAliHostAuthData({host_id:this.params.host_id,refresh:!1});return t.headers.Authorization=`Bearer ${a}`,t},t=>{throw(0,b.buildDError)(t,{message:t.message,detail:`request ali netdisk error code: ${t.status||"unknown"}`,stack:t.stack,rewritable:!1})})}async init(){await super.init();const{drive_id:t}=await this.sdk.getAliHostAuthData({host_id:this.params.host_id,refresh:!1});this.drive_id=t}async getUploadId(){return this.db.data.ali_upload_id}async cacheUploadId(t){this.db.data.ali_upload_id=t,await this.db.write()}async clearCache(){this.db.data={},await this.db.write()}async getFileId(){return this.db.data.ali_file_id}async cacheFileId(t){this.db.data.ali_file_id=t,await this.db.write()}async getPartInfoList(){return this.db.data.ali_part_info_list}async cachePartInfoList(t){const a=this.getExpiredAt();return this.db.data.ali_part_info_list=t.map(t=>({...t,expired_at:a})),await this.db.write(),this.db.data.ali_part_info_list}async getProofCode(t){const{part:a}=this.params,e=a.size,i=(0,m.createHash)("md5").update(t).digest("hex").slice(0,16),s=BigInt(`0x${i}`),r=Number(s%BigInt(e)),o=r,d=Math.min(e,r+8),n=a.start+o,h=a.start+d;return(await(0,w.getFileBufferSlice)(a.path,n,h-1)).toString("base64")}},k=n(require("axios")),U=require("@soga/types"),v=require("@soga/node-types"),P=require("@soga/error"),q=class extends y{queue_list;async onUploadSuccess(t){return await this.clearCache(),await this.postProgress({type:v.UploadProgress.upload_ali,percent:1,host_type:U.HostType.ALI}),{file_id:t,drive_id:this.drive_id}}async start(){try{return await this.coreStart(0)}catch(t){throw(0,P.buildDError)(t,{message:`upload to ali failed: ${t.message}`,detail:`upload to ali failed, upload to folder: ${this.params.cloud_folder_id}`})}}async coreStart(t=0){if(t++>3)throw(0,P.buildDError)(new Error,{message:`upload to ali failed file_id: ${this.params.cloud_folder_id}`,detail:`upload to ali failed file_id: ${this.params.cloud_folder_id}`});await this.init();const a=await this.getUploadId();if(a){if(!await this.correctUploaded(a))return await this.clearCache(),await this.coreStart(t+1)}else{const{need_upload:t,data:a}=await this.precreate();if(!t)return await this.onUploadSuccess(a.file_id)}await this.uploadChunks();const e=await this.postcreate();return await this.onUploadSuccess(e.file_id)}async postcreate(){const t=await this.getFileId(),a=await this.getUploadId(),e=await this.axios.post("/adrive/v1.0/openFile/complete",{drive_id:this.drive_id,file_id:t,upload_id:a});if(e)return{file_id:e.data.file_id}}async uploadChunks(){this.queue_list;const t=async()=>{if(this.queue_list.length){await this.autoRefreshUploadUrl();const a=this.queue_list.shift();await this.uploadOneChunk(a);const e=Math.min(a.part_number*this.chunk_size,this.params.part.size)/this.params.part.size;await this.postProgress({type:v.UploadProgress.upload_ali,percent:.97*e,host_type:U.HostType.ALI}),await t()}};await t()}async uploadOneChunk({upload_url:t,part_number:a}){const e=a-1,i=this.group_list[e],s=this.params.part.path,r=(0,p.createReadStream)(s,{start:i.start,end:i.end});await k.default.put(t,r,{headers:{"Content-Type":null,"User-Agent":this.params.ua},timeout:12e4})}async precreate(){const{access_token:t}=await this.sdk.getAliHostAuthData({host_id:this.params.host_id,refresh:!1}),a=await this.getProofCode(t),e=this.group_list.map((t,a)=>({part_number:a+1})),{part:i}=this.params,s=`dpan_p_${i.md5}.txt`,r={drive_id:this.drive_id,parent_file_id:this.params.cloud_folder_id,name:s,type:"file",check_name_mode:"refuse",part_info_list:e,size:i.size,content_hash_name:"sha1",content_hash:i.sha1,proof_code:a,proof_version:"v1"},o=await k.default.post("/adrive/v1.0/openFile/create",r,{baseURL:this.domain,headers:{Authorization:`Bearer ${t}`}}),{data:d}=o;return d.exist&&"available"===d.status||d.rapid_upload?{need_upload:!1,data:{file_id:d.file_id}}:(await this.cacheFileId(d.file_id),await this.cacheUploadId(d.upload_id),await this.cachePartInfoList(d.part_info_list),this.queue_list=await this.cachePartInfoList(d.part_info_list),{need_upload:!0})}async correctUploaded(t){const a=await this.getFileId();try{const e=(await this.axios.post("/adrive/v1.0/openFile/listUploadedParts",{drive_id:this.drive_id,upload_id:t,file_id:a})).data.uploaded_parts.map(t=>t.part_number),i=await this.getPartInfoList();if(i.length){const t=i.filter(t=>!e.includes(t.part_number));return this.queue_list=t,!0}return!1}catch(t){return!1}}async autoRefreshUploadUrl(){const{queue_list:t}=this;if(t[0].expired_at>Date.now())return;const a=t.map(t=>({part_number:t.part_number})),e={drive_id:this.drive_id,upload_id:await this.getUploadId(),file_id:await this.getFileId(),part_info_list:a},i=await this.axios.post("/adrive/v1.0/openFile/getUploadUrl",e);i&&(this.queue_list=await this.cachePartInfoList(i.data.part_info_list),await this.cachePartInfoList(i.data.part_info_list))}},x=n(require("form-data")),I=require("@soga/utils"),$=require("@soga/utils"),A=require("@soga/types"),D=n(require("axios")),z=require("@soga/error"),S=class extends f{axios;chunk_size=4194304;group_list=[];md5_list=[];params;constructor(t){super(t,A.HostType.BAIDU),this.params=t,this.initAxios()}async init(){await super.init(),await this.initMd5List()}initAxios(){this.axios=D.default.create({}),this.axios.interceptors.response.use(t=>t.data,t=>{throw(0,z.buildDError)(t,{message:t.message,detail:`request baidu netdisk error code: ${t.status||"unknown"}`,stack:t.stack,rewritable:!1})})}async initMd5List(){const t=this.db.data.baidu_md5_list;if(t)return void(this.md5_list=t);const a=this.group_list,e=[];for(const t of a){const{path:a}=this.params.part,i=await(0,$.calculateMd5)({file:a,start:t.start,end:t.end});e.push(i)}this.md5_list=e,this.db.data.baidu_md5_list=e,await this.db.write()}getSearchParams(t){Object.keys(t).forEach(a=>{void 0===t[a]&&delete t[a]});return new URLSearchParams(t).toString()}async getUploadId(){return this.db.data.baidu_upload_id}async cacheUploadId(t){this.db.data.baidu_upload_id=t,await this.db.write()}async clearCache(){this.db.data={},await this.db.write()}},O=require("path"),B=require("@soga/types"),C=require("@soga/error"),L=require("@soga/node-types"),E=class extends S{queue_list=[];get ua(){return this.params.ua||"pan.baidu.com"}get cloudPath(){return`${this.params.cloud_folder_path}/dpan_p_${this.params.part.md5}.bin`}upload_server="";async initUploadUrl(){const{access_token:t}=await this.sdk.getBaiduHostAuthData({host_id:this.params.host_id,refresh:!1}),a=await this.getUploadId(),e=`https://d.pcs.baidu.com/rest/2.0/pcs/file?${this.getSearchParams({method:"locateupload",appid:`${this.params.appid}`,access_token:t,path:this.cloudPath,uploadid:a,upload_version:"2.0"})}`,i=await this.axios.get(e);i.quic_servers.length?this.upload_server=i.quic_servers[0].server:i.servers.length?this.upload_server=i.servers[0].server:i.bak_servers.length?this.upload_server=i.bak_servers[0].server:this.upload_server="https://d.pcs.baidu.com"}async start(){try{await this.init();const t=await this.precreate();let a=t.data;return t.need_upload&&(await this.initUploadUrl(),await this.uploadChunks(),a=await this.postcreate()),await this.postProgress({type:L.UploadProgress.upload_baidu,percent:1,host_type:B.HostType.BAIDU}),await this.clearCache(),a}catch(t){throw(0,C.buildDError)(t,{message:`upload to baidu failed: ${t.message}`,detail:`upload to baidu failed, upload to folder: ${this.params.cloud_folder_path}`})}}async postcreate(){const t=await this.getUploadId(),a=this.cloudPath,e=this.md5_list,i=this.params.part.size,{access_token:s}=await this.sdk.getBaiduHostAuthData({host_id:this.params.host_id,refresh:!1}),r=`https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=${s}`,o=this.getSearchParams({path:a,size:`${i}`,isdir:"0",block_list:JSON.stringify(e),uploadid:t||void 0,rtype:"2"}),d=await this.axios.post(r,o,{headers:{"User-Agent":this.ua}});if(d.errno)throw(0,C.buildDError)(new Error,{message:`Unable to create file when uploading to Baidu Netdisk: ${d.errno}`,detail:`Unable to create file when uploading to Baidu Netdisk: ${JSON.stringify(d)}`});return{fs_id:d.fs_id}}async uploadChunks(){const t=this.queue_list.map(t=>t),a=async()=>{if(t.length){const e=t.shift(),i=await this.getChunkInfo(e);await this.uploadOneChunk(i);const s=Math.min((e+1)*this.chunk_size,this.params.part.size)/this.params.part.size;await this.postProgress({type:L.UploadProgress.upload_baidu,percent:.98*s,host_type:B.HostType.BAIDU}),await a()}};await a()}async precreate(){const t=this.md5_list,{md4:a,md5:e,size:i}=this.params.part,{access_token:s}=await this.sdk.getBaiduHostAuthData({host_id:this.params.host_id,refresh:!1}),r=await this.getUploadId(),o=`https://pan.baidu.com/rest/2.0/xpan/file?method=precreate&access_token=${s}`,d=this.getSearchParams({path:this.cloudPath,size:`${i}`,isdir:"0",block_list:JSON.stringify(t),autoinit:"1",rtype:"2","content-md5":e,"slice-md5":a,uploadid:r}),n=await this.axios.post(o,d,{headers:{"User-Agent":this.ua}});if(n.errno)throw(0,C.buildDError)(new Error,{message:`Unable to precreate file when uploading to Baidu Netdisk: ${n.errno}`,detail:`Unable to precreate file when uploading to Baidu Netdisk: ${JSON.stringify(n)}`});return 1==n.return_type?(n.uploadid&&n.uploadid!=r&&await this.cacheUploadId(n.uploadid),this.queue_list=n.block_list,{need_upload:!0}):{need_upload:!1,data:{fs_id:n.info.fs_id}}}async uploadOneChunk(t,a=0){try{const{host_id:e}=this.baseParams,i=await this.getUploadId(),s=this.cloudPath,{access_token:r}=await this.sdk.getBaiduHostAuthData({host_id:e,refresh:!1}),o=`${this.upload_server}/rest/2.0/pcs/superfile2?access_token=${r}&method=upload&type=tmpfile&path=${s}&uploadid=${i}&partseq=${t.index}`,d=new x.default;d.append("file",t.buffer,`${t.filename}`);const n=await this.axios.post(o,d,{timeout:2e4*(1.5*a+1),headers:{"User-Agent":this.ua}});if(!n)return;return n.md5}catch(e){if(a<4)return e.message?.includes("timeout")&&await this.initUploadUrl(),await this.uploadOneChunk(t,a+1);throw e}}async getChunkInfo(t){const a=this.group_list[t],e=this.params.part.path,i=await(0,I.getFileBufferSlice)(e,a.start,a.end),{md5_list:s}=this;return{index:t,buffer:i,md5:s[t],filename:(0,O.parse)(e).base,size:i.byteLength}}},H=async t=>{const a=new q(t);return await a.start()},F=async t=>{const a=new E(t);return await a.start()};
|
package/dist/main.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createReadStream as t}from"fs";import a from"axios";import{resolve as i}from"path";import{getSdk as s}from"@soga/sdk";import{getDb as e}from"@soga/lowdb";var r=class{port=null;GROUP_LIST_KEY="group_list";chunk_size=4194304;group_list=[];host_type;is_stop=!1;db;sdk;baseParams;constructor(t,a){this.host_type=a,this.baseParams=t,this.port=t.port??null}async init(){await this.initDb(),this.sdk=s(this.baseParams.sdk_domain),await this.initGroupList()}async initGroupList(){if(this.db.data.group_list)return void(this.group_list=this.db.data.group_list);const t=function(t,a=4194304){const i=[],s=t.size;if(s<=a)i.push({file:t.file,start:t.start,end:t.end,finish:!0});else{const e=s/a,r=Math.floor(e),o=e===r;for(let s=0;s<r;s++)i.push({file:t.file,start:t.start+s*a,end:t.start+(s+1)*a-1,finish:!!o&&s===r});if(!o){const s=t.start+r*a;i.push({file:t.file,start:s,end:t.end,finish:!0})}}return i.map((t,a)=>({...t,size:t.end-t.start+1,index:a}))}(this.baseParams.part,this.chunk_size);this.group_list=t,this.db.data.group_list=t,await this.db.write()}async initDb(){const t=`${this.host_type}_${this.baseParams.part.md5}.json`,a=i(this.baseParams.output_root,t),s=await e(a,{_timestamp:Date.now()});return await s.write(),this.db=s,s}async postProgress(t){await this.postMessage({id:this.baseParams.file_id,type:"percent",data:{part_id:this.baseParams.part_id,host_type:this.host_type,percent:t.percent}})}async postMessage(t){this.port&&this.port.postMessage(t)}};import{createHash as o}from"crypto";import{getFileBufferSlice as d}from"@soga/utils";import{HostType as h}from"@soga/types";import{buildDError as n}from"@soga/error";var p=class extends r{params;drive_id;axios;domain="https://openapi.alipan.com";chunk_size=4194304;group_list=[];constructor(t){super(t,h.ALI),this.params=t,this.initAxios()}getExpiredAt(){return Date.now()+348e4}initAxios(){this.axios=a.create({baseURL:this.domain}),this.axios.interceptors.request.use(async t=>{const{access_token:a}=await this.sdk.getAliHostAuthData({host_id:this.params.host_id,refresh:!1});return t.headers.Authorization=`Bearer ${a}`,t},t=>{throw n(t,{message:t.message,detail:`request ali netdisk error code: ${t.status||"unknown"}`,stack:t.stack,rewritable:!1})})}async init(){await super.init();const{drive_id:t}=await this.sdk.getAliHostAuthData({host_id:this.params.host_id,refresh:!1});this.drive_id=t}async getUploadId(){return this.db.data.ali_upload_id}async cacheUploadId(t){this.db.data.ali_upload_id=t,await this.db.write()}async clearCache(){this.db.data={},await this.db.write()}async getFileId(){return this.db.data.ali_file_id}async cacheFileId(t){this.db.data.ali_file_id=t,await this.db.write()}async getPartInfoList(){return this.db.data.ali_part_info_list}async cachePartInfoList(t){const a=this.getExpiredAt();return this.db.data.ali_part_info_list=t.map(t=>({...t,expired_at:a})),await this.db.write(),this.db.data.ali_part_info_list}async getProofCode(t){const{part:a}=this.params,i=a.size,s=o("md5").update(t).digest("hex").slice(0,16),e=BigInt(`0x${s}`),r=Number(e%BigInt(i)),h=r,n=Math.min(i,r+8),p=a.start+h,l=a.start+n;return(await d(a.path,p,l-1)).toString("base64")}};import l from"axios";import{HostType as u}from"@soga/types";import{UploadProgress as c}from"@soga/node-types";import{buildDError as _}from"@soga/error";var m=class extends p{queue_list;async onUploadSuccess(t){return await this.clearCache(),await this.postProgress({type:c.upload_ali,percent:1,host_type:u.ALI}),{file_id:t,drive_id:this.drive_id}}async start(){try{return await this.coreStart(0)}catch(t){throw _(t,{message:`upload to ali failed: ${t.message}`,detail:`upload to ali failed, upload to folder: ${this.params.cloud_folder_id}`})}}async coreStart(t=0){if(t++>3)throw _(new Error,{message:`upload to ali failed file_id: ${this.params.cloud_folder_id}`,detail:`upload to ali failed file_id: ${this.params.cloud_folder_id}`});await this.init();const a=await this.getUploadId();if(a){if(!await this.correctUploaded(a))return await this.clearCache(),await this.coreStart(t+1)}else{const{need_upload:t,data:a}=await this.precreate();if(!t)return await this.onUploadSuccess(a.file_id)}await this.uploadChunks();const i=await this.postcreate();return await this.onUploadSuccess(i.file_id)}async postcreate(){const t=await this.getFileId(),a=await this.getUploadId(),i=await this.axios.post("/adrive/v1.0/openFile/complete",{drive_id:this.drive_id,file_id:t,upload_id:a});if(i)return{file_id:i.data.file_id}}async uploadChunks(){this.queue_list;const t=async()=>{if(this.queue_list.length){await this.autoRefreshUploadUrl();const a=this.queue_list.shift();await this.uploadOneChunk(a);const i=Math.min(a.part_number*this.chunk_size,this.params.part.size)/this.params.part.size;await this.postProgress({type:c.upload_ali,percent:.97*i,host_type:u.ALI}),await t()}};await t()}async uploadOneChunk({upload_url:a,part_number:i}){const s=i-1,e=this.group_list[s],r=this.params.part.path,o=t(r,{start:e.start,end:e.end});await l.put(a,o,{headers:{"Content-Type":null,"User-Agent":this.params.ua},timeout:12e4})}async precreate(){const{access_token:t}=await this.sdk.getAliHostAuthData({host_id:this.params.host_id,refresh:!1}),a=await this.getProofCode(t),i=this.group_list.map((t,a)=>({part_number:a+1})),{part:s}=this.params,e=`dpan_p_${s.md5}.txt`,r={drive_id:this.drive_id,parent_file_id:this.params.cloud_folder_id,name:e,type:"file",check_name_mode:"refuse",part_info_list:i,size:s.size,content_hash_name:"sha1",content_hash:s.sha1,proof_code:a,proof_version:"v1"},o=await l.post("/adrive/v1.0/openFile/create",r,{baseURL:this.domain,headers:{Authorization:`Bearer ${t}`}}),{data:d}=o;return d.exist&&"available"===d.status||d.rapid_upload?{need_upload:!1,data:{file_id:d.file_id}}:(await this.cacheFileId(d.file_id),await this.cacheUploadId(d.upload_id),await this.cachePartInfoList(d.part_info_list),this.queue_list=await this.cachePartInfoList(d.part_info_list),{need_upload:!0})}async correctUploaded(t){const a=await this.getFileId();try{const i=(await this.axios.post("/adrive/v1.0/openFile/listUploadedParts",{drive_id:this.drive_id,upload_id:t,file_id:a})).data.uploaded_parts.map(t=>t.part_number),s=await this.getPartInfoList();if(s.length){const t=s.filter(t=>!i.includes(t.part_number));return this.queue_list=t,!0}return!1}catch(t){return!1}}async autoRefreshUploadUrl(){const{queue_list:t}=this;if(t[0].expired_at>Date.now())return;const a=t.map(t=>({part_number:t.part_number})),i={drive_id:this.drive_id,upload_id:await this.getUploadId(),file_id:await this.getFileId(),part_info_list:a},s=await this.axios.post("/adrive/v1.0/openFile/getUploadUrl",i);s&&(this.queue_list=await this.cachePartInfoList(s.data.part_info_list),await this.cachePartInfoList(s.data.part_info_list))}};import f from"form-data";import{getFileBufferSlice as w}from"@soga/utils";import{calculateMd5 as g}from"@soga/utils";import{HostType as b}from"@soga/types";import y from"axios";import{buildDError as k}from"@soga/error";var v=class extends r{axios;chunk_size=4194304;group_list=[];md5_list=[];params;constructor(t){super(t,b.BAIDU),this.params=t,this.initAxios()}async init(){await super.init(),await this.initMd5List()}initAxios(){this.axios=y.create({}),this.axios.interceptors.response.use(t=>t.data,t=>{throw k(t,{message:t.message,detail:`request baidu netdisk error code: ${t.status||"unknown"}`,stack:t.stack,rewritable:!1})})}async initMd5List(){const t=this.db.data.baidu_md5_list;if(t)return void(this.md5_list=t);const a=this.group_list,i=[];for(const t of a){const{path:a}=this.params.part,s=await g({file:a,start:t.start,end:t.end});i.push(s)}this.md5_list=i,this.db.data.baidu_md5_list=i,await this.db.write()}getSearchParams(t){Object.keys(t).forEach(a=>{void 0===t[a]&&delete t[a]});return new URLSearchParams(t).toString()}async getUploadId(){return this.db.data.baidu_upload_id}async cacheUploadId(t){this.db.data.baidu_upload_id=t,await this.db.write()}async clearCache(){this.db.data={},await this.db.write()}};import{parse as U}from"path";import{HostType as x}from"@soga/types";import{buildDError as I}from"@soga/error";import{UploadProgress as P}from"@soga/node-types";var $=class extends v{queue_list=[];get ua(){return this.params.ua||"pan.baidu.com"}get cloudPath(){return`${this.params.cloud_folder_path}/dpan_p_${this.params.part.md5}.bin`}upload_server="";async initUploadUrl(){const{access_token:t}=await this.sdk.getBaiduHostAuthData({host_id:this.params.host_id,refresh:!1}),a=await this.getUploadId(),i=`https://d.pcs.baidu.com/rest/2.0/pcs/file?${this.getSearchParams({method:"locateupload",appid:`${this.params.appid}`,access_token:t,path:this.cloudPath,uploadid:a,upload_version:"2.0"})}`,s=await this.axios.get(i);s.quic_servers.length?this.upload_server=s.quic_servers[0].server:s.servers.length?this.upload_server=s.servers[0].server:s.bak_servers.length?this.upload_server=s.bak_servers[0].server:this.upload_server="https://d.pcs.baidu.com"}async start(){try{await this.init();const t=await this.precreate();let a=t.data;return t.need_upload&&(await this.initUploadUrl(),await this.uploadChunks(),a=await this.postcreate()),await this.postProgress({type:P.upload_baidu,percent:1,host_type:x.BAIDU}),await this.clearCache(),a}catch(t){throw I(t,{message:`upload to baidu failed: ${t.message}`,detail:`upload to baidu failed, upload to folder: ${this.params.cloud_folder_path}`})}}async postcreate(){const t=await this.getUploadId(),a=this.cloudPath,i=this.md5_list,s=this.params.part.size,{access_token:e}=await this.sdk.getBaiduHostAuthData({host_id:this.params.host_id,refresh:!1}),r=`https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=${e}`,o=this.getSearchParams({path:a,size:`${s}`,isdir:"0",block_list:JSON.stringify(i),uploadid:t||void 0,rtype:"2"}),d=await this.axios.post(r,o,{headers:{"User-Agent":this.ua}});if(d.errno)throw I(new Error,{message:`Unable to create file when uploading to Baidu Netdisk: ${d.errno}`,detail:`Unable to create file when uploading to Baidu Netdisk: ${JSON.stringify(d)}`});return{fs_id:d.fs_id}}async uploadChunks(){const t=this.queue_list.map(t=>t),a=async()=>{if(t.length){const i=t.shift(),s=await this.getChunkInfo(i);await this.uploadOneChunk(s);const e=Math.min((i+1)*this.chunk_size,this.params.part.size)/this.params.part.size;await this.postProgress({type:P.upload_baidu,percent:.98*e,host_type:x.BAIDU}),await a()}};await a()}async precreate(){const t=this.md5_list,{md4:a,md5:i,size:s}=this.params.part,{access_token:e}=await this.sdk.getBaiduHostAuthData({host_id:this.params.host_id,refresh:!1}),r=await this.getUploadId(),o=`https://pan.baidu.com/rest/2.0/xpan/file?method=precreate&access_token=${e}`,d=this.getSearchParams({path:this.cloudPath,size:`${s}`,isdir:"0",block_list:JSON.stringify(t),autoinit:"1",rtype:"2","content-md5":i,"slice-md5":a,uploadid:r}),h=await this.axios.post(o,d,{headers:{"User-Agent":this.ua}});if(h.errno)throw I(new Error,{message:`Unable to precreate file when uploading to Baidu Netdisk: ${h.errno}`,detail:`Unable to precreate file when uploading to Baidu Netdisk: ${JSON.stringify(h)}`});return 1==h.return_type?(h.uploadid&&h.uploadid!=r&&await this.cacheUploadId(h.uploadid),this.queue_list=h.block_list,{need_upload:!0}):{need_upload:!1,data:{fs_id:h.info.fs_id}}}async uploadOneChunk(t,a=0){try{const{host_id:i}=this.baseParams,s=await this.getUploadId(),e=this.cloudPath,{access_token:r}=await this.sdk.getBaiduHostAuthData({host_id:i,refresh:!1}),o=`${this.upload_server}/rest/2.0/pcs/superfile2?access_token=${r}&method=upload&type=tmpfile&path=${e}&uploadid=${s}&partseq=${t.index}`,d=new f;d.append("file",t.buffer,`${t.filename}`);const h=await this.axios.post(o,d,{timeout:2e4*(1.5*a+1),headers:{"User-Agent":this.ua}});if(!h)return;return h.md5}catch(i){if(a<4)return i.message?.includes("timeout")&&await this.initUploadUrl(),await this.uploadOneChunk(t,a+1);throw i}}async getChunkInfo(t){const a=this.group_list[t],i=this.params.part.path,s=await w(i,a.start,a.end),{md5_list:e}=this;return{index:t,buffer:s,md5:e[t],filename:U(i).base,size:s.byteLength}}},A=async t=>{const a=new m(t);return await a.start()},z=async t=>{const a=new $(t);return await a.start()};export{m as AliUploader,$ as BaiduUploader,A as uploadAli,z as uploadBaidu};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soga/part-uploader",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -10,26 +10,30 @@
|
|
|
10
10
|
"files": [
|
|
11
11
|
"dist"
|
|
12
12
|
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "rimraf dist && tsup src/main.ts --format cjs,esm --dts --minify terser",
|
|
15
|
+
"prepublishOnly": "npm run build"
|
|
16
|
+
},
|
|
13
17
|
"keywords": [],
|
|
14
18
|
"author": "",
|
|
15
19
|
"license": "ISC",
|
|
16
20
|
"dependencies": {
|
|
17
|
-
"@soga/
|
|
18
|
-
"@soga/
|
|
19
|
-
"@soga/
|
|
20
|
-
"@soga/
|
|
21
|
+
"@soga/types": "latest",
|
|
22
|
+
"@soga/node-types": "latest",
|
|
23
|
+
"@soga/lowdb": "latest",
|
|
24
|
+
"@soga/error": "latest",
|
|
25
|
+
"@soga/sdk": "latest",
|
|
26
|
+
"@soga/utils": "latest",
|
|
21
27
|
"axios": "^1.9.0",
|
|
22
28
|
"form-data": "^4.0.2",
|
|
23
|
-
"fs-extra": "^11.3.
|
|
29
|
+
"fs-extra": "^11.3.2"
|
|
24
30
|
},
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"dev": "ts-node ./src/main.ts",
|
|
33
|
-
"lint": "eslint . --ext .ts"
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@soga/typescript-config": "latest",
|
|
33
|
+
"rimraf": "^6.0.1",
|
|
34
|
+
"terser": "^5.43.1",
|
|
35
|
+
"tsup": "^8.5.0",
|
|
36
|
+
"typescript": "^5.8.3",
|
|
37
|
+
"@types/node": "^24.5.2"
|
|
34
38
|
}
|
|
35
|
-
}
|
|
39
|
+
}
|
package/README.md
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
# single-uploader
|
package/dist/types/main.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { FilePartItem } from '@soga/types';
|
|
2
|
-
import { MessagePort } from 'node:worker_threads';
|
|
3
|
-
export type CommonPartUploaderParams = {
|
|
4
|
-
file_id: number;
|
|
5
|
-
host_id: number;
|
|
6
|
-
part_id: number;
|
|
7
|
-
output_root: string;
|
|
8
|
-
part: FilePartItem;
|
|
9
|
-
sdk_domain: string;
|
|
10
|
-
port?: MessagePort;
|
|
11
|
-
};
|
|
12
|
-
export type UploadBaiduParams = CommonPartUploaderParams & {
|
|
13
|
-
cloud_folder_path: string;
|
|
14
|
-
ua?: string;
|
|
15
|
-
};
|
|
16
|
-
export type UploadAliParams = CommonPartUploaderParams & {
|
|
17
|
-
cloud_folder_id: string;
|
|
18
|
-
ua?: string;
|
|
19
|
-
};
|
package/dist/types/main.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { AxiosInstance } from 'axios';
|
|
2
|
-
import { UploadAliParams } from '../types/main';
|
|
3
|
-
import { CompleteGroupList } from '../utils/chunk';
|
|
4
|
-
import { BaseUploader } from './base-uploader';
|
|
5
|
-
export declare class AliHelper extends BaseUploader {
|
|
6
|
-
params: UploadAliParams;
|
|
7
|
-
protected drive_id: string;
|
|
8
|
-
protected axios: AxiosInstance;
|
|
9
|
-
protected domain: string;
|
|
10
|
-
protected chunk_size: number;
|
|
11
|
-
protected group_list: CompleteGroupList;
|
|
12
|
-
constructor(params: UploadAliParams);
|
|
13
|
-
protected getExpiredAt(): number;
|
|
14
|
-
private initAxios;
|
|
15
|
-
protected init(): Promise<void>;
|
|
16
|
-
protected getUploadId(): Promise<string>;
|
|
17
|
-
protected cacheUploadId(upload_id: string): Promise<void>;
|
|
18
|
-
protected clearCache(): Promise<void>;
|
|
19
|
-
protected getFileId(): Promise<string>;
|
|
20
|
-
protected cacheFileId(file_id: string): Promise<void>;
|
|
21
|
-
protected getPartInfoList(): Promise<{
|
|
22
|
-
part_number: number;
|
|
23
|
-
upload_url: string;
|
|
24
|
-
expired_at: number;
|
|
25
|
-
}[]>;
|
|
26
|
-
protected cachePartInfoList(part_info_list: {
|
|
27
|
-
part_number: number;
|
|
28
|
-
upload_url: string;
|
|
29
|
-
}[]): Promise<{
|
|
30
|
-
part_number: number;
|
|
31
|
-
upload_url: string;
|
|
32
|
-
expired_at: number;
|
|
33
|
-
}[]>;
|
|
34
|
-
protected getProofCode(access_token: string): Promise<string>;
|
|
35
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var __importDefault=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.AliHelper=void 0;const axios_1=__importDefault(require("axios")),base_uploader_1=require("./base-uploader"),crypto_1=require("crypto"),utils_1=require("@soga/utils"),types_1=require("@soga/types");class AliHelper extends base_uploader_1.BaseUploader{params;drive_id;axios;domain="https://openapi.alipan.com";chunk_size=4194304;group_list=[];constructor(t){super(t,types_1.HostType.ALI),this.params=t,this.initAxios()}getExpiredAt(){return Date.now()+348e4}initAxios(){this.axios=axios_1.default.create({baseURL:this.domain}),this.axios.interceptors.request.use((async t=>{const{access_token:i}=await this.sdk.getAliHostAuthData({host_id:this.params.host_id,refresh:!1});return t.headers.Authorization=`Bearer ${i}`,t}),(t=>Promise.reject(t)))}async init(){await super.init();const{drive_id:t}=await this.sdk.getAliHostAuthData({host_id:this.params.host_id,refresh:!1});this.drive_id=t}async getUploadId(){return this.db.data.ali_upload_id}async cacheUploadId(t){this.db.data.ali_upload_id=t,await this.db.write()}async clearCache(){this.db.data={},await this.db.write()}async getFileId(){return this.db.data.ali_file_id}async cacheFileId(t){this.db.data.ali_file_id=t,await this.db.write()}async getPartInfoList(){return this.db.data.ali_part_info_list}async cachePartInfoList(t){const i=this.getExpiredAt();return this.db.data.ali_part_info_list=t.map((t=>({...t,expired_at:i}))),await this.db.write(),this.db.data.ali_part_info_list}async getProofCode(t){const{part:i}=this.params,e=i.size,a=(0,crypto_1.createHash)("md5").update(t).digest("hex").slice(0,16),s=BigInt(`0x${a}`),r=Number(s%BigInt(e)),o=r,d=Math.min(e,r+8),l=i.start+o,n=i.start+d;return(await(0,utils_1.getFileBufferSlice)(i.path,l,n-1)).toString("base64")}}exports.AliHelper=AliHelper;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { AliHelper } from './ali-helper';
|
|
2
|
-
export declare class AliUploader extends AliHelper {
|
|
3
|
-
private queue_list;
|
|
4
|
-
private onUploadSuccess;
|
|
5
|
-
start(): Promise<{
|
|
6
|
-
file_id: string;
|
|
7
|
-
drive_id: string;
|
|
8
|
-
}>;
|
|
9
|
-
private coreStart;
|
|
10
|
-
protected postcreate(): Promise<{
|
|
11
|
-
file_id: string;
|
|
12
|
-
}>;
|
|
13
|
-
protected uploadChunks(): Promise<void>;
|
|
14
|
-
protected uploadOneChunk({ upload_url, part_number, }: {
|
|
15
|
-
upload_url: string;
|
|
16
|
-
part_number: number;
|
|
17
|
-
}): Promise<void>;
|
|
18
|
-
protected precreate(): Promise<{
|
|
19
|
-
need_upload: boolean;
|
|
20
|
-
data: {
|
|
21
|
-
file_id: string;
|
|
22
|
-
};
|
|
23
|
-
} | {
|
|
24
|
-
need_upload: boolean;
|
|
25
|
-
data?: undefined;
|
|
26
|
-
}>;
|
|
27
|
-
protected correctUploaded(upload_id: string): Promise<boolean>;
|
|
28
|
-
protected autoRefreshUploadUrl(): Promise<void>;
|
|
29
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var __importDefault=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.AliUploader=void 0;const fs_1=require("fs"),ali_helper_1=require("./ali-helper"),axios_1=__importDefault(require("axios")),types_1=require("@soga/types");class AliUploader extends ali_helper_1.AliHelper{queue_list;async onUploadSuccess(t){return await this.clearCache(),await this.postProgress({type:types_1.UploadProcessStep.upload_ali,percent:1,host_type:types_1.HostType.ALI}),{file_id:t,drive_id:this.drive_id}}async start(){return await this.coreStart(0)}async coreStart(t=0){if(t++>3)throw new Error("upload failed");await this.init();const a=await this.getUploadId();if(a){if(!await this.correctUploaded(a))return await this.clearCache(),await this.coreStart(t+1)}else{const{need_upload:t,data:a}=await this.precreate();if(!t)return await this.onUploadSuccess(a.file_id)}await this.uploadChunks();const e=await this.postcreate();return await this.onUploadSuccess(e.file_id)}async postcreate(){const t=await this.getFileId(),a=await this.getUploadId(),e=await this.axios.post("/adrive/v1.0/openFile/complete",{drive_id:this.drive_id,file_id:t,upload_id:a});if(e)return{file_id:e.data.file_id}}async uploadChunks(){this.queue_list;const t=async()=>{if(this.queue_list.length){await this.autoRefreshUploadUrl();const a=this.queue_list.shift();await this.uploadOneChunk(a);const e=Math.min(a.part_number*this.chunk_size,this.params.part.size)/this.params.part.size;await this.postProgress({type:types_1.UploadProcessStep.upload_ali,percent:.97*e,host_type:types_1.HostType.ALI}),await t()}};await t()}async uploadOneChunk({upload_url:t,part_number:a}){const e=a-1,i=this.group_list[e],s=this.params.part.path,r=(0,fs_1.createReadStream)(s,{start:i.start,end:i.end});await axios_1.default.put(t,r,{headers:{"Content-Type":null,"User-Agent":this.params.ua},timeout:12e4})}async precreate(){const{access_token:t}=await this.sdk.getAliHostAuthData({host_id:this.params.host_id,refresh:!1}),a=await this.getProofCode(t),e=this.group_list.map(((t,a)=>({part_number:a+1}))),{part:i}=this.params,s=`dpan_p_${i.md5}.txt`,r={drive_id:this.drive_id,parent_file_id:this.params.cloud_folder_id,name:s,type:"file",check_name_mode:"refuse",part_info_list:e,size:i.size,content_hash_name:"sha1",content_hash:i.sha1,proof_code:a,proof_version:"v1"},o=await axios_1.default.post("/adrive/v1.0/openFile/create",r,{baseURL:this.domain,headers:{Authorization:`Bearer ${t}`}}),{data:d}=o;return d.exist&&"available"===d.status||d.rapid_upload?{need_upload:!1,data:{file_id:d.file_id}}:(await this.cacheFileId(d.file_id),await this.cacheUploadId(d.upload_id),await this.cachePartInfoList(d.part_info_list),this.queue_list=await this.cachePartInfoList(d.part_info_list),{need_upload:!0})}async correctUploaded(t){const a=await this.getFileId();try{const e=(await this.axios.post("/adrive/v1.0/openFile/listUploadedParts",{drive_id:this.drive_id,upload_id:t,file_id:a})).data.uploaded_parts.map((t=>t.part_number)),i=await this.getPartInfoList();if(i.length){const t=i.filter((t=>!e.includes(t.part_number)));return this.queue_list=t,!0}return!1}catch(t){return!1}}async autoRefreshUploadUrl(){const{queue_list:t}=this;if(t[0].expired_at>Date.now())return;const a=t.map((t=>({part_number:t.part_number}))),e={drive_id:this.drive_id,upload_id:await this.getUploadId(),file_id:await this.getFileId(),part_info_list:a},i=await this.axios.post("/adrive/v1.0/openFile/getUploadUrl",e);i&&(this.queue_list=await this.cachePartInfoList(i.data.part_info_list),await this.cachePartInfoList(i.data.part_info_list))}}exports.AliUploader=AliUploader;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { UploadBaiduParams } from '../types/main';
|
|
2
|
-
import { BaseUploader } from './base-uploader';
|
|
3
|
-
import { CompleteGroupList } from '../utils/chunk';
|
|
4
|
-
export declare class BaiduHelper extends BaseUploader {
|
|
5
|
-
protected chunk_size: number;
|
|
6
|
-
protected group_list: CompleteGroupList;
|
|
7
|
-
protected md5_list: string[];
|
|
8
|
-
protected params: UploadBaiduParams;
|
|
9
|
-
constructor(params: UploadBaiduParams);
|
|
10
|
-
protected init(): Promise<void>;
|
|
11
|
-
protected initMd5List(): Promise<void>;
|
|
12
|
-
protected getSearchParams(payload: Record<string, string>): string;
|
|
13
|
-
protected getUploadId(): Promise<string>;
|
|
14
|
-
protected cacheUploadId(upload_id: string): Promise<void>;
|
|
15
|
-
protected clearCache(): Promise<void>;
|
|
16
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BaiduHelper=void 0;const utils_1=require("@soga/utils"),base_uploader_1=require("./base-uploader"),types_1=require("@soga/types");class BaiduHelper extends base_uploader_1.BaseUploader{chunk_size=4194304;group_list=[];md5_list=[];params;constructor(t){super(t,types_1.HostType.BAIDU),this.params=t}async init(){await super.init(),await this.initMd5List()}async initMd5List(){const t=this.db.data.baidu_md5_list;if(t)return void(this.md5_list=t);const a=this.group_list,s=[];for(const t of a){const{path:a}=this.params.part,e=await(0,utils_1.calculateMd5)({file:a,start:t.start,end:t.end});s.push(e)}this.md5_list=s,this.db.data.baidu_md5_list=s,await this.db.write()}getSearchParams(t){Object.keys(t).forEach((a=>{void 0===t[a]&&delete t[a]}));return new URLSearchParams(t).toString()}async getUploadId(){return this.db.data.baidu_upload_id}async cacheUploadId(t){this.db.data.baidu_upload_id=t,await this.db.write()}async clearCache(){this.db.data={},await this.db.write()}}exports.BaiduHelper=BaiduHelper;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { BaiduHelper } from './baidu-helper';
|
|
2
|
-
export declare class BaiduUploader extends BaiduHelper {
|
|
3
|
-
private queue_list;
|
|
4
|
-
protected get cloudPath(): string;
|
|
5
|
-
start(): Promise<{
|
|
6
|
-
fs_id: number;
|
|
7
|
-
}>;
|
|
8
|
-
protected postcreate(): Promise<{
|
|
9
|
-
fs_id: number;
|
|
10
|
-
}>;
|
|
11
|
-
protected uploadChunks(): Promise<void>;
|
|
12
|
-
protected precreate(): Promise<{
|
|
13
|
-
need_upload: boolean;
|
|
14
|
-
data?: undefined;
|
|
15
|
-
} | {
|
|
16
|
-
need_upload: boolean;
|
|
17
|
-
data: {
|
|
18
|
-
fs_id: number;
|
|
19
|
-
};
|
|
20
|
-
}>;
|
|
21
|
-
private uploadOneChunk;
|
|
22
|
-
private getChunkInfo;
|
|
23
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var __importDefault=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.BaiduUploader=void 0;const form_data_1=__importDefault(require("form-data")),utils_1=require("@soga/utils"),baidu_helper_1=require("./baidu-helper"),axios_1=__importDefault(require("axios")),path_1=require("path"),types_1=require("@soga/types");class BaiduUploader extends baidu_helper_1.BaiduHelper{queue_list=[];get cloudPath(){return`${this.params.cloud_folder_path}/dpan_p_${this.params.part.md5}.bin`}async start(){await this.init();const t=await this.precreate();let a=t.data;return t.need_upload&&(await this.uploadChunks(),a=await this.postcreate()),await this.postProgress({type:types_1.UploadProcessStep.upload_baidu,percent:1,host_type:types_1.HostType.BAIDU}),await this.clearCache(),a}async postcreate(){const t=await this.getUploadId(),a=this.cloudPath,e=this.md5_list,s=this.params.part.size,{access_token:i}=await this.sdk.getBaiduHostAuthData({host_id:this.params.host_id,refresh:!1}),r=`https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=${i}`,o=this.getSearchParams({path:a,size:`${s}`,isdir:"0",block_list:JSON.stringify(e),uploadid:t||void 0,rtype:"2"}),d=await axios_1.default.post(r,o);if(!d)return;const{data:p}=d;if(!p.errno)return{fs_id:p.fs_id};{const t=`upload create file error: ${p.errno}`;await this.throwError({code:15300,message:t,stack:JSON.stringify(p)})}}async uploadChunks(){const t=this.queue_list.map((t=>t)),a=async()=>{if(t.length){const e=t.shift(),s=await this.getChunkInfo(e);await this.uploadOneChunk(s);const i=Math.min((e+1)*this.chunk_size,this.params.part.size)/this.params.part.size;await this.postProgress({type:types_1.UploadProcessStep.upload_baidu,percent:.98*i,host_type:types_1.HostType.BAIDU}),await a()}};await a()}async precreate(){const t=this.md5_list,{md4:a,md5:e,size:s}=this.params.part,{access_token:i}=await this.sdk.getBaiduHostAuthData({host_id:this.params.host_id,refresh:!1}),r=await this.getUploadId(),o=`https://pan.baidu.com/rest/2.0/xpan/file?method=precreate&access_token=${i}`,d=this.getSearchParams({path:this.cloudPath,size:`${s}`,isdir:"0",block_list:JSON.stringify(t),autoinit:"1",rtype:"2","content-md5":e,"slice-md5":a,uploadid:r}),p=await axios_1.default.post(o,d,{headers:{"User-Agent":this.params.ua}});if(!p)return;const{data:h}=p;if(!h.errno)return 1==h.return_type?(h.uploadid&&h.uploadid!=r&&await this.cacheUploadId(h.uploadid),this.queue_list=h.block_list,{need_upload:!0}):{need_upload:!1,data:{fs_id:h.info.fs_id}};{const t=`precreate error: ${h.errno}`;await this.throwError({code:15100,message:t,stack:JSON.stringify(h)})}}async uploadOneChunk(t){const a=await this.getUploadId(),e=this.cloudPath,{access_token:s}=await this.sdk.getBaiduHostAuthData({host_id:this.params.host_id,refresh:!1}),i=`https://d.pcs.baidu.com/rest/2.0/pcs/superfile2?access_token=${s}&method=upload&type=tmpfile&path=${e}&uploadid=${a}&partseq=${t.index}`,r=new form_data_1.default;r.append("file",t.buffer,`${t.filename}`);const o=await axios_1.default.post(i,r,{timeout:6e4});if(o)return o.data.md5}async getChunkInfo(t){const a=this.group_list[t],e=this.params.part.path,s=await(0,utils_1.getFileBufferSlice)(e,a.start,a.end),{md5_list:i}=this;return{index:t,buffer:s,md5:i[t],filename:(0,path_1.parse)(e).base,size:s.byteLength}}}exports.BaiduUploader=BaiduUploader;
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { CommonPartUploaderParams } from '../types/main';
|
|
2
|
-
import { HostType, UploadProcessStep, UploadWorkerMessage } from '@soga/types';
|
|
3
|
-
import { Sdk } from '@soga/sdk';
|
|
4
|
-
import { CompleteGroupList } from '../utils/chunk';
|
|
5
|
-
import { MessagePort } from 'node:worker_threads';
|
|
6
|
-
import { LowType } from '@soga/bridge';
|
|
7
|
-
export declare class BaseUploader {
|
|
8
|
-
protected port: MessagePort | null;
|
|
9
|
-
protected GROUP_LIST_KEY: string;
|
|
10
|
-
protected chunk_size: number;
|
|
11
|
-
protected group_list: CompleteGroupList;
|
|
12
|
-
protected host_type: HostType;
|
|
13
|
-
protected is_stop: boolean;
|
|
14
|
-
db: LowType<{
|
|
15
|
-
group_list?: CompleteGroupList;
|
|
16
|
-
baidu_upload_id?: string;
|
|
17
|
-
baidu_md5_list?: string[];
|
|
18
|
-
ali_upload_id?: string;
|
|
19
|
-
ali_file_id?: string;
|
|
20
|
-
ali_part_info_list?: {
|
|
21
|
-
part_number: number;
|
|
22
|
-
upload_url: string;
|
|
23
|
-
expired_at: number;
|
|
24
|
-
}[];
|
|
25
|
-
}>;
|
|
26
|
-
protected sdk: Sdk;
|
|
27
|
-
protected baseParams: CommonPartUploaderParams;
|
|
28
|
-
constructor(params: CommonPartUploaderParams, host_type: HostType);
|
|
29
|
-
protected init(): Promise<void>;
|
|
30
|
-
protected initGroupList(): Promise<void>;
|
|
31
|
-
private initDb;
|
|
32
|
-
throwError(error: {
|
|
33
|
-
code: number;
|
|
34
|
-
message: string;
|
|
35
|
-
stack?: string;
|
|
36
|
-
}): Promise<void>;
|
|
37
|
-
postProgress(progress: {
|
|
38
|
-
type: UploadProcessStep;
|
|
39
|
-
percent: number;
|
|
40
|
-
host_type: HostType;
|
|
41
|
-
}): Promise<void>;
|
|
42
|
-
protected postMessage(data: UploadWorkerMessage): Promise<void>;
|
|
43
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BaseUploader=void 0;const utils_1=require("@soga/utils"),path_1=require("path"),sdk_1=require("@soga/sdk"),chunk_1=require("../utils/chunk"),bridge_1=require("@soga/bridge");class BaseUploader{port=null;GROUP_LIST_KEY="group_list";chunk_size=4194304;group_list=[];host_type;is_stop=!1;db;sdk;baseParams;constructor(s,t){this.host_type=t,this.baseParams=s,this.port=s.port??null}async init(){await this.initDb(),this.sdk=(0,sdk_1.getSdk)(this.baseParams.sdk_domain),await this.initGroupList()}async initGroupList(){if(this.db.data.group_list)return void(this.group_list=this.db.data.group_list);const s=(0,chunk_1.groupChunk)(this.baseParams.part,this.chunk_size);this.group_list=s,this.db.data.group_list=s,await this.db.write()}async initDb(){const s=`${this.host_type}_${this.baseParams.part.md5}.json`,t=(0,path_1.resolve)(this.baseParams.output_root,s),i=await(0,bridge_1.getDb)(t,{});return this.db=i,i}async throwError(s){throw new utils_1.SimpleError(s)}async postProgress(s){await this.postMessage({id:this.baseParams.file_id,type:"percent",data:{part_id:this.baseParams.part_id,host_type:this.host_type,percent:s.percent}})}async postMessage(s){this.port&&this.port.postMessage(s)}}exports.BaseUploader=BaseUploader;
|
package/dist/utils/chunk.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { FilePartItem } from '@soga/types';
|
|
2
|
-
export declare function groupChunk(fileItem: FilePartItem, chunkSize?: number): CompleteGroupList;
|
|
3
|
-
export type GroupItem = {
|
|
4
|
-
index: number;
|
|
5
|
-
file: string;
|
|
6
|
-
start: number;
|
|
7
|
-
end: number;
|
|
8
|
-
finish: boolean;
|
|
9
|
-
};
|
|
10
|
-
type CompleteGroupItem = GroupItem & {
|
|
11
|
-
size: number;
|
|
12
|
-
};
|
|
13
|
-
export type CompleteGroupList = CompleteGroupItem[];
|
|
14
|
-
export {};
|
package/dist/utils/chunk.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";function groupChunk(e,t=4194304){const s=[],i=e.size;if(i<=t)s.push({file:e.file,start:e.start,end:e.end,finish:!0});else{const n=i/t,r=Math.floor(n),f=n===r;for(let i=0;i<r;i++)s.push({file:e.file,start:e.start+i*t,end:e.start+(i+1)*t-1,finish:!!f&&i===r});if(!f){const i=e.start+r*t;s.push({file:e.file,start:i,end:e.end,finish:!0})}}return s.map(((e,t)=>({...e,size:e.end-e.start+1,index:t})))}Object.defineProperty(exports,"__esModule",{value:!0}),exports.groupChunk=groupChunk;
|