UNPKG

seaweedfs-client

Version:

Nodejs Seaweedfs Client

146 lines (143 loc) 4.58 kB
import axios from 'axios'; import FormData from 'form-data'; import { readFile } from 'fs'; import { sign } from 'jsonwebtoken'; import { join } from 'path'; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; class Filter { constructor(params) { this.baseUrl = params.baseUrl; this.jwtSecretKey = params.jwtSecretKey; this.baseRemoteFilePath = params.baseRemoteFilePath || ""; } genJwtToken() { if (this.jwtSecretKey) { return sign({}, this.jwtSecretKey, { expiresIn: "1h" }); } } prepareFileBody(file) { return __async(this, null, function* () { return new Promise((resolve, reject) => { if (file instanceof Buffer) { return resolve(file); } readFile(file, {}, (err, data) => { if (err) { return reject(err); } resolve(data); }); }); }); } _request(method, urlPath, options) { return __async(this, null, function* () { const targetPath = join(this.baseRemoteFilePath, urlPath); const url = new URL(targetPath, this.baseUrl); return axios({ method, url: url.href, data: options == null ? void 0 : options.body, params: options == null ? void 0 : options.params, headers: __spreadProps(__spreadValues({}, options == null ? void 0 : options.headers), { Authorization: this.jwtSecretKey ? `Bearer ${this.genJwtToken()}` : "" }), maxContentLength: Infinity, maxBodyLength: Infinity }); }); } uploadFile(file, saveAs, options) { return __async(this, null, function* () { const fileBody = yield this.prepareFileBody(file); const formData = new FormData(); formData.append("file", fileBody); const savePath = join(this.baseRemoteFilePath, saveAs); const url = new URL(savePath, this.baseUrl); return this._request("post", saveAs, { body: formData, params: (options == null ? void 0 : options.params) || {}, headers: __spreadValues({}, formData.getHeaders()) }).then((res) => { return __spreadValues({ url: url.href, path: url.pathname }, res.data); }).catch((error) => { throw error; }); }); } createDir(dirPath) { return __async(this, null, function* () { return this._request("post", dirPath); }); } _delete(targetPath, options) { const params = new URLSearchParams(); if (options == null ? void 0 : options.recursive) { params.append("recursive", "true"); } if (options == null ? void 0 : options.ignoreRecursiveError) { params.append("ignoreRecursiveError", "true"); } if (options == null ? void 0 : options.skipChunkDeletion) { params.append("skipChunkDeletion", "true"); } return this._request("delete", targetPath, { params }); } deleteFile(filePath) { return __async(this, null, function* () { return this._delete(filePath); }); } deleteDir(dirPath) { return __async(this, null, function* () { return this._delete(dirPath, { recursive: true }); }); } } export { Filter }; //# sourceMappingURL=index.module.js.map