@soga/part-uploader 0.2.15 → 0.2.22

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"),UPLOAD_ID="upload-id",FILE_ID="file-id",PART_INFO_LIST="part-info-list";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:e}=await this.sdk.getAliHostAuthData({host_id:this.params.host_id,refresh:!1});return t.headers.Authorization=`Bearer ${e}`,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(){const t=UPLOAD_ID,e=await this.db.get(t);if(e)return e}async cacheUploadId(t){const e=UPLOAD_ID;await this.db.put(e,t)}async clearCache(){await this.db.del(UPLOAD_ID),await this.db.del(FILE_ID),await this.db.del(this.GROUP_LIST_KEY),await this.db.del(PART_INFO_LIST)}async getFileId(){const t=FILE_ID,e=await this.db.get(t);if(e)return e}async cacheFileId(t){const e=FILE_ID;await this.db.put(e,t)}async getPartInfoList(){const t=PART_INFO_LIST,e=await this.db.get(t);if(e){const{expired_at:t,part_info_list:i}=e;return Date.now()<t?{state:!0,expired_at:t,data:i}:{state:!1,expired_at:t,data:i}}}async cachePartInfoList(t){const e=PART_INFO_LIST;await this.db.put(e,{expired_at:this.getExpiredAt(),part_info_list:t})}async getProofCode(){const{access_token:t}=await this.sdk.getAliHostAuthData({host_id:this.params.host_id,refresh:!1}),{part:e}=this.params,i=e.size,s=(0,crypto_1.createHash)("md5").update(t).digest("hex").slice(0,16),a=BigInt(`0x${s}`),r=Number(a%BigInt(i)),o=r,d=Math.min(i,r+8),_=e.start+o,n=e.start+d;return(await(0,utils_1.getFileBufferSlice)(e.path,_,n-1)).toString("base64")}}exports.AliHelper=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.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:e}=await this.sdk.getAliHostAuthData({host_id:this.params.host_id,refresh:!1});return t.headers.Authorization=`Bearer ${e}`,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(){const t=this.db.data.ali_part_info_list;if(t){const{expired_at:e,data:a}=t;return Date.now()<e?{state:!0,expired_at:e,data:a}:{state:!1,expired_at:e,data:a}}}async cachePartInfoList(t){this.db.data.ali_part_info_list={expired_at:this.getExpiredAt(),data:t}}async getProofCode(){const{access_token:t}=await this.sdk.getAliHostAuthData({host_id:this.params.host_id,refresh:!1}),{part:e}=this.params,a=e.size,i=(0,crypto_1.createHash)("md5").update(t).digest("hex").slice(0,16),s=BigInt(`0x${i}`),r=Number(s%BigInt(a)),d=r,o=Math.min(a,r+8),_=e.start+d,n=e.start+o;return(await(0,utils_1.getFileBufferSlice)(e.path,_,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_expired_at;queue_list;async start(){try{await this.init();const t=await this.getUploadId();if(t)await this.correctUploaded(t);else{const{need_upload:t,data:e}=await this.precreate();if(!t)return await this.clearCache(),await this.postProgress({type:types_1.UploadProcessStep.upload_ali,percent:1,host_type:types_1.HostType.ALI}),{...e,drive_id:this.drive_id}}await this.uploadChunks();const e=await this.postcreate();return await this.clearCache(),await this.postProgress({type:types_1.UploadProcessStep.upload_ali,percent:1,host_type:types_1.HostType.ALI}),{...e,drive_id:this.drive_id}}catch(t){if("ManualStopError"!=t.name&&"CanceledError"!=t.name)throw t}finally{this.db&&await this.db.close()}}async postcreate(){const t=await this.getFileId(),e=await this.getUploadId(),a=await this.axios.post("/adrive/v1.0/openFile/complete",{drive_id:this.drive_id,file_id:t,upload_id:e},{signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:16300,message:t.message,stack:t.stack})}));if(a)return{file_id:a.data.file_id}}async uploadChunks(){this.queue_list;const t=async()=>{if(this.queue_list.length){await this.autoRefreshUploadUrl();const e=this.queue_list.shift();await this.uploadOneChunk(e);const a=Math.min(e.part_number*this.chunk_size,this.params.part.size)/this.params.part.size;await this.postProgress({type:types_1.UploadProcessStep.upload_ali,percent:.97*a,host_type:types_1.HostType.ALI}),await t()}};await t()}async uploadOneChunk({upload_url:t,part_number:e}){const a=e-1,i=this.group_list[a],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,signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:16200,message:t.message,stack:t.stack})}))}async precreate(){const t=await this.getProofCode(),e=this.group_list.map(((t,e)=>({part_number:e+1}))),{part:a}=this.params,i=`dpan_p_${a.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:e,size:a.size,content_hash_name:"sha1",content_hash:a.sha1,proof_code:t,proof_version:"v1"},r=await this.axios.post("/adrive/v1.0/openFile/create",s,{signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:16100,message:t.message,stack:t.stack})}));if(!r)return;const{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=o.part_info_list,this.queue_expired_at=this.getExpiredAt(),{need_upload:!0})}async correctUploaded(t){const e=await this.getFileId(),a=await this.axios.post("/adrive/v1.0/openFile/listUploadedParts",{drive_id:this.drive_id,upload_id:t,file_id:e},{signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:16110,message:t.message,stack:t.stack})}));if(!a)return;const i=a.data.uploaded_parts.map((t=>t.part_number)),s=await this.getPartInfoList();if(s){const t=s.data.filter((t=>!i.includes(t.part_number)));this.queue_expired_at=s.expired_at,this.queue_list=t,await this.autoRefreshUploadUrl()}}async autoRefreshUploadUrl(){const{queue_expired_at:t,queue_list:e}=this;if(t>Date.now())return;const a=e.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,{signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:16120,message:t.message,stack:t.stack})}));s&&(this.queue_expired_at=this.getExpiredAt(),this.queue_list=s.data.part_info_list,await this.cachePartInfoList(s.data.part_info_list))}}exports.AliUploader=AliUploader;
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_expired_at;queue_list;async start(){try{await this.init();const t=await this.getUploadId();if(t)await this.correctUploaded(t);else{const{need_upload:t,data:e}=await this.precreate();if(!t)return await this.clearCache(),await this.postProgress({type:types_1.UploadProcessStep.upload_ali,percent:1,host_type:types_1.HostType.ALI}),{...e,drive_id:this.drive_id}}await this.uploadChunks();const e=await this.postcreate();return await this.clearCache(),await this.postProgress({type:types_1.UploadProcessStep.upload_ali,percent:1,host_type:types_1.HostType.ALI}),{...e,drive_id:this.drive_id}}catch(t){if("ManualStopError"!=t.name&&"CanceledError"!=t.name)throw t}}async postcreate(){const t=await this.getFileId(),e=await this.getUploadId(),a=await this.axios.post("/adrive/v1.0/openFile/complete",{drive_id:this.drive_id,file_id:t,upload_id:e},{signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:16300,message:t.message,stack:t.stack})}));if(a)return{file_id:a.data.file_id}}async uploadChunks(){this.queue_list;const t=async()=>{if(this.queue_list.length){await this.autoRefreshUploadUrl();const e=this.queue_list.shift();await this.uploadOneChunk(e);const a=Math.min(e.part_number*this.chunk_size,this.params.part.size)/this.params.part.size;await this.postProgress({type:types_1.UploadProcessStep.upload_ali,percent:.97*a,host_type:types_1.HostType.ALI}),await t()}};await t()}async uploadOneChunk({upload_url:t,part_number:e}){const a=e-1,i=this.group_list[a],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,signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:16200,message:t.message,stack:t.stack})}))}async precreate(){const t=await this.getProofCode(),e=this.group_list.map(((t,e)=>({part_number:e+1}))),{part:a}=this.params,i=`dpan_p_${a.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:e,size:a.size,content_hash_name:"sha1",content_hash:a.sha1,proof_code:t,proof_version:"v1"},r=await this.axios.post("/adrive/v1.0/openFile/create",s,{signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:16100,message:t.message,stack:t.stack})}));if(!r)return;const{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=o.part_info_list,this.queue_expired_at=this.getExpiredAt(),{need_upload:!0})}async correctUploaded(t){const e=await this.getFileId(),a=await this.axios.post("/adrive/v1.0/openFile/listUploadedParts",{drive_id:this.drive_id,upload_id:t,file_id:e},{signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:16110,message:t.message,stack:t.stack})}));if(!a)return;const i=a.data.uploaded_parts.map((t=>t.part_number)),s=await this.getPartInfoList();if(s){const t=s.data.filter((t=>!i.includes(t.part_number)));this.queue_expired_at=s.expired_at,this.queue_list=t,await this.autoRefreshUploadUrl()}}async autoRefreshUploadUrl(){const{queue_expired_at:t,queue_list:e}=this;if(t>Date.now())return;const a=e.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,{signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:16120,message:t.message,stack:t.stack})}));s&&(this.queue_expired_at=this.getExpiredAt(),this.queue_list=s.data.part_info_list,await this.cachePartInfoList(s.data.part_info_list))}}exports.AliUploader=AliUploader;
@@ -1 +1 @@
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"),UPLOAD_ID="upload-id",MD5_LIST="md5-list";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=MD5_LIST,s=await this.db.get(t);if(s)return void(this.md5_list=s);const e=this.group_list,a=[];for(const t of e){const{path:s}=this.params.part,e=await(0,utils_1.calculateMd5)({file:s,start:t.start,end:t.end});a.push(e)}await this.db.put(t,a),this.md5_list=a}getSearchParams(t){Object.keys(t).forEach((s=>{void 0===t[s]&&delete t[s]}));return new URLSearchParams(t).toString()}async getUploadId(){const t=UPLOAD_ID,s=await this.db.get(t);if(s)return s}async cacheUploadId(t){const s=UPLOAD_ID;await this.db.put(s,t)}async clearCache(){await this.db.del(UPLOAD_ID),await this.db.del(this.GROUP_LIST_KEY),await this.db.del(MD5_LIST)}}exports.BaiduHelper=BaiduHelper;
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 +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(){try{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}finally{this.db&&await this.db.close()}}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,{signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:15300,message:t.message,stack:t.stack})}));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},signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:15100,message:t.message,stack:t.stack})}));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:3e5,signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:15200,message:t.message,stack:t.stack})}));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
+ "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,{signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:15300,message:t.message,stack:t.stack})}));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},signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:15100,message:t.message,stack:t.stack})}));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:3e5,signal:this.controller.signal}).catch((async t=>{if("CanceledError"==t.name)throw t;await this.throwError({code:15200,message:t.message,stack:t.stack})}));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,10 +1,9 @@
1
- import { Level } from 'level';
2
1
  import { CommonPartUploaderParams } from '../types/main';
3
2
  import { HostType, UploadProcessStep, UploadWorkerMessage } from '@soga/types';
4
3
  import { Sdk } from '@soga/sdk';
5
4
  import { CompleteGroupList } from '../utils/chunk';
6
5
  import { MessagePort } from 'node:worker_threads';
7
- type LevelDataType = string | number | boolean | object | null;
6
+ import { LowType } from '@soga/bridge';
8
7
  export declare class BaseUploader {
9
8
  protected port: MessagePort | null;
10
9
  protected GROUP_LIST_KEY: string;
@@ -13,13 +12,26 @@ export declare class BaseUploader {
13
12
  protected host_type: HostType;
14
13
  protected controller: AbortController;
15
14
  protected is_stop: boolean;
16
- protected db: Level<string, LevelDataType>;
15
+ db: LowType<{
16
+ group_list?: CompleteGroupList;
17
+ baidu_upload_id?: string;
18
+ baidu_md5_list?: string[];
19
+ ali_upload_id?: string;
20
+ ali_file_id?: string;
21
+ ali_part_info_list?: {
22
+ expired_at: number;
23
+ data: {
24
+ part_number: number;
25
+ upload_url: string;
26
+ }[];
27
+ };
28
+ }>;
17
29
  protected sdk: Sdk;
18
30
  protected baseParams: CommonPartUploaderParams;
19
31
  constructor(params: CommonPartUploaderParams, host_type: HostType);
20
32
  protected init(): Promise<void>;
21
33
  protected initGroupList(): Promise<void>;
22
- private initLevel;
34
+ private initDb;
23
35
  throwError(error: {
24
36
  code: number;
25
37
  message: string;
@@ -32,4 +44,3 @@ export declare class BaseUploader {
32
44
  }): Promise<void>;
33
45
  protected postMessage(data: UploadWorkerMessage): Promise<void>;
34
46
  }
35
- export {};
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BaseUploader=void 0;const utils_1=require("@soga/utils"),level_1=require("level"),path_1=require("path"),sdk_1=require("@soga/sdk"),chunk_1=require("../utils/chunk");class BaseUploader{port=null;GROUP_LIST_KEY="group_list";chunk_size=4194304;group_list=[];host_type;controller=new AbortController;is_stop=!1;db;sdk;baseParams;constructor(s,t){this.host_type=t,this.baseParams=s,this.port=s.port??null}async init(){this.initLevel(),this.sdk=(0,sdk_1.getSdk)(this.baseParams.sdk_domain),await this.initGroupList()}async initGroupList(){const s=await this.db.get(this.GROUP_LIST_KEY);if(s)return void(this.group_list=s);const t=(0,chunk_1.groupChunk)(this.baseParams.part,this.chunk_size);this.group_list=t,await this.db.put(this.GROUP_LIST_KEY,t)}initLevel(){const s=`upload_part_${this.host_type}_${this.baseParams.part_id}`,t=(0,path_1.resolve)(this.baseParams.output_root,s),e=new level_1.Level(t,{valueEncoding:"json"});this.db=e}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;
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;controller=new AbortController;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.15",
3
+ "version": "0.2.22",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -45,15 +45,13 @@
45
45
  "author": "",
46
46
  "license": "ISC",
47
47
  "dependencies": {
48
- "@soga/sdk": "file:../base-sdk",
49
- "@soga/types": "file:../base-types",
50
- "@soga/utils": "file:../base-utils",
48
+ "@soga/bridge": "^0.2.22",
49
+ "@soga/sdk": "^0.2.22",
50
+ "@soga/types": "^0.2.22",
51
+ "@soga/utils": "^0.2.22",
51
52
  "axios": "^1.7.9",
52
53
  "form-data": "^4.0.2",
53
54
  "fs-extra": "^11.2.0"
54
55
  },
55
- "peerDependencies": {
56
- "level": "*"
57
- },
58
- "gitHead": "2592173a0ca38f6ff55cbbc8e29d7b1ac4bb4072"
56
+ "gitHead": "6a9ab21995f575fd1325a2b5fa7eea4fd50564db"
59
57
  }