UNPKG

@xjin-lab/pixnix

Version:

104 lines (103 loc) 4.81 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _a, _Pixnix_uploadWithStream, _Pixnix_postJson; Object.defineProperty(exports, "__esModule", { value: true }); exports.Pixnix = void 0; // pixnix.ts const axios_1 = __importDefault(require("axios")); const form_data_1 = __importDefault(require("form-data")); const stream_1 = require("stream"); const PRODUCTION = true; const BASE_URL = PRODUCTION ? "https://media.xjin.in" : "http://localhost:3003"; class Pixnix { // ------------------ PUBLIC METHODS ------------------ static async uploadImage(payload) { return __classPrivateFieldGet(this, _a, "m", _Pixnix_uploadWithStream).call(this, `${BASE_URL}/image/upload`, payload); } static async uploadVideo(payload) { return __classPrivateFieldGet(this, _a, "m", _Pixnix_uploadWithStream).call(this, `${BASE_URL}/video/upload`, payload); } static async uploadAudio(payload) { return __classPrivateFieldGet(this, _a, "m", _Pixnix_uploadWithStream).call(this, `${BASE_URL}/audio/upload`, payload); } static async uploadDocument(payload) { return __classPrivateFieldGet(this, _a, "m", _Pixnix_uploadWithStream).call(this, `${BASE_URL}/document/upload`, payload); } static async generateVideo(payload) { return __classPrivateFieldGet(this, _a, "m", _Pixnix_postJson).call(this, `${BASE_URL}/video/generate`, payload); } } exports.Pixnix = Pixnix; _a = Pixnix, _Pixnix_uploadWithStream = async function _Pixnix_uploadWithStream(url, payload) { var _b, _c, _d; try { if (!payload.file || !(payload.file instanceof stream_1.Readable)) { throw new Error("Invalid or missing file stream"); } if (!payload.filename) throw new Error("Filename is required"); if (!payload.relativePath) throw new Error("relativePath is required"); const form = new form_data_1.default(); form.append("file", payload.file, { filename: payload.filename, contentType: payload.mimetype || "application/octet-stream", }); form.append("relativePath", payload.relativePath); if (payload.metadata) form.append("metadata", JSON.stringify(payload.metadata)); if (payload.height) form.append("height", payload.height.toString()); if (payload.width) form.append("width", payload.width.toString()); if (payload.quality) form.append("quality", payload.quality.toString()); const response = await axios_1.default.post(url, form, { headers: form.getHeaders(), maxBodyLength: 100 * 1024 * 1024, // 100 MB limit timeout: 60000, // 60 seconds onUploadProgress: (evt) => { if (evt.total) console.log(`📤 ${payload.filename}: ${((evt.loaded / evt.total) * 100).toFixed(2)}%`); }, }); return { message: "Upload successful", data: response.data, status: response.status, }; } catch (err) { const message = ((_c = (_b = err.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.message) || err.message || "Upload failed"; console.error(`❌ [Pixnix] ${message}`); return { message, status: ((_d = err.response) === null || _d === void 0 ? void 0 : _d.status) || 500, error: true, }; } }, _Pixnix_postJson = async function _Pixnix_postJson(url, payload) { var _b, _c, _d; try { const response = await axios_1.default.post(url, payload, { timeout: 60000 }); return { message: "Request successful", data: response.data, status: response.status, }; } catch (err) { const message = ((_c = (_b = err.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.message) || err.message || "Request failed"; return { message, status: ((_d = err.response) === null || _d === void 0 ? void 0 : _d.status) || 500, error: true }; } };