UNPKG

pcs-cli

Version:

Baidu Personal Cloud Storage's Command Line Interface.

2 lines (1 loc) 2.69 kB
import{_ as e}from"../_tslib-NbJOzQvB.js";import t from"https";import o from"fs";import{dirname as a}from"path";import n from"progress";import"../utils/axios.js";import s from"axios";const r={oauthDevice:e=>s.get("https://openapi.baidu.com/oauth/2.0/device/code",{params:{client_id:e,response_type:"device_code",scope:"basic,netdisk"}}),oauthToken:(e,t,o)=>s.get("https://openapi.baidu.com/oauth/2.0/token",{params:{client_id:e,client_secret:t,code:o,grant_type:"device_token",scope:"basic,netdisk"}}),refreshToken:(e,t,o)=>s.get("https://openapi.baidu.com/oauth/2.0/token",{params:{client_id:e,client_secret:t,refresh_token:o,grant_type:"refresh_token"}}),quotaInfo:e=>s.get("/pcs/quota",{params:{method:"info",access_token:e}}),getMeta:(e,t)=>s.get("/pcs/file",{params:{method:"meta",access_token:e,path:t}}),listFile:(e,t)=>s.get("/pcs/file",{params:{method:"list",access_token:e,path:t}}),download(t,r,i){return e(this,void 0,void 0,(function*(){o.mkdirSync(a(i),{recursive:!0});const e=o.createWriteStream(i),{data:p,headers:c}=yield s.get("/pcs/file",{params:{method:"download",access_token:t,path:r},responseType:"stream",responseParser:e=>e}),d=c["content-length"],m=new n(" downloading [:bar] :rate/bps :percent :etas",{complete:"=",incomplete:" ",width:40,total:parseInt(d,10)});return p.on("data",(e=>m.tick(e.length))),p.pipe(e),new Promise(((t,o)=>{e.on("finish",t),e.on("error",o)}))}))},upload2(a,s,r,i="overwrite",p){return e(this,void 0,void 0,(function*(){let e=`/rest/2.0/pcs/file?method=upload&access_token=${a}&path=${encodeURIComponent(r)}&ondup=${i}`;p&&(e=`${e}&type=${p}`);const c=o.statSync(s),d=Math.random().toString(16),m=`--${d}\r\nContent-Type: text/plain\r\nContent-Disposition: form-data; name="file"; filename="${r}"\r\n\r\n`,h=`\r\n--${d}--`,l=Buffer.byteLength(m)+Buffer.byteLength(h)+c.size,f=new n("[:bar] :rate/bps :percent :etas",{complete:"=",incomplete:" ",width:40,total:c.size});return new Promise(((a,n)=>{const r=t.request({hostname:"pcs.baidu.com",method:"POST",path:e},(e=>{e.on("data",(e=>{a(JSON.parse(e))})),e.on("end",(()=>{})),e.on("error",(e=>{n(e)}))}));r.setHeader("Content-Type",`multipart/form-data; boundary=${d}`),r.setHeader("Content-Length",l),r.write(m);const i=o.createReadStream(s);i.pipe(r,{end:!1}),i.on("end",(()=>{r.end(h)})),i.on("data",(e=>{f.tick(e.length)}))}))}))},delete:(e,t)=>s.get("/pcs/file",{params:{method:"delete",access_token:e,path:t}}),fetch:(e,t,o)=>s.get("/pcs/services/cloud_dl",{params:{method:"add_task",access_token:e,save_path:o,source_url:t}}),createSuperFile:(e,t,o)=>s.get("/pcs/file",{params:{method:"createsuperfile",access_token:e,path:t,param:JSON.stringify(o)}})};export{r as default};