@soga/part-uploader 0.2.26 → 0.2.33
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.
|
@@ -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: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{
|
|
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 +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{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
|
|
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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soga/part-uploader",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.33",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -45,13 +45,13 @@
|
|
|
45
45
|
"author": "",
|
|
46
46
|
"license": "ISC",
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@soga/bridge": "^0.2.
|
|
49
|
-
"@soga/sdk": "^0.2.
|
|
50
|
-
"@soga/types": "^0.2.
|
|
51
|
-
"@soga/utils": "^0.2.
|
|
48
|
+
"@soga/bridge": "^0.2.33",
|
|
49
|
+
"@soga/sdk": "^0.2.33",
|
|
50
|
+
"@soga/types": "^0.2.33",
|
|
51
|
+
"@soga/utils": "^0.2.33",
|
|
52
52
|
"axios": "^1.7.9",
|
|
53
53
|
"form-data": "^4.0.2",
|
|
54
54
|
"fs-extra": "^11.2.0"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "70d4fae5e473e2f40b7ced6ca434c7b29a527547"
|
|
57
57
|
}
|