waterbase
Version:
SDK for waterbase-server
138 lines (137 loc) • 4.79 kB
JavaScript
;
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _client;
const form_data_1 = __importDefault(require("form-data"));
const path_1 = __importDefault(require("path"));
class File {
constructor(client, path) {
this.path = '';
_client.set(this, void 0);
__classPrivateFieldSet(this, _client, client);
this.path = path;
}
/**
* Delete the file
*
* @return Promise<any>
*/
delete() {
return new Promise((res, rej) => {
__classPrivateFieldGet(this, _client).call('delete', '/storage/', {
'content-type': 'application/json',
}, { path: this.path })
.then((resp) => {
if (resp.success) {
res(resp);
}
else {
rej(new Error("Wasn't able to delete the file"));
}
})
.catch(rej);
});
}
/**
* Gets a download url for the file
*
* @return string
*/
getDownloadUrl() {
return new Promise((res, rej) => {
__classPrivateFieldGet(this, _client).call('post', '/storage/file', {
'content-type': 'application/json',
}, { path: this.path })
.then((resp) => {
if (resp.success) {
const { path } = resp.file;
res(`${__classPrivateFieldGet(this, _client).endpoint}/${path}`);
}
else {
rej(new Error("Wasn't able to get a download url"));
}
})
.catch(rej);
});
}
/**
* Uploads the param to the file path
*
* @param file Buffer
*
* @return Promise<object>
*/
upload(file) {
return new Promise((res, rej) => {
const form = new form_data_1.default();
form.append('path', this.path);
form.append('file', file, path_1.default.basename(this.path));
// Web or node
if (typeof window !== 'undefined') {
__classPrivateFieldGet(this, _client).call('post', '/storage/', {
'content-type': 'multipart/form-data;',
}, form)
.then((data) => {
res(data);
})
.catch(rej);
}
else {
__classPrivateFieldGet(this, _client).call('post', '/storage/', Object.assign({ 'content-type': 'multipart/form-data;' }, form.getHeaders()), form.getBuffer())
.then((data) => {
res(data);
})
.catch(rej);
}
});
}
/**
* Updates the file with a new path or just the file
*
* @param file Buffer
*
* @param newPath? string
*
* @return Promise<object>
*/
update(file, newPath) {
return new Promise((res, rej) => {
const form = new form_data_1.default();
form.append('oldPath', this.path);
form.append('path', newPath || this.path);
form.append('file', file, path_1.default.basename(this.path));
if (typeof window !== 'undefined') {
__classPrivateFieldGet(this, _client).call('put', '/storage/', {
'content-type': 'multipart/form-data;',
}, form)
.then((data) => {
res(data);
})
.catch(rej);
}
else {
__classPrivateFieldGet(this, _client).call('put', '/storage/', Object.assign({ 'content-type': 'multipart/form-data;' }, form.getHeaders()), form.getBuffer())
.then((data) => {
res(data);
})
.catch(rej);
}
});
}
}
_client = new WeakMap();
module.exports = File;