UNPKG

@jrmc/adonis-attachment

Version:

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

122 lines (121 loc) 4 kB
/** * @jrmc/adonis-attachment * * @license MIT * @copyright Jeremy Chaufourier <jeremy@chaufourier.fr> */ import { defaultStateAttributeMixin } from '../../utils/default_values.js'; import { AttachmentTransactionService } from './attachment_transaction_service.js'; import { AttachmentPersisterService } from './attachment_persister_service.js'; import { AttachmentVariantService } from './attachment_variant_service.js'; import { AttachmentDetachmentService } from './attachment_detachment_service.js'; import { AttachmentUtils } from './attachment_utils.js'; export default class AttachmentRecorderService { #row; #transactionService = new AttachmentTransactionService(); #persistenceService = new AttachmentPersisterService(); #variantService = new AttachmentVariantService(); #detachmentService = new AttachmentDetachmentService(); constructor(row) { this.#row = row; this.#initializeAttachments(); } /** * Initialize attachments state if not exists */ #initializeAttachments() { if (!this.#row.$attachments) { this.#row.$attachments = structuredClone(defaultStateAttributeMixin); } } /** * During commit, we should cleanup the old detached files */ async commit() { await this.#transactionService.commit(this.#row.$attachments.detached); } /** * During rollback we should remove the attached files. */ async rollback() { await this.#transactionService.rollback(this.#row.$attachments.attached); } /** * Persist attachments before saving the row to the database */ async persist() { await this.#persistenceService.persistAttachments(this); } /** * Handle transaction lifecycle */ async transaction(options = { enabledRollback: true }) { await this.#transactionService.handleTransaction(this, options); } /** * Pre-compute URLs for all attachments */ async preComputeUrl() { await this.#persistenceService.preComputeUrls(this); } /** * Set key IDs for all attachments */ async setKeyId() { await this.#persistenceService.setKeyIds(this); } /** * Generate variants for all dirty attachment attributes */ async generateVariants() { await this.#variantService.generateVariants(this); } /** * Regenerate variants with specific options */ async regenerateVariants(options = {}) { await this.#variantService.regenerateVariants(this, options); } /** * Detach dirty attachments (mark for deletion) */ async detach() { await this.#detachmentService.detach(this); } /** * Detach all attachments (mark all for deletion) */ async detachAll() { await this.#detachmentService.detachAll(this); } /** * Get row instance */ get row() { return this.#row; } /** * Get attachments with specific options */ async getAttachments(options) { let attachments; if (options.requiredOriginal) { attachments = AttachmentUtils.getOriginalAttachmentsByAttributeName(this.#row, options.attributeName); } else if (options.requiredDirty) { attachments = AttachmentUtils.getDirtyAttachmentsByAttributeName(this.#row, options.attributeName); } else { attachments = AttachmentUtils.getAttachmentsByAttributeName(this.#row, options.attributeName); } const opts = AttachmentUtils.getOptionsByAttributeName(this.#row, options.attributeName); attachments.forEach(async (attachment) => { if (attachment) { attachment.setOptions(opts); await attachment.makeFolder(this.#row); // await attachment.makeName(this.#row, options.attributeName, attachment.originalName) } }); return attachments; } }