UNPKG

@jrmc/adonis-attachment

Version:

Turn any field on your Lucid model to an attachment data type

141 lines (140 loc) 3.82 kB
/** * @jrmc/adonis-attachment * * @license MIT * @copyright Jeremy Chaufourier <jeremy@chaufourier.fr> */ import string from '@adonisjs/core/helpers/string'; import path from 'node:path'; import { cuid } from '@adonisjs/core/helpers'; import { defaultOptionsDecorator } from '../utils/default_values.js'; import { extractPathParameters } from '../utils/helpers.js'; export class AttachmentBase { drive; input; #name; #folder; size; extname; mimeType; meta; originalPath; url; options; constructor(drive, attributes, input) { this.input = input; this.size = attributes.size; this.meta = attributes.meta; this.extname = attributes.extname; this.mimeType = attributes.mimeType; this.originalPath = attributes.path; this.#folder = attributes.folder; this.setOptions({ folder: attributes.folder }); if (attributes.name) { this.#name = attributes.name; } else { this.#name = `${cuid()}.${this.extname}`; } this.options = defaultOptionsDecorator; this.drive = drive; } /** * Getters / setters */ get name() { return this.#name; } set name(name) { this.#name = name; } get folder() { if (this.#folder) { return this.#folder; } if (typeof this.options.folder === 'string') { const parameters = extractPathParameters(this.options.folder); if (!parameters.length) { return this.options.folder; } } } get path() { if (!this.folder && this.originalPath) { return this.originalPath; } return path.join(this.folder, this.name); } /** * Methods */ getDisk() { return this.drive.use(this.options?.disk); } getBytes() { return this.getDisk().getBytes(this.path); } async getBuffer() { const arrayBuffer = await this.getBytes(); return Buffer.from(arrayBuffer); } getStream() { return this.getDisk().getStream(this.path); } getUrl() { return this.getDisk().getUrl(this.path); } getSignedUrl(signedUrlOptions) { return this.getDisk().getSignedUrl(this.path, signedUrlOptions); } setOptions(options) { this.options = { ...this.options, ...options, }; return this; } makeFolder(record) { if (typeof this.options.folder === 'function' && record) { this.#folder = this.options.folder(record); } else if (this.options.folder) { this.#folder = this.options.folder; } if (this.#folder && record) { const parameters = extractPathParameters(this.#folder); if (parameters) { parameters.forEach((parameter) => { const attribute = record.$attributes[parameter]; if (typeof attribute === 'string') { const name = string.slug(string.noCase(string.escapeHTML(attribute.toLowerCase()))); this.#folder = this.#folder?.replace(`:${parameter}`, name); } }); } } return this; } /** * */ toObject() { return { name: this.name, extname: this.extname, size: this.size, meta: this.meta, mimeType: this.mimeType, path: this.path, }; } toJSON() { if (this.url) { return { ...this.toObject(), url: this.url, }; } return this.toObject(); } }