@wepublish/api
Version:
API core for we.publish.
111 lines • 4.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KarmaMediaAdapter = exports.MediaServerError = void 0;
const tslib_1 = require("tslib");
const form_data_1 = tslib_1.__importDefault(require("form-data"));
const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
class MediaServerError extends Error {
constructor(message) {
super(`Received error from media server. Message: ${message}`);
}
}
exports.MediaServerError = MediaServerError;
class KarmaMediaAdapter {
constructor(url, token, internalURL = url) {
this.url = url;
this.token = token;
this.internalURL = internalURL;
}
_uploadImage(form) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
// The form-data module reports a known length for the stream returned by createReadStream,
// which is wrong, override it and always set it to false.
// Related issue: https://github.com/form-data/form-data/issues/394
form.hasKnownLength = () => false;
const response = yield (0, node_fetch_1.default)(this.internalURL, {
method: 'POST',
headers: { authorization: `Bearer ${this.token}` },
body: form
});
const json = yield response.json();
if (response.status !== 200) {
throw new MediaServerError(response.statusText);
}
const { id, filename, fileSize, extension, mimeType, format, width, height } = json;
return {
id,
filename,
fileSize,
extension,
mimeType,
format,
width,
height
};
});
}
uploadImage(fileUpload) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const form = new form_data_1.default();
const { filename: inputFilename, mimetype, createReadStream } = yield fileUpload;
form.append('file', createReadStream(), { filename: inputFilename, contentType: mimetype });
return this._uploadImage(form);
});
}
uploadImageFromArrayBuffer(arrayBufferUpload) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const form = new form_data_1.default();
const { filename: inputFilename, mimetype, arrayBuffer } = yield arrayBufferUpload;
form.append('file', arrayBuffer, { filename: inputFilename, contentType: mimetype });
return this._uploadImage(form);
});
}
deleteImage(id) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const response = yield (0, node_fetch_1.default)(`${this.internalURL}${id}`, {
method: 'DELETE',
headers: { authorization: `Bearer ${this.token}` }
});
if (response.status !== 204) {
throw new MediaServerError(response.statusText);
}
return true;
});
}
getImageURL({ id, filename, extension, focalPoint }, transformation) {
var _a, _b;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
filename = filename || 'untitled';
if (transformation) {
const { width, height, rotation, output, quality } = transformation;
const fullFilename = encodeURIComponent(`${filename}${output ? `.${output}` : extension}`);
const transformations = [];
if (width)
transformations.push(`w_${width}`);
if (height)
transformations.push(`h_${height}`);
if (rotation)
transformations.push(`r_${rotation}`);
if (output)
transformations.push(`o_${output}`);
if (quality)
transformations.push(`q_${quality}`);
if (focalPoint && (width || height)) {
transformations.push(`f_${(_a = focalPoint.x) === null || _a === void 0 ? void 0 : _a.toFixed(3)}:${(_b = focalPoint.y) === null || _b === void 0 ? void 0 : _b.toFixed(3)}`);
}
if (transformations.length > 0) {
return `${this.url}${id}/t/${transformations.join(',')}/${fullFilename}`;
}
else {
return `${this.url}${id}/${fullFilename}`;
}
}
else {
const fullFilename = encodeURIComponent(`${filename}${extension}`);
return `${this.url}${id}/${fullFilename}`;
}
});
}
}
exports.KarmaMediaAdapter = KarmaMediaAdapter;
//# sourceMappingURL=karmaMediaAdapter.js.map