pixeldrainjs
Version:
A NodeJS Wrapper for the pixeldrain.com API
129 lines • 5.2 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PixeldrainService = void 0;
const node_fetch_1 = require("node-fetch");
const fs = require("fs");
const superchargedFs = require('@supercharge/fs');
const https = require("https");
const pixeldrainapierror_1 = require("../components/errors/pixeldrainapierror");
class PixeldrainService {
//private readonly authorizationHeader =
constructor(APIKey = "") {
this.BASE_URL = 'https://pixeldrain.com/api';
this.APIKey = APIKey;
this.authorizationHeader = this.APIKey == "" ? {} : { "Authorization": "Basic " + Buffer.from(":" + this.APIKey).toString('base64') };
}
/**
* uploadFile
*/
uploadFile(file) {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
let readStream = fs.createReadStream(file.path);
readStream.on("error", reject);
//TODO: Figure out what to do with file Name
file.name = yield superchargedFs.basename(file.path);
(0, node_fetch_1.default)(`${this.BASE_URL}/file/${file.name}`, {
method: 'PUT',
headers: file.anonymous ? undefined : this.authorizationHeader,
body: readStream
})
.then((res) => __awaiter(this, void 0, void 0, function* () {
if (res.status > 400) {
let json = yield res.json();
reject(new pixeldrainapierror_1.PixeldrainAPIError('Error occured whilst uploading file', json.value, file.name));
}
return res.json();
})).then(function (json) {
resolve(json.id);
})
.catch(reject);
}));
}
/**
* deleteFile
*/
deleteFile(fileID) {
return new Promise((resolve, reject) => {
(0, node_fetch_1.default)(`${this.BASE_URL}/file/${fileID}`, {
method: 'delete',
headers: this.authorizationHeader
})
.then((res) => __awaiter(this, void 0, void 0, function* () {
if (res.status > 400) {
let json = yield res.json();
return reject(new pixeldrainapierror_1.PixeldrainAPIError("Error occured whilst deleting a file.", json.value, fileID));
}
resolve();
}))
.catch(reject);
});
}
/**
* fileInfo
*/
getFileInfo(fileID) {
return new Promise((resolve, reject) => {
(0, node_fetch_1.default)(`${this.BASE_URL}/file/${fileID}/info`, {
method: 'get',
headers: this.authorizationHeader
})
.then((res) => __awaiter(this, void 0, void 0, function* () {
if (res.status > 400) {
let json = yield res.json();
return reject(new pixeldrainapierror_1.PixeldrainAPIError('Error occured whilst obtaining file details.', json.value, fileID));
}
return res.json();
}))
.then((json) => {
resolve(json);
})
.catch(reject);
});
}
/**
* downloadFile
*/
downloadFile(path = "", id) {
return new Promise((resolve, reject) => {
this.getFileInfo(id).then((file) => {
https.get(`${this.BASE_URL}/file/${id}`, function (response) {
return __awaiter(this, void 0, void 0, function* () {
if (path != "") {
yield superchargedFs.ensureDir(path);
}
const writeSteam = fs.createWriteStream(path == "" ? file.name : `${path}/${file.name}`);
writeSteam.on("error", reject);
response.pipe(writeSteam);
writeSteam.on("finish", () => {
writeSteam.close();
resolve();
});
});
});
});
});
}
/**
* createList
*/
createList(list) {
return new Promise((resolve, reject) => {
});
}
/**
* listInformation
*/
getListInformation(listID) {
}
}
exports.PixeldrainService = PixeldrainService;
//# sourceMappingURL=pixeldrainservice.js.map