@jrmc/adonis-attachment 3.0.0 → 3.1.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/bin/test.d.ts +1 -0
- package/build/bin/test.js +34 -0
- package/build/providers/attachment_provider.d.ts +1 -1
- package/build/src/adapters/exif.d.ts +7 -1
- package/build/src/adapters/exif.js +8 -5
- package/build/src/attachment_manager.d.ts +3 -0
- package/build/src/attachment_manager.js +20 -3
- package/build/src/errors.d.ts +18 -14
- package/build/src/errors.js +4 -0
- package/build/src/mixins/attachmentable.d.ts +127 -112
- package/build/src/mixins/attachmentable.js +2 -2
- package/build/src/utils/helpers.d.ts +2 -0
- package/build/src/utils/helpers.js +35 -1
- package/build/tests/attachment-manager.spec.d.ts +7 -0
- package/build/tests/attachment-manager.spec.js +234 -0
- package/build/tests/attachment.spec.d.ts +7 -0
- package/build/tests/attachment.spec.js +16 -0
- package/build/tests/commands.spec.d.ts +7 -0
- package/build/tests/commands.spec.js +58 -0
- package/build/tests/fixtures/converters/image_converter.d.ts +12 -0
- package/build/tests/fixtures/converters/image_converter.js +12 -0
- package/build/tests/fixtures/factories/user.d.ts +8 -0
- package/build/tests/fixtures/factories/user.js +19 -0
- package/build/tests/fixtures/factories/user_with_variants.d.ts +8 -0
- package/build/tests/fixtures/factories/user_with_variants.js +19 -0
- package/build/tests/fixtures/migrations/create_users_table.d.ts +12 -0
- package/build/tests/fixtures/migrations/create_users_table.js +23 -0
- package/build/tests/fixtures/models/user.d.ts +466 -0
- package/build/tests/fixtures/models/user.js +36 -0
- package/build/tests/fixtures/models/user_with_variants.d.ts +465 -0
- package/build/tests/fixtures/models/user_with_variants.js +33 -0
- package/build/tests/helpers/app.d.ts +29 -0
- package/build/tests/helpers/app.js +104 -0
- package/build/tests/helpers/index.d.ts +7 -0
- package/build/tests/helpers/index.js +7 -0
- package/build/tests/options.spec.d.ts +7 -0
- package/build/tests/options.spec.js +126 -0
- package/build/tests/variants.spec.d.ts +7 -0
- package/build/tests/variants.spec.js +21 -0
- package/package.json +39 -14
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import { copyFile, mkdir } from 'node:fs/promises';
|
|
8
|
+
import { IgnitorFactory } from '@adonisjs/core/factories';
|
|
9
|
+
import { defineConfig as defineLucidConfig } from '@adonisjs/lucid';
|
|
10
|
+
import { defineConfig } from '../../src/define_config.js';
|
|
11
|
+
import { defineConfig as defineDriveConfig, services } from '@adonisjs/drive';
|
|
12
|
+
import { BASE_URL } from './index.js';
|
|
13
|
+
const IMPORTER = (filePath) => {
|
|
14
|
+
if (filePath.startsWith('./') || filePath.startsWith('../')) {
|
|
15
|
+
return import(new URL(filePath, BASE_URL).href);
|
|
16
|
+
}
|
|
17
|
+
return import(filePath);
|
|
18
|
+
};
|
|
19
|
+
const attachmentConfig = defineConfig({
|
|
20
|
+
converters: {
|
|
21
|
+
thumbnail: {
|
|
22
|
+
converter: () => import('../fixtures/converters/image_converter.js'),
|
|
23
|
+
options: {
|
|
24
|
+
resize: 300,
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
medium: {
|
|
28
|
+
converter: () => import('../fixtures/converters/image_converter.js'),
|
|
29
|
+
options: {
|
|
30
|
+
resize: 600,
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
export async function createApp(options = {}) {
|
|
36
|
+
const app = new IgnitorFactory()
|
|
37
|
+
.merge({
|
|
38
|
+
rcFileContents: {
|
|
39
|
+
commands: [() => import('@adonisjs/lucid/commands')],
|
|
40
|
+
providers: [
|
|
41
|
+
() => import('@adonisjs/lucid/database_provider'),
|
|
42
|
+
() => import('@adonisjs/drive/drive_provider'),
|
|
43
|
+
() => import('../../providers/attachment_provider.js'),
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
config: {
|
|
47
|
+
attachment: options
|
|
48
|
+
? defineConfig({
|
|
49
|
+
...options,
|
|
50
|
+
converters: {
|
|
51
|
+
thumbnail: {
|
|
52
|
+
converter: () => import('../fixtures/converters/image_converter.js'),
|
|
53
|
+
options: {
|
|
54
|
+
resize: 300,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
})
|
|
59
|
+
: attachmentConfig,
|
|
60
|
+
drive: defineDriveConfig({
|
|
61
|
+
default: 'fs',
|
|
62
|
+
services: {
|
|
63
|
+
fs: services.fs({
|
|
64
|
+
location: BASE_URL,
|
|
65
|
+
serveFiles: true,
|
|
66
|
+
routeBasePath: '/uploads',
|
|
67
|
+
visibility: 'public',
|
|
68
|
+
}),
|
|
69
|
+
},
|
|
70
|
+
}),
|
|
71
|
+
database: defineLucidConfig({
|
|
72
|
+
connection: 'sqlite',
|
|
73
|
+
connections: {
|
|
74
|
+
sqlite: {
|
|
75
|
+
client: 'better-sqlite3',
|
|
76
|
+
connection: {
|
|
77
|
+
filename: new URL('../db.sqlite', BASE_URL).pathname,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
}),
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
.withCoreConfig()
|
|
85
|
+
.withCoreProviders()
|
|
86
|
+
.create(BASE_URL, {
|
|
87
|
+
importer: IMPORTER,
|
|
88
|
+
})
|
|
89
|
+
.createApp('web');
|
|
90
|
+
await app.init();
|
|
91
|
+
await app.boot();
|
|
92
|
+
await mkdir(app.migrationsPath(), { recursive: true });
|
|
93
|
+
await copyFile(new URL('../fixtures/migrations/create_users_table.ts', import.meta.url), app.migrationsPath('create_users_table.ts'));
|
|
94
|
+
return app;
|
|
95
|
+
}
|
|
96
|
+
export async function initializeDatabase(app) {
|
|
97
|
+
const ace = await app.container.make('ace');
|
|
98
|
+
await ace.exec('migration:fresh', []);
|
|
99
|
+
// await seedDatabase()
|
|
100
|
+
}
|
|
101
|
+
// async function seedDatabase() {
|
|
102
|
+
// const { default: User } = await import('./fixtures/models/user.js')
|
|
103
|
+
// await User.createMany([{ name: 'AdonisJS' }, { name: 'Jeremy' }])
|
|
104
|
+
// }
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import { test } from '@japa/runner';
|
|
8
|
+
import { createApp } from './helpers/app.js';
|
|
9
|
+
import { UserFactory } from './fixtures/factories/user.js';
|
|
10
|
+
test.group('options', () => {
|
|
11
|
+
test('with default values', async ({ assert, cleanup }) => {
|
|
12
|
+
const app = await createApp();
|
|
13
|
+
cleanup(() => {
|
|
14
|
+
app.terminate();
|
|
15
|
+
});
|
|
16
|
+
const user = await UserFactory.create();
|
|
17
|
+
assert.deepEqual(user.avatar.options, {
|
|
18
|
+
disk: undefined,
|
|
19
|
+
folder: 'uploads',
|
|
20
|
+
preComputeUrl: false,
|
|
21
|
+
variants: [],
|
|
22
|
+
meta: true,
|
|
23
|
+
rename: true,
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
test('with config values', async ({ assert, cleanup }) => {
|
|
27
|
+
const app = await createApp({
|
|
28
|
+
preComputeUrl: true,
|
|
29
|
+
meta: false,
|
|
30
|
+
rename: false,
|
|
31
|
+
});
|
|
32
|
+
cleanup(() => {
|
|
33
|
+
app.terminate();
|
|
34
|
+
});
|
|
35
|
+
const user = await UserFactory.create();
|
|
36
|
+
assert.deepEqual(user.avatar.options, {
|
|
37
|
+
disk: undefined,
|
|
38
|
+
folder: 'uploads',
|
|
39
|
+
preComputeUrl: true,
|
|
40
|
+
variants: [],
|
|
41
|
+
meta: false,
|
|
42
|
+
rename: false,
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
test('with model values', async ({ assert, cleanup }) => {
|
|
46
|
+
const app = await createApp();
|
|
47
|
+
const attachmentManager = await app.container.make('jrmc.attachment');
|
|
48
|
+
cleanup(() => {
|
|
49
|
+
app.terminate();
|
|
50
|
+
});
|
|
51
|
+
const avatar2 = attachmentManager.createFromDbResponse(JSON.stringify({
|
|
52
|
+
size: 1440,
|
|
53
|
+
name: 'foo123.jpg',
|
|
54
|
+
originalName: 'foo.jpg',
|
|
55
|
+
extname: 'jpg',
|
|
56
|
+
mimeType: 'image/jpg',
|
|
57
|
+
}));
|
|
58
|
+
const user = await UserFactory.merge({ avatar2 }).create();
|
|
59
|
+
assert.deepEqual(user.avatar2.options, {
|
|
60
|
+
disk: 's3',
|
|
61
|
+
folder: 'avatar',
|
|
62
|
+
preComputeUrl: true,
|
|
63
|
+
variants: [],
|
|
64
|
+
meta: false,
|
|
65
|
+
rename: false,
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
test('with priority 1/model 2/config 3/default values', async ({ assert, cleanup }) => {
|
|
69
|
+
const app = await createApp({
|
|
70
|
+
preComputeUrl: false,
|
|
71
|
+
meta: true,
|
|
72
|
+
rename: true,
|
|
73
|
+
});
|
|
74
|
+
const attachmentManager = await app.container.make('jrmc.attachment');
|
|
75
|
+
cleanup(() => {
|
|
76
|
+
app.terminate();
|
|
77
|
+
});
|
|
78
|
+
const avatar2 = attachmentManager.createFromDbResponse(JSON.stringify({
|
|
79
|
+
size: 1440,
|
|
80
|
+
name: 'foo123.jpg',
|
|
81
|
+
originalName: 'foo.jpg',
|
|
82
|
+
extname: 'jpg',
|
|
83
|
+
mimeType: 'image/jpg',
|
|
84
|
+
}));
|
|
85
|
+
const user = await UserFactory.merge({ avatar2 }).create();
|
|
86
|
+
assert.deepEqual(user.avatar2.options, {
|
|
87
|
+
disk: 's3',
|
|
88
|
+
folder: 'avatar',
|
|
89
|
+
preComputeUrl: true,
|
|
90
|
+
variants: [],
|
|
91
|
+
meta: false,
|
|
92
|
+
rename: false,
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
test('with rename options', async ({ assert, cleanup }) => {
|
|
96
|
+
const app = await createApp({
|
|
97
|
+
rename: true,
|
|
98
|
+
});
|
|
99
|
+
const attachmentManager = await app.container.make('jrmc.attachment');
|
|
100
|
+
cleanup(() => {
|
|
101
|
+
app.terminate();
|
|
102
|
+
});
|
|
103
|
+
const avatar2 = attachmentManager.createFromDbResponse(JSON.stringify({
|
|
104
|
+
size: 1440,
|
|
105
|
+
name: 'foo123.jpg',
|
|
106
|
+
originalName: 'foo.jpg',
|
|
107
|
+
extname: 'jpg',
|
|
108
|
+
mimeType: 'image/jpg',
|
|
109
|
+
}));
|
|
110
|
+
const user = await UserFactory.merge({ avatar2 }).create();
|
|
111
|
+
const data = user.serialize();
|
|
112
|
+
assert.notEqual(data.avatar.name, 'avatar.jpg');
|
|
113
|
+
assert.equal(data.avatar2.name, 'foo.jpg');
|
|
114
|
+
});
|
|
115
|
+
test('with meta options', async ({ assert, cleanup }) => {
|
|
116
|
+
const app = await createApp({
|
|
117
|
+
meta: true,
|
|
118
|
+
});
|
|
119
|
+
cleanup(() => {
|
|
120
|
+
app.terminate();
|
|
121
|
+
});
|
|
122
|
+
const user = await UserFactory.create();
|
|
123
|
+
const data = user.serialize();
|
|
124
|
+
assert.deepEqual(data.avatar.meta, { dimension: { width: 1920, height: 1313 } });
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jrmc/adonis-attachment
|
|
3
|
+
*
|
|
4
|
+
* @license MIT
|
|
5
|
+
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
|
|
6
|
+
*/
|
|
7
|
+
import { test } from '@japa/runner';
|
|
8
|
+
import { createApp } from './helpers/app.js';
|
|
9
|
+
import { UserFactory } from './fixtures/factories/user_with_variants.js';
|
|
10
|
+
test.group('variants', () => {
|
|
11
|
+
test('generation', async ({ assert, cleanup }) => {
|
|
12
|
+
const app = await createApp();
|
|
13
|
+
cleanup(() => {
|
|
14
|
+
app.terminate();
|
|
15
|
+
});
|
|
16
|
+
const user = await UserFactory.create();
|
|
17
|
+
const data = await user.serialize();
|
|
18
|
+
assert.isNotNull(data.avatar.thumbnail);
|
|
19
|
+
assert.isNotNull(data.avatar.medium);
|
|
20
|
+
}).timeout(10_000);
|
|
21
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jrmc/adonis-attachment",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Turn any field on your Lucid model to an attachment data type",
|
|
6
6
|
"engines": {
|
|
@@ -46,7 +46,9 @@
|
|
|
46
46
|
"format": "prettier --write .",
|
|
47
47
|
"docs:dev": "vitepress dev docs",
|
|
48
48
|
"docs:build": "vitepress build docs",
|
|
49
|
-
"docs:preview": "vitepress preview docs"
|
|
49
|
+
"docs:preview": "vitepress preview docs",
|
|
50
|
+
"test": "c8 npm run quick:test",
|
|
51
|
+
"quick:test": "node --import=./tsnode.esm.js --enable-source-maps bin/test.ts"
|
|
50
52
|
},
|
|
51
53
|
"exports": {
|
|
52
54
|
".": "./build/index.js",
|
|
@@ -63,22 +65,36 @@
|
|
|
63
65
|
},
|
|
64
66
|
"dependencies": {
|
|
65
67
|
"@poppinss/defer": "^1.1.0",
|
|
66
|
-
"exifreader": "^4.
|
|
67
|
-
"file-type": "^19.
|
|
68
|
+
"exifreader": "^4.25.0",
|
|
69
|
+
"file-type": "^19.6.0"
|
|
68
70
|
},
|
|
69
71
|
"devDependencies": {
|
|
70
|
-
"@adonisjs/assembler": "^7.
|
|
71
|
-
"@adonisjs/core": "^6.
|
|
72
|
+
"@adonisjs/assembler": "^7.8.2",
|
|
73
|
+
"@adonisjs/core": "^6.17.0",
|
|
72
74
|
"@adonisjs/drive": "^3.2.0",
|
|
73
|
-
"@adonisjs/lucid": "^
|
|
74
|
-
"@adonisjs/prettier-config": "^1.
|
|
75
|
-
"@adonisjs/tsconfig": "^1.
|
|
76
|
-
"@
|
|
75
|
+
"@adonisjs/lucid": "^21.5.1",
|
|
76
|
+
"@adonisjs/prettier-config": "^1.4.0",
|
|
77
|
+
"@adonisjs/tsconfig": "^1.4.0",
|
|
78
|
+
"@japa/assert": "^3.0.0",
|
|
79
|
+
"@japa/expect-type": "^2.0.2",
|
|
80
|
+
"@japa/file-system": "^2.3.0",
|
|
81
|
+
"@japa/plugin-adonisjs": "^3.0.1",
|
|
82
|
+
"@japa/runner": "^3.1.4",
|
|
83
|
+
"@poppinss/utils": "^6.8.3",
|
|
84
|
+
"@swc/core": "^1.10.1",
|
|
85
|
+
"@types/luxon": "^3.4.2",
|
|
86
|
+
"@types/node": "^22.10.2",
|
|
87
|
+
"@types/sinon": "^17.0.3",
|
|
88
|
+
"better-sqlite3": "^11.7.0",
|
|
89
|
+
"c8": "^10.1.3",
|
|
77
90
|
"copyfiles": "^2.4.1",
|
|
78
|
-
"del-cli": "^
|
|
79
|
-
"
|
|
80
|
-
"
|
|
81
|
-
"
|
|
91
|
+
"del-cli": "^6.0.0",
|
|
92
|
+
"flydrive": "^1.1.0",
|
|
93
|
+
"luxon": "^3.5.0",
|
|
94
|
+
"prettier": "^3.4.2",
|
|
95
|
+
"ts-node": "^10.9.2",
|
|
96
|
+
"typescript": "^5.7.2",
|
|
97
|
+
"vitepress": "^1.5.0"
|
|
82
98
|
},
|
|
83
99
|
"peerDependencies": {
|
|
84
100
|
"@adonisjs/core": "^6.12.1",
|
|
@@ -86,6 +102,15 @@
|
|
|
86
102
|
"@adonisjs/lucid": "^20.6.0 || ^21.0.0"
|
|
87
103
|
},
|
|
88
104
|
"prettier": "@adonisjs/prettier-config",
|
|
105
|
+
"c8": {
|
|
106
|
+
"reporter": [
|
|
107
|
+
"text",
|
|
108
|
+
"html"
|
|
109
|
+
],
|
|
110
|
+
"exclude": [
|
|
111
|
+
"tests/**"
|
|
112
|
+
]
|
|
113
|
+
},
|
|
89
114
|
"publishConfig": {
|
|
90
115
|
"access": "public",
|
|
91
116
|
"tag": "latest"
|