@jrmc/adonis-attachment 2.1.1-1 → 2.2.0
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/src/attachment_manager.d.ts +3 -2
- package/build/src/attachment_manager.js +10 -12
- package/build/src/attachments/attachment.js +26 -7
- package/build/src/decorators/attachment.js +4 -0
- package/build/src/mixins/attachmentable.js +2 -2
- package/build/src/types/config.d.ts +2 -0
- package/build/src/utils/actions.d.ts +1 -1
- package/build/src/utils/actions.js +2 -2
- package/build/src/utils/default_values.d.ts +1 -1
- package/build/src/utils/default_values.js +2 -2
- package/package.json +2 -2
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
6
|
*/
|
|
7
7
|
import type { LoggerService } from '@adonisjs/core/types';
|
|
8
|
-
import type { DriveService } from '@adonisjs/drive/types';
|
|
8
|
+
import type { DriveService, SignedURLOptions } from '@adonisjs/drive/types';
|
|
9
9
|
import type { MultipartFile } from '@adonisjs/core/bodyparser';
|
|
10
10
|
import type { AttachmentBase, Attachment as AttachmentType } from './types/attachment.js';
|
|
11
11
|
import type { ResolvedAttachmentConfig } from './types/config.js';
|
|
@@ -19,7 +19,8 @@ export declare class AttachmentManager {
|
|
|
19
19
|
createFromFile(file: MultipartFile): Promise<Attachment>;
|
|
20
20
|
createFromBuffer(buffer: Buffer, name?: string): Promise<Attachment>;
|
|
21
21
|
getConverter(key: string): Promise<void | Converter>;
|
|
22
|
-
computeUrl(attachment: AttachmentType): Promise<void>;
|
|
22
|
+
computeUrl(attachment: AttachmentType | AttachmentBase, signedUrlOptions?: SignedURLOptions): Promise<void>;
|
|
23
|
+
preComputeUrl(attachment: AttachmentType): Promise<void>;
|
|
23
24
|
save(attachment: AttachmentBase): Promise<void>;
|
|
24
25
|
delete(attachment: AttachmentBase): Promise<void>;
|
|
25
26
|
}
|
|
@@ -58,27 +58,25 @@ export class AttachmentManager {
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
|
-
async computeUrl(attachment) {
|
|
62
|
-
if (attachment.options?.preComputeUrl === false) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
61
|
+
async computeUrl(attachment, signedUrlOptions) {
|
|
65
62
|
const disk = attachment.getDisk();
|
|
66
63
|
const fileVisibility = await disk.getVisibility(attachment.path);
|
|
67
64
|
if (fileVisibility === 'private') {
|
|
68
|
-
attachment.url = await attachment.getSignedUrl();
|
|
65
|
+
attachment.url = await attachment.getSignedUrl(signedUrlOptions);
|
|
69
66
|
}
|
|
70
67
|
else {
|
|
71
68
|
attachment.url = await attachment.getUrl();
|
|
72
69
|
}
|
|
73
|
-
|
|
70
|
+
}
|
|
71
|
+
async preComputeUrl(attachment) {
|
|
72
|
+
if (attachment.options?.preComputeUrl === false) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
await this.computeUrl(attachment);
|
|
76
|
+
if (attachment instanceof Attachment && attachment.variants) {
|
|
74
77
|
for (const key in attachment.variants) {
|
|
75
78
|
if (Object.prototype.hasOwnProperty.call(attachment.variants, key)) {
|
|
76
|
-
|
|
77
|
-
attachment.variants[key].url = await attachment.getSignedUrl(attachment.variants[key].key);
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
attachment.variants[key].url = await attachment.getUrl(attachment.variants[key].key);
|
|
81
|
-
}
|
|
79
|
+
await this.computeUrl(attachment.variants[key]);
|
|
82
80
|
}
|
|
83
81
|
}
|
|
84
82
|
}
|
|
@@ -79,6 +79,14 @@ export class Attachment extends AttachmentBase {
|
|
|
79
79
|
this.folder = this.options.folder;
|
|
80
80
|
this.path = path.join(this.folder, this.name);
|
|
81
81
|
}
|
|
82
|
+
if (this.variants) {
|
|
83
|
+
this.variants.forEach((v) => {
|
|
84
|
+
v.setOptions({
|
|
85
|
+
...this.options,
|
|
86
|
+
variants: []
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
|
82
90
|
return this;
|
|
83
91
|
}
|
|
84
92
|
toObject() {
|
|
@@ -98,15 +106,26 @@ export class Attachment extends AttachmentBase {
|
|
|
98
106
|
mimetype: this.mimeType,
|
|
99
107
|
meta: this.meta,
|
|
100
108
|
};
|
|
109
|
+
if (this.variants) {
|
|
110
|
+
this.variants.map(async (v) => {
|
|
111
|
+
data[v.key] = {
|
|
112
|
+
name: v.name,
|
|
113
|
+
extname: v.extname,
|
|
114
|
+
mimetype: v.mimeType,
|
|
115
|
+
meta: v.meta,
|
|
116
|
+
size: v.size,
|
|
117
|
+
};
|
|
118
|
+
});
|
|
119
|
+
}
|
|
101
120
|
if (this.url) {
|
|
102
121
|
data.url = this.url;
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
}
|
|
122
|
+
}
|
|
123
|
+
if (this.variants) {
|
|
124
|
+
this.variants.map(async (v) => {
|
|
125
|
+
if (v.url) {
|
|
126
|
+
data[v.key].url = v.url;
|
|
127
|
+
}
|
|
128
|
+
});
|
|
110
129
|
}
|
|
111
130
|
return data;
|
|
112
131
|
}
|
|
@@ -16,6 +16,7 @@ export const attachment = (options) => {
|
|
|
16
16
|
const defaultOptions = {
|
|
17
17
|
meta: defaultConfig.meta !== undefined ? defaultConfig.meta : defaultOptionsDecorator.meta,
|
|
18
18
|
rename: defaultConfig.rename !== undefined ? defaultConfig.rename : defaultOptionsDecorator.rename,
|
|
19
|
+
preComputeUrl: defaultConfig.preComputeUrl !== undefined ? defaultConfig.preComputeUrl : defaultOptionsDecorator.preComputeUrl,
|
|
19
20
|
};
|
|
20
21
|
if (!options || options?.meta === undefined) {
|
|
21
22
|
options.meta = defaultOptions.meta;
|
|
@@ -23,6 +24,9 @@ export const attachment = (options) => {
|
|
|
23
24
|
if (!options || options?.rename === undefined) {
|
|
24
25
|
options.rename = defaultOptions.rename;
|
|
25
26
|
}
|
|
27
|
+
if (!options || options?.preComputeUrl === undefined) {
|
|
28
|
+
options.preComputeUrl = defaultOptions.preComputeUrl;
|
|
29
|
+
}
|
|
26
30
|
target[optionsSym][attributeName] = options;
|
|
27
31
|
const Model = target.constructor;
|
|
28
32
|
Model.boot();
|
|
@@ -11,7 +11,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
11
11
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
12
12
|
};
|
|
13
13
|
import { beforeSave, afterSave, beforeDelete, afterFind, afterFetch, afterPaginate } from '@adonisjs/lucid/orm';
|
|
14
|
-
import { persistAttachment, commit, rollback, generateVariants,
|
|
14
|
+
import { persistAttachment, commit, rollback, generateVariants, preComputeUrl } from '../utils/actions.js';
|
|
15
15
|
import { clone, getAttachmentAttributeNames } from '../utils/helpers.js';
|
|
16
16
|
import { defaultStateAttributeMixin } from '../utils/default_values.js';
|
|
17
17
|
export const Attachmentable = (superclass) => {
|
|
@@ -20,7 +20,7 @@ export const Attachmentable = (superclass) => {
|
|
|
20
20
|
static async afterFindHook(modelInstance) {
|
|
21
21
|
const attachmentAttributeNames = getAttachmentAttributeNames(modelInstance);
|
|
22
22
|
await Promise.all(attachmentAttributeNames.map((attributeName) => {
|
|
23
|
-
return
|
|
23
|
+
return preComputeUrl(modelInstance, attributeName);
|
|
24
24
|
}));
|
|
25
25
|
}
|
|
26
26
|
static async afterFetchHook(modelInstances) {
|
|
@@ -21,6 +21,7 @@ export type AttachmentConfig = {
|
|
|
21
21
|
bin?: BinPaths;
|
|
22
22
|
meta?: boolean;
|
|
23
23
|
rename?: boolean;
|
|
24
|
+
preComputeUrl?: boolean;
|
|
24
25
|
converters?: ConverterConfig[];
|
|
25
26
|
};
|
|
26
27
|
export type ResolvedConverter = {
|
|
@@ -31,6 +32,7 @@ export type ResolvedAttachmentConfig = {
|
|
|
31
32
|
bin?: BinPaths;
|
|
32
33
|
meta?: boolean;
|
|
33
34
|
rename?: boolean;
|
|
35
|
+
preComputeUrl?: boolean;
|
|
34
36
|
converters?: ResolvedConverter[];
|
|
35
37
|
};
|
|
36
38
|
export {};
|
|
@@ -17,7 +17,7 @@ export declare function rollback(modelInstance: ModelWithAttachment): Promise<vo
|
|
|
17
17
|
* Persist attachment for a given attachment attributeName
|
|
18
18
|
*/
|
|
19
19
|
export declare function persistAttachment(modelInstance: ModelWithAttachment, attributeName: string): Promise<void>;
|
|
20
|
-
export declare function
|
|
20
|
+
export declare function preComputeUrl(modelInstance: ModelWithAttachment, attributeName: string): Promise<void>;
|
|
21
21
|
/**
|
|
22
22
|
* Launch converter by variant option
|
|
23
23
|
*/
|
|
@@ -62,11 +62,11 @@ export async function persistAttachment(modelInstance, attributeName) {
|
|
|
62
62
|
await attachmentManager.save(newFile);
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
|
-
export async function
|
|
65
|
+
export async function preComputeUrl(modelInstance, attributeName) {
|
|
66
66
|
const attachment = modelInstance.$attributes[attributeName];
|
|
67
67
|
const options = getOptions(modelInstance, attributeName);
|
|
68
68
|
attachment.setOptions(options);
|
|
69
|
-
return attachmentManager.
|
|
69
|
+
return attachmentManager.preComputeUrl(attachment);
|
|
70
70
|
}
|
|
71
71
|
/**
|
|
72
72
|
* Launch converter by variant option
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jrmc/adonis-attachment",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Turn any field on your Lucid model to an attachment data type",
|
|
6
6
|
"engines": {
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
"prettier": "@adonisjs/prettier-config",
|
|
83
83
|
"publishConfig": {
|
|
84
84
|
"access": "public",
|
|
85
|
-
"tag": "
|
|
85
|
+
"tag": "latest"
|
|
86
86
|
},
|
|
87
87
|
"volta": {
|
|
88
88
|
"node": "20.17.0"
|