Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as path from 'path'
  2. import axios, { AxiosRequestConfig } from 'axios'
  3. import { request } from 'http'
  4.  
  5.  
  6. export class YandexDisk{
  7.     private login: string
  8.     private password: string
  9.     private workDir: string
  10.  
  11.     constructor(login: string, password: string){
  12.         this.login = login;
  13.         this.password = password;
  14.         this.workDir = "/";
  15.     }
  16.  
  17.     public async createDir(path: string){
  18.         try {
  19.             const request = await this.sendRequest("MKCOL", path);
  20.             return true;
  21.         } catch {
  22.             return false;
  23.         }
  24.     }
  25.  
  26.     public async deleteFile(path: string){
  27.         try {
  28.             const request = await this.sendRequest("DELETE", path);
  29.             return true;
  30.         } catch(e) {
  31.             console.log(e);
  32.             return false;
  33.         }
  34.     }
  35.  
  36.     public async uploadFile(srcFile: any, targetPath: string){
  37.         try {
  38.             const request = await this.sendRequest("PUT", targetPath, {
  39.                 'Expect': '100-continue',
  40.                 'Content-Type': 'application/binary',
  41.                 'Transfer-Encoding': 'chunked',
  42.             }, srcFile);
  43.             return request
  44.         } catch(e){
  45.             console.log(e);
  46.         }
  47.     }
  48.  
  49.     public async sendRequest(method: any, path: string, headers: object={}, body: any=null){
  50.         const authToken = Buffer.from(`${this.login}:${this.password}`).toString('base64');
  51.         let options: AxiosRequestConfig = {
  52.             baseURL: 'https://webdav.yandex.ru:443',
  53.             method: method.toUpperCase(),
  54.             url: encodeURI(this.normalizePath(path)),
  55.             headers: {
  56.                 'Host': 'webdav.yandex.ru',
  57.                 'Accept': '*/*',
  58.                 'Authorization': `Basic ${authToken}`,
  59.             }
  60.         };
  61.  
  62.         if (body){
  63.             options.data = body
  64.         }
  65.  
  66.         for (const header of Object.keys(headers)){
  67.             Object.defineProperty(options.headers, header, {
  68.                 value: Object.getOwnPropertyDescriptor(headers, header)?.value,
  69.                 writable: true,
  70.                 configurable: true,
  71.                 enumerable: true});
  72.         }
  73.  
  74.         let request = await axios(options);
  75.         return request.data
  76.     }
  77.  
  78.     private normalizePath(fpath: string) {
  79.         return fpath.indexOf('/') == 0 ? fpath : path.join(this.workDir, fpath).replace(/\\/g, '/');
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement