@soga/part-uploader 1.3.5 → 1.3.7

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 CHANGED
@@ -80,7 +80,7 @@ declare class BaiduHelper extends BaseUploader {
80
80
  private initAxios;
81
81
  protected initMd5List(): Promise<void>;
82
82
  protected getSearchParams(payload: Record<string, string>): string;
83
- protected getUploadId(): Promise<any>;
83
+ protected getUploadId(): Promise<string>;
84
84
  protected cacheUploadId(upload_id: string): Promise<void>;
85
85
  protected clearCache(): Promise<void>;
86
86
  }
@@ -122,16 +122,24 @@ declare class AliHelper extends BaseUploader {
122
122
  protected getExpiredAt(): number;
123
123
  private initAxios;
124
124
  protected init(): Promise<void>;
125
- protected getUploadId(): Promise<any>;
125
+ protected getUploadId(): Promise<string>;
126
126
  protected cacheUploadId(upload_id: string): Promise<void>;
127
127
  protected clearCache(): Promise<void>;
128
- protected getFileId(): Promise<any>;
128
+ protected getFileId(): Promise<string>;
129
129
  protected cacheFileId(file_id: string): Promise<void>;
130
- protected getPartInfoList(): Promise<any>;
130
+ protected getPartInfoList(): Promise<{
131
+ part_number: number;
132
+ upload_url: string;
133
+ expired_at: number;
134
+ }[]>;
131
135
  protected cachePartInfoList(part_info_list: {
132
136
  part_number: number;
133
137
  upload_url: string;
134
- }[]): Promise<any>;
138
+ }[]): Promise<{
139
+ part_number: number;
140
+ upload_url: string;
141
+ expired_at: number;
142
+ }[]>;
135
143
  protected getProofCode(access_token: string): Promise<string>;
136
144
  }
137
145
 
package/dist/main.d.ts CHANGED
@@ -80,7 +80,7 @@ declare class BaiduHelper extends BaseUploader {
80
80
  private initAxios;
81
81
  protected initMd5List(): Promise<void>;
82
82
  protected getSearchParams(payload: Record<string, string>): string;
83
- protected getUploadId(): Promise<any>;
83
+ protected getUploadId(): Promise<string>;
84
84
  protected cacheUploadId(upload_id: string): Promise<void>;
85
85
  protected clearCache(): Promise<void>;
86
86
  }
@@ -122,16 +122,24 @@ declare class AliHelper extends BaseUploader {
122
122
  protected getExpiredAt(): number;
123
123
  private initAxios;
124
124
  protected init(): Promise<void>;
125
- protected getUploadId(): Promise<any>;
125
+ protected getUploadId(): Promise<string>;
126
126
  protected cacheUploadId(upload_id: string): Promise<void>;
127
127
  protected clearCache(): Promise<void>;
128
- protected getFileId(): Promise<any>;
128
+ protected getFileId(): Promise<string>;
129
129
  protected cacheFileId(file_id: string): Promise<void>;
130
- protected getPartInfoList(): Promise<any>;
130
+ protected getPartInfoList(): Promise<{
131
+ part_number: number;
132
+ upload_url: string;
133
+ expired_at: number;
134
+ }[]>;
131
135
  protected cachePartInfoList(part_info_list: {
132
136
  part_number: number;
133
137
  upload_url: string;
134
- }[]): Promise<any>;
138
+ }[]): Promise<{
139
+ part_number: number;
140
+ upload_url: string;
141
+ expired_at: number;
142
+ }[]>;
135
143
  protected getProofCode(access_token: string): Promise<string>;
136
144
  }
137
145
 
package/dist/main.js CHANGED
@@ -1 +1 @@
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()};
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,{});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 CHANGED
@@ -1 +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};
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,{});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": "1.3.5",
3
+ "version": "1.3.7",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -17,12 +17,12 @@
17
17
  "axios": "^1.9.0",
18
18
  "form-data": "^4.0.2",
19
19
  "fs-extra": "^11.3.2",
20
- "@soga/types": "1.0.1",
21
- "@soga/node-types": "1.0.0",
22
- "@soga/lowdb": "1.0.2",
23
- "@soga/error": "1.0.8",
24
- "@soga/utils": "1.0.0",
25
- "@soga/sdk": "1.0.0"
20
+ "@soga/types": "1.3.7",
21
+ "@soga/lowdb": "1.3.7",
22
+ "@soga/error": "1.3.7",
23
+ "@soga/sdk": "1.3.7",
24
+ "@soga/utils": "1.3.7",
25
+ "@soga/node-types": "1.3.7"
26
26
  },
27
27
  "devDependencies": {
28
28
  "rimraf": "^6.0.1",
@@ -30,7 +30,7 @@
30
30
  "tsup": "^8.5.0",
31
31
  "typescript": "^5.8.3",
32
32
  "@types/node": "^24.5.2",
33
- "@soga/typescript-config": "1.0.0"
33
+ "@soga/typescript-config": "1.3.7"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "rimraf dist && tsup src/main.ts --format cjs,esm --dts --minify terser"