@soga/part-uploader 0.2.23 → 0.2.24
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.ts +1 -1
- package/dist/uploader/ali-helper.d.ts +8 -7
- package/dist/uploader/ali-helper.js +1 -1
- package/dist/uploader/ali-uploader.d.ts +4 -3
- package/dist/uploader/ali-uploader.js +1 -1
- package/dist/uploader/baidu-uploader.js +1 -1
- package/dist/uploader/base-uploader.d.ts +3 -6
- package/dist/uploader/base-uploader.js +1 -1
- package/package.json +2 -2
package/dist/main.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { UploadAliParams, UploadBaiduParams } from './types/main';
|
|
|
2
2
|
export { BaiduUploader } from './uploader/baidu-uploader';
|
|
3
3
|
export { AliUploader } from './uploader/ali-uploader';
|
|
4
4
|
export declare const uploadAli: (params: UploadAliParams) => Promise<{
|
|
5
|
-
drive_id: string;
|
|
6
5
|
file_id: string;
|
|
6
|
+
drive_id: string;
|
|
7
7
|
}>;
|
|
8
8
|
export declare const uploadBaidu: (params: UploadBaiduParams) => Promise<{
|
|
9
9
|
fs_id: number;
|
|
@@ -19,16 +19,17 @@ export declare class AliHelper extends BaseUploader {
|
|
|
19
19
|
protected getFileId(): Promise<string>;
|
|
20
20
|
protected cacheFileId(file_id: string): Promise<void>;
|
|
21
21
|
protected getPartInfoList(): Promise<{
|
|
22
|
-
|
|
22
|
+
part_number: number;
|
|
23
|
+
upload_url: string;
|
|
23
24
|
expired_at: number;
|
|
24
|
-
|
|
25
|
-
part_number: number;
|
|
26
|
-
upload_url: string;
|
|
27
|
-
}[];
|
|
28
|
-
}>;
|
|
25
|
+
}[]>;
|
|
29
26
|
protected cachePartInfoList(part_info_list: {
|
|
30
27
|
part_number: number;
|
|
31
28
|
upload_url: string;
|
|
32
|
-
}[]): Promise<
|
|
29
|
+
}[]): Promise<{
|
|
30
|
+
part_number: number;
|
|
31
|
+
upload_url: string;
|
|
32
|
+
expired_at: number;
|
|
33
|
+
}[]>;
|
|
33
34
|
protected getProofCode(): Promise<string>;
|
|
34
35
|
}
|
|
@@ -1 +1 @@
|
|
|
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:
|
|
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(){const{access_token:t}=await this.sdk.getAliHostAuthData({host_id:this.params.host_id,refresh:!1}),{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),_=i.start+o,h=i.start+d;return(await(0,utils_1.getFileBufferSlice)(i.path,_,h-1)).toString("base64")}}exports.AliHelper=AliHelper;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { AliHelper } from './ali-helper';
|
|
2
2
|
export declare class AliUploader extends AliHelper {
|
|
3
|
-
private queue_expired_at;
|
|
4
3
|
private queue_list;
|
|
4
|
+
private onUploadSuccess;
|
|
5
5
|
start(): Promise<{
|
|
6
|
-
drive_id: string;
|
|
7
6
|
file_id: string;
|
|
7
|
+
drive_id: string;
|
|
8
8
|
}>;
|
|
9
|
+
private coreStart;
|
|
9
10
|
protected postcreate(): Promise<{
|
|
10
11
|
file_id: string;
|
|
11
12
|
}>;
|
|
@@ -23,6 +24,6 @@ export declare class AliUploader extends AliHelper {
|
|
|
23
24
|
need_upload: boolean;
|
|
24
25
|
data?: undefined;
|
|
25
26
|
}>;
|
|
26
|
-
protected correctUploaded(upload_id: string): Promise<
|
|
27
|
+
protected correctUploaded(upload_id: string): Promise<boolean>;
|
|
27
28
|
protected autoRefreshUploadUrl(): Promise<void>;
|
|
28
29
|
}
|
|
@@ -1 +1 @@
|
|
|
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{
|
|
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()}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 t=await this.getProofCode(),a=this.group_list.map(((t,a)=>({part_number:a+1}))),{part:e}=this.params,i=`dpan_p_${e.md5}.txt`,s={drive_id:this.drive_id,parent_file_id:this.params.cloud_folder_id,name:i,type:"file",check_name_mode:"refuse",part_info_list:a,size:e.size,content_hash_name:"sha1",content_hash:e.sha1,proof_code:t,proof_version:"v1"},r=await this.axios.post("/adrive/v1.0/openFile/create",s),{data:o}=r;return o.exist&&"available"===o.status||o.rapid_upload?{need_upload:!1,data:{file_id:o.file_id}}:(await this.cacheFileId(o.file_id),await this.cacheUploadId(o.upload_id),await this.cachePartInfoList(o.part_info_list),this.queue_list=await this.cachePartInfoList(o.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 +1 @@
|
|
|
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.clearCache(),await this.postProgress({type:types_1.UploadProcessStep.upload_baidu,percent:1,host_type:types_1.HostType.BAIDU}),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
|
|
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.clearCache(),await this.postProgress({type:types_1.UploadProcessStep.upload_baidu,percent:1,host_type:types_1.HostType.BAIDU}),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;
|
|
@@ -10,7 +10,6 @@ export declare class BaseUploader {
|
|
|
10
10
|
protected chunk_size: number;
|
|
11
11
|
protected group_list: CompleteGroupList;
|
|
12
12
|
protected host_type: HostType;
|
|
13
|
-
protected controller: AbortController;
|
|
14
13
|
protected is_stop: boolean;
|
|
15
14
|
db: LowType<{
|
|
16
15
|
group_list?: CompleteGroupList;
|
|
@@ -19,12 +18,10 @@ export declare class BaseUploader {
|
|
|
19
18
|
ali_upload_id?: string;
|
|
20
19
|
ali_file_id?: string;
|
|
21
20
|
ali_part_info_list?: {
|
|
21
|
+
part_number: number;
|
|
22
|
+
upload_url: string;
|
|
22
23
|
expired_at: number;
|
|
23
|
-
|
|
24
|
-
part_number: number;
|
|
25
|
-
upload_url: string;
|
|
26
|
-
}[];
|
|
27
|
-
};
|
|
24
|
+
}[];
|
|
28
25
|
}>;
|
|
29
26
|
protected sdk: Sdk;
|
|
30
27
|
protected baseParams: CommonPartUploaderParams;
|
|
@@ -1 +1 @@
|
|
|
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;
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soga/part-uploader",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.24",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -53,5 +53,5 @@
|
|
|
53
53
|
"form-data": "^4.0.2",
|
|
54
54
|
"fs-extra": "^11.2.0"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "a996cac62fa189a6a7e64f16ec247f4c04e07a4b"
|
|
57
57
|
}
|