@jrmc/adonis-attachment 5.0.0-beta.2 → 5.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.d.ts +0 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +0 -1
- package/build/providers/attachment_provider.d.ts.map +1 -1
- package/build/providers/attachment_provider.js +10 -1
- package/build/services/regenerate_service.js +3 -3
- package/build/src/adapters/lock.d.ts +9 -0
- package/build/src/adapters/lock.d.ts.map +1 -0
- package/build/src/adapters/lock.js +21 -0
- package/build/src/attachment_manager.d.ts +4 -2
- package/build/src/attachment_manager.d.ts.map +1 -1
- package/build/src/attachment_manager.js +7 -1
- package/build/src/attachments/attachment.d.ts +1 -1
- package/build/src/attachments/attachment.js +1 -1
- package/build/src/controllers/attachments_controller.d.ts.map +1 -1
- package/build/src/controllers/attachments_controller.js +83 -74
- package/build/src/decorators/attachment.d.ts +3 -3
- package/build/src/decorators/attachment.d.ts.map +1 -1
- package/build/src/services/attachment/attachment_detachment_service.d.ts +19 -0
- package/build/src/services/attachment/attachment_detachment_service.d.ts.map +1 -0
- package/build/src/services/attachment/attachment_detachment_service.js +64 -0
- package/build/src/services/attachment/attachment_persister_service.d.ts +22 -0
- package/build/src/services/attachment/attachment_persister_service.d.ts.map +1 -0
- package/build/src/services/attachment/attachment_persister_service.js +93 -0
- package/build/src/services/attachment/attachment_recorder_service.d.ts +68 -0
- package/build/src/services/attachment/attachment_recorder_service.d.ts.map +1 -0
- package/build/src/services/attachment/attachment_recorder_service.js +119 -0
- package/build/src/services/attachment/attachment_transaction_service.d.ts +26 -0
- package/build/src/services/attachment/attachment_transaction_service.d.ts.map +1 -0
- package/build/src/services/attachment/attachment_transaction_service.js +43 -0
- package/build/src/services/attachment/attachment_utils.d.ts +35 -0
- package/build/src/services/attachment/attachment_utils.d.ts.map +1 -0
- package/build/src/services/attachment/attachment_utils.js +71 -0
- package/build/src/services/attachment/attachment_variant_service.d.ts +17 -0
- package/build/src/services/attachment/attachment_variant_service.d.ts.map +1 -0
- package/build/src/services/attachment/attachment_variant_service.js +72 -0
- package/build/src/services/attachment/index.d.ts +15 -0
- package/build/src/services/attachment/index.d.ts.map +1 -0
- package/build/src/services/attachment/index.js +15 -0
- package/build/src/services/attachment_service.d.ts +8 -0
- package/build/src/services/attachment_service.d.ts.map +1 -0
- package/build/src/services/attachment_service.js +7 -0
- package/build/src/services/variant/variant_generator_service.d.ts +24 -0
- package/build/src/services/variant/variant_generator_service.d.ts.map +1 -0
- package/build/src/services/variant/variant_generator_service.js +97 -0
- package/build/src/services/variant/variant_persister_service.d.ts +23 -0
- package/build/src/services/variant/variant_persister_service.d.ts.map +1 -0
- package/build/src/services/variant/variant_persister_service.js +60 -0
- package/build/src/services/variant/variant_purger_service.d.ts +15 -0
- package/build/src/services/variant/variant_purger_service.d.ts.map +1 -0
- package/build/src/services/variant/variant_purger_service.js +48 -0
- package/build/src/services/variant_service.d.ts +13 -0
- package/build/src/services/variant_service.d.ts.map +1 -0
- package/build/src/services/variant_service.js +58 -0
- package/build/src/types/attachment.d.ts +3 -3
- package/build/src/types/attachment.d.ts.map +1 -1
- package/build/src/types/index.d.ts +6 -0
- package/build/src/types/index.d.ts.map +1 -1
- package/build/src/types/index.js +6 -0
- package/build/src/types/lock.d.ts +14 -0
- package/build/src/types/lock.d.ts.map +1 -0
- package/build/src/types/lock.js +7 -0
- package/build/src/types/metadata.d.ts +6 -0
- package/build/src/types/metadata.d.ts.map +1 -1
- package/build/src/types/metadata.js +6 -0
- package/build/src/types/regenerate.d.ts +6 -0
- package/build/src/types/regenerate.d.ts.map +1 -1
- package/build/src/types/regenerate.js +6 -0
- package/build/src/types/service.d.ts +6 -0
- package/build/src/types/service.d.ts.map +1 -1
- package/build/src/types/service.js +6 -0
- package/build/src/utils/hooks.js +5 -5
- package/build/stubs/config.stub +102 -4
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +27 -22
- package/build/src/converter_manager.d.ts +0 -13
- package/build/src/converter_manager.d.ts.map +0 -1
- package/build/src/converter_manager.js +0 -121
- package/build/src/services/record_with_attachment.d.ts +0 -33
- package/build/src/services/record_with_attachment.d.ts.map +0 -1
- package/build/src/services/record_with_attachment.js +0 -303
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import type { RowWithAttachment } from '../../types/mixin.js';
|
|
8
|
+
import type { Attachment as AttachmentType } from '../../types/attachment.js';
|
|
9
|
+
import type { RecordWithAttachment as RecordWithAttachmentImplementation } from '../../types/service.js';
|
|
10
|
+
import type { RegenerateOptions } from '../../types/regenerate.js';
|
|
11
|
+
import type { TransactionOptions } from './attachment_transaction_service.js';
|
|
12
|
+
export default class AttachmentRecorderService implements RecordWithAttachmentImplementation {
|
|
13
|
+
#private;
|
|
14
|
+
constructor(row: RowWithAttachment);
|
|
15
|
+
/**
|
|
16
|
+
* During commit, we should cleanup the old detached files
|
|
17
|
+
*/
|
|
18
|
+
commit(): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* During rollback we should remove the attached files.
|
|
21
|
+
*/
|
|
22
|
+
rollback(): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Persist attachments before saving the row to the database
|
|
25
|
+
*/
|
|
26
|
+
persist(): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Handle transaction lifecycle
|
|
29
|
+
*/
|
|
30
|
+
transaction(options?: TransactionOptions): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Pre-compute URLs for all attachments
|
|
33
|
+
*/
|
|
34
|
+
preComputeUrl(): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Set key IDs for all attachments
|
|
37
|
+
*/
|
|
38
|
+
setKeyId(): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Generate variants for all dirty attachment attributes
|
|
41
|
+
*/
|
|
42
|
+
generateVariants(): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Regenerate variants with specific options
|
|
45
|
+
*/
|
|
46
|
+
regenerateVariants(options?: RegenerateOptions): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Detach dirty attachments (mark for deletion)
|
|
49
|
+
*/
|
|
50
|
+
detach(): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Detach all attachments (mark all for deletion)
|
|
53
|
+
*/
|
|
54
|
+
detachAll(): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Get row instance
|
|
57
|
+
*/
|
|
58
|
+
get row(): RowWithAttachment;
|
|
59
|
+
/**
|
|
60
|
+
* Get attachments with specific options
|
|
61
|
+
*/
|
|
62
|
+
getAttachments(options: {
|
|
63
|
+
attributeName: string;
|
|
64
|
+
requiredOriginal?: boolean;
|
|
65
|
+
requiredDirty?: boolean;
|
|
66
|
+
}): AttachmentType[];
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=attachment_recorder_service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachment_recorder_service.d.ts","sourceRoot":"","sources":["../../../../src/services/attachment/attachment_recorder_service.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AAC7D,OAAO,KAAK,EAAE,UAAU,IAAI,cAAc,EAAE,MAAM,2BAA2B,CAAA;AAC7E,OAAO,KAAK,EAAE,oBAAoB,IAAI,kCAAkC,EAAE,MAAM,wBAAwB,CAAA;AACxG,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAA;AAClE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAA;AAS7E,MAAM,CAAC,OAAO,OAAO,yBAA0B,YAAW,kCAAkC;;gBAO9E,GAAG,EAAE,iBAAiB;IAclC;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAI7B;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAI/B;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAI9B;;OAEG;IACG,WAAW,CAAC,OAAO,GAAE,kBAA8C,GAAG,OAAO,CAAC,IAAI,CAAC;IAIzF;;OAEG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IAIpC;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAI/B;;OAEG;IACG,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC;IAIvC;;OAEG;IACG,kBAAkB,CAAC,OAAO,GAAE,iBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC;IAIxE;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAI7B;;OAEG;IACG,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;IAIhC;;OAEG;IACH,IAAI,GAAG,IAAI,iBAAiB,CAE3B;IAED;;OAEG;IACH,cAAc,CAAC,OAAO,EAAE;QACtB,aAAa,EAAE,MAAM,CAAA;QACrB,gBAAgB,CAAC,EAAE,OAAO,CAAA;QAC1B,aAAa,CAAC,EAAE,OAAO,CAAA;KACxB,GAAG,cAAc,EAAE;CAoBrB"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import { defaultStateAttributeMixin } from '../../utils/default_values.js';
|
|
8
|
+
import { AttachmentTransactionService } from './attachment_transaction_service.js';
|
|
9
|
+
import { AttachmentPersisterService } from './attachment_persister_service.js';
|
|
10
|
+
import { AttachmentVariantService } from './attachment_variant_service.js';
|
|
11
|
+
import { AttachmentDetachmentService } from './attachment_detachment_service.js';
|
|
12
|
+
import { AttachmentUtils } from './attachment_utils.js';
|
|
13
|
+
export default class AttachmentRecorderService {
|
|
14
|
+
#row;
|
|
15
|
+
#transactionService = new AttachmentTransactionService();
|
|
16
|
+
#persistenceService = new AttachmentPersisterService();
|
|
17
|
+
#variantService = new AttachmentVariantService();
|
|
18
|
+
#detachmentService = new AttachmentDetachmentService();
|
|
19
|
+
constructor(row) {
|
|
20
|
+
this.#row = row;
|
|
21
|
+
this.#initializeAttachments();
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Initialize attachments state if not exists
|
|
25
|
+
*/
|
|
26
|
+
#initializeAttachments() {
|
|
27
|
+
if (!this.#row.$attachments) {
|
|
28
|
+
this.#row.$attachments = structuredClone(defaultStateAttributeMixin);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* During commit, we should cleanup the old detached files
|
|
33
|
+
*/
|
|
34
|
+
async commit() {
|
|
35
|
+
await this.#transactionService.commit(this.#row.$attachments.detached);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* During rollback we should remove the attached files.
|
|
39
|
+
*/
|
|
40
|
+
async rollback() {
|
|
41
|
+
await this.#transactionService.rollback(this.#row.$attachments.attached);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Persist attachments before saving the row to the database
|
|
45
|
+
*/
|
|
46
|
+
async persist() {
|
|
47
|
+
await this.#persistenceService.persistAttachments(this);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Handle transaction lifecycle
|
|
51
|
+
*/
|
|
52
|
+
async transaction(options = { enabledRollback: true }) {
|
|
53
|
+
await this.#transactionService.handleTransaction(this, options);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Pre-compute URLs for all attachments
|
|
57
|
+
*/
|
|
58
|
+
async preComputeUrl() {
|
|
59
|
+
await this.#persistenceService.preComputeUrls(this);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Set key IDs for all attachments
|
|
63
|
+
*/
|
|
64
|
+
async setKeyId() {
|
|
65
|
+
await this.#persistenceService.setKeyIds(this);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Generate variants for all dirty attachment attributes
|
|
69
|
+
*/
|
|
70
|
+
async generateVariants() {
|
|
71
|
+
await this.#variantService.generateVariants(this);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Regenerate variants with specific options
|
|
75
|
+
*/
|
|
76
|
+
async regenerateVariants(options = {}) {
|
|
77
|
+
await this.#variantService.regenerateVariants(this, options);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Detach dirty attachments (mark for deletion)
|
|
81
|
+
*/
|
|
82
|
+
async detach() {
|
|
83
|
+
await this.#detachmentService.detach(this);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Detach all attachments (mark all for deletion)
|
|
87
|
+
*/
|
|
88
|
+
async detachAll() {
|
|
89
|
+
await this.#detachmentService.detachAll(this);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Get row instance
|
|
93
|
+
*/
|
|
94
|
+
get row() {
|
|
95
|
+
return this.#row;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Get attachments with specific options
|
|
99
|
+
*/
|
|
100
|
+
getAttachments(options) {
|
|
101
|
+
let attachments;
|
|
102
|
+
if (options.requiredOriginal) {
|
|
103
|
+
attachments = AttachmentUtils.getOriginalAttachmentsByAttributeName(this.#row, options.attributeName);
|
|
104
|
+
}
|
|
105
|
+
else if (options.requiredDirty) {
|
|
106
|
+
attachments = AttachmentUtils.getDirtyAttachmentsByAttributeName(this.#row, options.attributeName);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
attachments = AttachmentUtils.getAttachmentsByAttributeName(this.#row, options.attributeName);
|
|
110
|
+
}
|
|
111
|
+
const opts = AttachmentUtils.getOptionsByAttributeName(this.#row, options.attributeName);
|
|
112
|
+
attachments.forEach((attachment) => {
|
|
113
|
+
if (attachment) {
|
|
114
|
+
attachment.setOptions(opts).makeFolder(this.#row);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
return attachments;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import type { Attachment as AttachmentType } from '../../types/attachment.js';
|
|
8
|
+
import type { RecordWithAttachment as RecordWithAttachmentImplementation } from '../../types/service.js';
|
|
9
|
+
export interface TransactionOptions {
|
|
10
|
+
enabledRollback?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export declare class AttachmentTransactionService {
|
|
13
|
+
/**
|
|
14
|
+
* During commit, we should cleanup the old detached files
|
|
15
|
+
*/
|
|
16
|
+
commit(detachedAttachments: AttachmentType[]): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* During rollback we should remove the attached files.
|
|
19
|
+
*/
|
|
20
|
+
rollback(attachedAttachments: AttachmentType[]): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Handle transaction lifecycle with commit and rollback hooks
|
|
23
|
+
*/
|
|
24
|
+
handleTransaction(record: RecordWithAttachmentImplementation, options?: TransactionOptions): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=attachment_transaction_service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachment_transaction_service.d.ts","sourceRoot":"","sources":["../../../../src/services/attachment/attachment_transaction_service.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,UAAU,IAAI,cAAc,EAAE,MAAM,2BAA2B,CAAA;AAC7E,OAAO,KAAK,EAAE,oBAAoB,IAAI,kCAAkC,EAAE,MAAM,wBAAwB,CAAA;AAGxG,MAAM,WAAW,kBAAkB;IACjC,eAAe,CAAC,EAAE,OAAO,CAAA;CAC1B;AAED,qBAAa,4BAA4B;IACvC;;OAEG;IACG,MAAM,CAAC,mBAAmB,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAQlE;;OAEG;IACG,QAAQ,CAAC,mBAAmB,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAQpE;;OAEG;IACG,iBAAiB,CACrB,MAAM,EAAE,kCAAkC,EAC1C,OAAO,GAAE,kBAA8C,GACtD,OAAO,CAAC,IAAI,CAAC;CAiBjB"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import attachmentManager from '../../../services/main.js';
|
|
8
|
+
export class AttachmentTransactionService {
|
|
9
|
+
/**
|
|
10
|
+
* During commit, we should cleanup the old detached files
|
|
11
|
+
*/
|
|
12
|
+
async commit(detachedAttachments) {
|
|
13
|
+
await Promise.allSettled(detachedAttachments.map((attachment) => attachmentManager.remove(attachment)));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* During rollback we should remove the attached files.
|
|
17
|
+
*/
|
|
18
|
+
async rollback(attachedAttachments) {
|
|
19
|
+
await Promise.allSettled(attachedAttachments.map((attachment) => attachmentManager.remove(attachment)));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Handle transaction lifecycle with commit and rollback hooks
|
|
23
|
+
*/
|
|
24
|
+
async handleTransaction(record, options = { enabledRollback: true }) {
|
|
25
|
+
try {
|
|
26
|
+
if (record.row.$trx) {
|
|
27
|
+
record.row.$trx.after('commit', () => this.commit(record.row.$attachments.detached));
|
|
28
|
+
if (options.enabledRollback) {
|
|
29
|
+
record.row.$trx.after('rollback', () => this.rollback(record.row.$attachments.attached));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
await this.commit(record.row.$attachments.detached);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (options.enabledRollback) {
|
|
38
|
+
await this.rollback(record.row.$attachments.attached);
|
|
39
|
+
}
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import type { RowWithAttachment } from '../../types/mixin.js';
|
|
8
|
+
import type { Attachment as AttachmentType, LucidOptions } from '../../types/attachment.js';
|
|
9
|
+
export declare class AttachmentUtils {
|
|
10
|
+
/**
|
|
11
|
+
* Get attachments by attribute name, handling both single and array values
|
|
12
|
+
*/
|
|
13
|
+
static getAttachmentsByAttributeName(row: RowWithAttachment, name: string): AttachmentType[];
|
|
14
|
+
/**
|
|
15
|
+
* Get original attachments by attribute name, handling both single and array values
|
|
16
|
+
*/
|
|
17
|
+
static getOriginalAttachmentsByAttributeName(row: RowWithAttachment, name: string): AttachmentType[];
|
|
18
|
+
/**
|
|
19
|
+
* Get dirty attachments by attribute name, handling both single and array values
|
|
20
|
+
*/
|
|
21
|
+
static getDirtyAttachmentsByAttributeName(row: RowWithAttachment, name: string): AttachmentType[];
|
|
22
|
+
/**
|
|
23
|
+
* Get options by attribute name from the model prototype
|
|
24
|
+
*/
|
|
25
|
+
static getOptionsByAttributeName(row: RowWithAttachment, name: string): LucidOptions;
|
|
26
|
+
/**
|
|
27
|
+
* Get all attribute names that contain attachments
|
|
28
|
+
*/
|
|
29
|
+
static getAttributeNamesOfAttachment(row: RowWithAttachment): string[];
|
|
30
|
+
/**
|
|
31
|
+
* Get dirty attribute names that contain attachments
|
|
32
|
+
*/
|
|
33
|
+
static getDirtyAttributeNamesOfAttachment(row: RowWithAttachment): string[];
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=attachment_utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachment_utils.d.ts","sourceRoot":"","sources":["../../../../src/services/attachment/attachment_utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AAC7D,OAAO,KAAK,EAAE,UAAU,IAAI,cAAc,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAI3F,qBAAa,eAAe;IAC1B;;OAEG;IACH,MAAM,CAAC,6BAA6B,CAAC,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,MAAM,GAAG,cAAc,EAAE;IAO5F;;OAEG;IACH,MAAM,CAAC,qCAAqC,CAAC,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,MAAM,GAAG,cAAc,EAAE;IAOpG;;OAEG;IACH,MAAM,CAAC,kCAAkC,CAAC,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,MAAM,GAAG,cAAc,EAAE;IAOjG;;OAEG;IACH,MAAM,CAAC,yBAAyB,CAAC,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY;IAIpF;;OAEG;IACH,MAAM,CAAC,6BAA6B,CAAC,GAAG,EAAE,iBAAiB,GAAG,MAAM,EAAE;IAUtE;;OAEG;IACH,MAAM,CAAC,kCAAkC,CAAC,GAAG,EAAE,iBAAiB,GAAG,MAAM,EAAE;CAoB5E"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import { Attachment } from '../../attachments/attachment.js';
|
|
8
|
+
import { optionsSym } from '../../utils/symbols.js';
|
|
9
|
+
export class AttachmentUtils {
|
|
10
|
+
/**
|
|
11
|
+
* Get attachments by attribute name, handling both single and array values
|
|
12
|
+
*/
|
|
13
|
+
static getAttachmentsByAttributeName(row, name) {
|
|
14
|
+
if (Array.isArray(row.$attributes[name])) {
|
|
15
|
+
return row.$attributes[name];
|
|
16
|
+
}
|
|
17
|
+
return [row.$attributes[name]];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Get original attachments by attribute name, handling both single and array values
|
|
21
|
+
*/
|
|
22
|
+
static getOriginalAttachmentsByAttributeName(row, name) {
|
|
23
|
+
if (Array.isArray(row.$original[name])) {
|
|
24
|
+
return row.$original[name];
|
|
25
|
+
}
|
|
26
|
+
return [row.$original[name]];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get dirty attachments by attribute name, handling both single and array values
|
|
30
|
+
*/
|
|
31
|
+
static getDirtyAttachmentsByAttributeName(row, name) {
|
|
32
|
+
if (Array.isArray(row.$dirty[name])) {
|
|
33
|
+
return row.$dirty[name];
|
|
34
|
+
}
|
|
35
|
+
return [row.$dirty[name]];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Get options by attribute name from the model prototype
|
|
39
|
+
*/
|
|
40
|
+
static getOptionsByAttributeName(row, name) {
|
|
41
|
+
return row.constructor.prototype[optionsSym]?.[name];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get all attribute names that contain attachments
|
|
45
|
+
*/
|
|
46
|
+
static getAttributeNamesOfAttachment(row) {
|
|
47
|
+
return Object.keys(row.$attributes).filter((name) => {
|
|
48
|
+
const value = row.$attributes[name];
|
|
49
|
+
return (value instanceof Attachment ||
|
|
50
|
+
(Array.isArray(value) && value.every((item) => item instanceof Attachment)));
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Get dirty attribute names that contain attachments
|
|
55
|
+
*/
|
|
56
|
+
static getDirtyAttributeNamesOfAttachment(row) {
|
|
57
|
+
return Object.keys(row.$dirty).filter((name) => {
|
|
58
|
+
const dirtyValue = row.$dirty[name];
|
|
59
|
+
const originalValue = row.$original[name]; // if dirtyValue is null, check original type
|
|
60
|
+
const isDirtyAttachment = dirtyValue instanceof Attachment ||
|
|
61
|
+
(Array.isArray(dirtyValue) &&
|
|
62
|
+
dirtyValue.length &&
|
|
63
|
+
dirtyValue.every((item) => item instanceof Attachment));
|
|
64
|
+
const isOriginalAttachment = originalValue instanceof Attachment ||
|
|
65
|
+
(Array.isArray(originalValue) &&
|
|
66
|
+
originalValue.length &&
|
|
67
|
+
originalValue.every((item) => item instanceof Attachment));
|
|
68
|
+
return isDirtyAttachment || isOriginalAttachment;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { RecordWithAttachment as RecordWithAttachmentImplementation } from '../../types/service.js';
|
|
2
|
+
import type { RegenerateOptions } from '../../types/regenerate.js';
|
|
3
|
+
export interface VariantOptions {
|
|
4
|
+
variants?: string[];
|
|
5
|
+
}
|
|
6
|
+
export declare class AttachmentVariantService {
|
|
7
|
+
#private;
|
|
8
|
+
/**
|
|
9
|
+
* Generate variants for all dirty attachment attributes
|
|
10
|
+
*/
|
|
11
|
+
generateVariants(record: RecordWithAttachmentImplementation): Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Regenerate variants with specific options
|
|
14
|
+
*/
|
|
15
|
+
regenerateVariants(record: RecordWithAttachmentImplementation, options?: RegenerateOptions): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=attachment_variant_service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachment_variant_service.d.ts","sourceRoot":"","sources":["../../../../src/services/attachment/attachment_variant_service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,oBAAoB,IAAI,kCAAkC,EAAE,MAAM,wBAAwB,CAAA;AACxG,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAA;AAOlE,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;CACpB;AAED,qBAAa,wBAAwB;;IACnC;;OAEG;IACG,gBAAgB,CAAC,MAAM,EAAE,kCAAkC,GAAG,OAAO,CAAC,IAAI,CAAC;IAajF;;OAEG;IACG,kBAAkB,CACtB,MAAM,EAAE,kCAAkC,EAC1C,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,IAAI,CAAC;CAyDjB"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import logger from '@adonisjs/core/services/logger';
|
|
2
|
+
import attachmentManager from '../../../services/main.js';
|
|
3
|
+
import VariantService from '../variant_service.js';
|
|
4
|
+
import { AttachmentUtils } from './attachment_utils.js';
|
|
5
|
+
import { E_CANNOT_CREATE_VARIANT } from '../../errors.js';
|
|
6
|
+
export class AttachmentVariantService {
|
|
7
|
+
/**
|
|
8
|
+
* Generate variants for all dirty attachment attributes
|
|
9
|
+
*/
|
|
10
|
+
async generateVariants(record) {
|
|
11
|
+
/* this.#row.$dirty is not available in afterSave hooks */
|
|
12
|
+
const attachmentAttributeNames = record.row.$attachments.dirtied;
|
|
13
|
+
for await (const name of attachmentAttributeNames) {
|
|
14
|
+
if (!record.row.$attributes[name]) {
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
this.#queueVariantGeneration(name, record, {});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Regenerate variants with specific options
|
|
22
|
+
*/
|
|
23
|
+
async regenerateVariants(record, options = {}) {
|
|
24
|
+
let attachmentAttributeNames;
|
|
25
|
+
if (options.attributes?.length) {
|
|
26
|
+
attachmentAttributeNames = options.attributes;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
attachmentAttributeNames = AttachmentUtils.getAttributeNamesOfAttachment(record.row);
|
|
30
|
+
}
|
|
31
|
+
for await (const name of attachmentAttributeNames) {
|
|
32
|
+
if (!record.row.$attributes[name]) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
this.#queueVariantGeneration(name, record, { variants: options.variants });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Queue variant generation with error handling
|
|
40
|
+
*/
|
|
41
|
+
#queueVariantGeneration(name, record, options) {
|
|
42
|
+
attachmentManager.queue.push({
|
|
43
|
+
name: `${record.row.constructor.name}-${name}`,
|
|
44
|
+
async run() {
|
|
45
|
+
const model = record.row.constructor;
|
|
46
|
+
await attachmentManager.lock.createLock(`attachment.${model.table}-${name}`).run(async () => {
|
|
47
|
+
const variantService = new VariantService({
|
|
48
|
+
record,
|
|
49
|
+
attributeName: name,
|
|
50
|
+
options: AttachmentUtils.getOptionsByAttributeName(record.row, name),
|
|
51
|
+
filters: {
|
|
52
|
+
variants: options.variants
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
await variantService.run();
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
})
|
|
59
|
+
.onError = (error) => this.#handleVariantError(error);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Handle variant generation errors
|
|
63
|
+
*/
|
|
64
|
+
#handleVariantError(error) {
|
|
65
|
+
if (error.message) {
|
|
66
|
+
logger.error(error.message);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
throw new E_CANNOT_CREATE_VARIANT([error]);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
export { default as AttachmentRecordService } from './attachment_recorder_service.js';
|
|
8
|
+
export { AttachmentTransactionService } from './attachment_transaction_service.js';
|
|
9
|
+
export { AttachmentPersisterService } from './attachment_persister_service.js';
|
|
10
|
+
export { AttachmentVariantService } from './attachment_variant_service.js';
|
|
11
|
+
export { AttachmentDetachmentService } from './attachment_detachment_service.js';
|
|
12
|
+
export { AttachmentUtils } from './attachment_utils.js';
|
|
13
|
+
export type { TransactionOptions } from './attachment_transaction_service.js';
|
|
14
|
+
export type { VariantOptions } from './attachment_variant_service.js';
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/services/attachment/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,OAAO,IAAI,uBAAuB,EAAE,MAAM,kCAAkC,CAAA;AAGrF,OAAO,EAAE,4BAA4B,EAAE,MAAM,qCAAqC,CAAA;AAClF,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAA;AAC9E,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAA;AAC1E,OAAO,EAAE,2BAA2B,EAAE,MAAM,oCAAoC,CAAA;AAGhF,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAGvD,YAAY,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAA;AAC7E,YAAY,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
// Main service
|
|
8
|
+
export { default as AttachmentRecordService } from './attachment_recorder_service.js';
|
|
9
|
+
// Specialized services
|
|
10
|
+
export { AttachmentTransactionService } from './attachment_transaction_service.js';
|
|
11
|
+
export { AttachmentPersisterService } from './attachment_persister_service.js';
|
|
12
|
+
export { AttachmentVariantService } from './attachment_variant_service.js';
|
|
13
|
+
export { AttachmentDetachmentService } from './attachment_detachment_service.js';
|
|
14
|
+
// Utilities
|
|
15
|
+
export { AttachmentUtils } from './attachment_utils.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachment_service.d.ts","sourceRoot":"","sources":["../../../src/services/attachment_service.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,uBAAuB,IAAI,OAAO,EAAE,MAAM,uBAAuB,CAAA"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import type { Attachment, Variant, LucidOptions } from '../../types/attachment.js';
|
|
8
|
+
import type { Converter } from '../../types/converter.js';
|
|
9
|
+
export default class VariantGeneratorService {
|
|
10
|
+
#private;
|
|
11
|
+
generate({ attachments, options, filters }: {
|
|
12
|
+
attachments: Attachment[];
|
|
13
|
+
options: LucidOptions;
|
|
14
|
+
filters?: {
|
|
15
|
+
variants?: string[];
|
|
16
|
+
};
|
|
17
|
+
}): Promise<Variant[]>;
|
|
18
|
+
generateVariant({ key, attachment, converter }: {
|
|
19
|
+
key: string;
|
|
20
|
+
attachment: Attachment;
|
|
21
|
+
converter: Converter;
|
|
22
|
+
}): Promise<Variant | null>;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=variant_generator_service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"variant_generator_service.d.ts","sourceRoot":"","sources":["../../../../src/services/variant/variant_generator_service.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAClF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AAOzD,MAAM,CAAC,OAAO,OAAO,uBAAuB;;IACpC,QAAQ,CAAC,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE;QAChD,WAAW,EAAE,UAAU,EAAE,CAAC;QAC1B,OAAO,EAAE,YAAY,CAAC;QACtB,OAAO,CAAC,EAAE;YAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;SAAE,CAAA;KAClC,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IAmBhB,eAAe,CAAC,EAAE,GAAG,EAAE,UAAU,EAAE,SAAS,EAAE,EAAE;QACpD,GAAG,EAAE,MAAM,CAAC;QACZ,UAAU,EAAE,UAAU,CAAC;QACvB,SAAS,EAAE,SAAS,CAAA;KACrB,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;CAkF5B"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import logger from '@adonisjs/core/services/logger';
|
|
8
|
+
import attachmentManager from '../../../services/main.js';
|
|
9
|
+
import { streamToTempFile } from '../../utils/helpers.js';
|
|
10
|
+
export default class VariantGeneratorService {
|
|
11
|
+
async generate({ attachments, options, filters }) {
|
|
12
|
+
const variants = [];
|
|
13
|
+
const variantKeys = this.#getVariantKeysToProcess(options, filters);
|
|
14
|
+
for (const key of variantKeys) {
|
|
15
|
+
const converter = await this.#getConverter(key);
|
|
16
|
+
if (!converter)
|
|
17
|
+
continue;
|
|
18
|
+
for (const attachment of attachments) {
|
|
19
|
+
const variant = await this.generateVariant({ key, attachment, converter });
|
|
20
|
+
if (variant) {
|
|
21
|
+
variants.push(variant);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return variants;
|
|
26
|
+
}
|
|
27
|
+
async generateVariant({ key, attachment, converter }) {
|
|
28
|
+
try {
|
|
29
|
+
const input = await this.#prepareInput(attachment);
|
|
30
|
+
const output = await this.#convertFile(input, converter);
|
|
31
|
+
if (!output) {
|
|
32
|
+
// throw new errors.E_CANNOT_PATH_BY_CONVERTER()
|
|
33
|
+
logger.warn(`Converter returned no output for key: ${key}`);
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
const variant = await attachment.createVariant(key, output);
|
|
37
|
+
await this.#processBlurhash(variant, converter);
|
|
38
|
+
await attachmentManager.write(variant);
|
|
39
|
+
return variant;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
logger.error(`Failed to generate variant ${key} for attachment: ${error.message}`);
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
#getVariantKeysToProcess(options, filters) {
|
|
47
|
+
if (!options.variants)
|
|
48
|
+
return [];
|
|
49
|
+
return options.variants.filter(key => filters?.variants === undefined ||
|
|
50
|
+
filters.variants.includes(key));
|
|
51
|
+
}
|
|
52
|
+
async #getConverter(key) {
|
|
53
|
+
try {
|
|
54
|
+
return await attachmentManager.getConverter(key);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
logger.error(`Failed to get converter for key ${key}: ${error.message}`);
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async #prepareInput(attachment) {
|
|
62
|
+
if (attachment.input) {
|
|
63
|
+
return attachment.input;
|
|
64
|
+
}
|
|
65
|
+
const stream = await attachment.getStream();
|
|
66
|
+
return await streamToTempFile(stream);
|
|
67
|
+
}
|
|
68
|
+
async #convertFile(input, converter) {
|
|
69
|
+
if (!converter.handle) {
|
|
70
|
+
throw new Error('Converter handle method is required');
|
|
71
|
+
}
|
|
72
|
+
if (!converter.options) {
|
|
73
|
+
throw new Error('Converter options are required');
|
|
74
|
+
}
|
|
75
|
+
return await converter.handle({
|
|
76
|
+
input,
|
|
77
|
+
options: converter.options,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
async #processBlurhash(variant, converter) {
|
|
81
|
+
const blurhashConfig = converter.options?.blurhash;
|
|
82
|
+
if (!blurhashConfig)
|
|
83
|
+
return;
|
|
84
|
+
const shouldGenerate = typeof blurhashConfig === 'boolean'
|
|
85
|
+
? blurhashConfig
|
|
86
|
+
: blurhashConfig.enabled === true;
|
|
87
|
+
if (!shouldGenerate)
|
|
88
|
+
return;
|
|
89
|
+
try {
|
|
90
|
+
const options = typeof blurhashConfig !== 'boolean' ? blurhashConfig : undefined;
|
|
91
|
+
await variant.generateBlurhash(options);
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
logger.error(`Blurhash generation failed: ${error.message}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|