UNPKG

@coko/server

Version:

Reusable server for use by Coko's projects

129 lines 4.37 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const uuid_1 = require("uuid"); const logger_1 = __importDefault(require("../../logger")); const base_model_1 = __importDefault(require("../base.model")); const useTransaction_1 = __importDefault(require("../useTransaction")); const types_1 = require("../_helpers/types"); // Type declaration const mimetype = { type: 'string', pattern: '^(application|audio|font|image|model|multipart|text|video)/[a-z0-9]+([-+.][a-z0-9]+)*$', // if you want to know why this is default, look at // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types default: 'application/octet-stream', }; const arrayOfStoredObjects = { type: 'array', items: { type: 'object', additionalProperties: false, required: ['type', 'key', 'mimetype', 'extension', 'size'], properties: { id: types_1.id, type: { type: 'string', enum: ['original', 'small', 'medium', 'full'] }, key: types_1.stringNotEmpty, mimetype, extension: types_1.stringNotEmpty, imageMetadata: { type: ['object', 'null'], required: ['height', 'width'], additionalProperties: false, properties: { id: types_1.id, density: types_1.integerPositive, height: types_1.integerPositive, space: types_1.stringNotEmpty, width: types_1.integerPositive, }, }, size: types_1.integerPositive, }, }, }; class File extends base_model_1.default { alt; caption; meta; name; objectId; storedObjects; referenceId; tags; uploadStatus; constructor() { super(); this.type = 'file'; } static get tableName() { return 'files'; } static get schema() { return { type: 'object', required: ['name', 'storedObjects'], properties: { alt: types_1.stringNullable, caption: types_1.stringNullable, meta: types_1.objectDefaultEmpty, name: types_1.stringNotEmpty, objectId: types_1.idNullable, storedObjects: arrayOfStoredObjects, referenceId: types_1.idNullable, tags: types_1.arrayOfStrings, uploadStatus: types_1.stringNullable, }, }; } ensureIds() { if (this.storedObjects) { this.storedObjects.forEach((storedObject, index) => { if (!storedObject.id) { this.storedObjects[index].id = (0, uuid_1.v4)(); } if (storedObject.imageMetadata) { if (!storedObject.imageMetadata.id) { this.storedObjects[index].imageMetadata.id = (0, uuid_1.v4)(); } } }); } } $beforeInsert() { super.$beforeInsert(); this.ensureIds(); } $beforeUpdate() { super.$beforeUpdate(); this.ensureIds(); } static async getEntityFiles(objectId, options = {}) { try { return (0, useTransaction_1.default)(async (tr) => { const { result: files } = await File.find({ objectId }, { trx: tr }); return files; }, { trx: options.trx, passedTrxOnly: true }); } catch (e) { logger_1.default.error('File model: getEntityFiles failed', e); throw new Error(`File model: Cannot get files for entity with id ${objectId}`); } } getStoredObjectBasedOnType(type) { try { const found = this.storedObjects.find(o => o.type === type); if (!found) { throw new Error('Unknown type of stored object provided'); } return found; } catch (e) { throw new Error(e.message); } } } exports.default = File; //# sourceMappingURL=file.model.js.map