@jrmc/adonis-attachment 5.0.0-beta.2 → 5.0.0-beta.3

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.
Files changed (36) hide show
  1. package/build/index.d.ts +0 -1
  2. package/build/index.d.ts.map +1 -1
  3. package/build/index.js +0 -1
  4. package/build/providers/attachment_provider.d.ts.map +1 -1
  5. package/build/providers/attachment_provider.js +2 -1
  6. package/build/src/attachment_manager.d.ts +4 -2
  7. package/build/src/attachment_manager.d.ts.map +1 -1
  8. package/build/src/attachment_manager.js +3 -1
  9. package/build/src/attachments/attachment.d.ts +1 -1
  10. package/build/src/attachments/attachment.js +1 -1
  11. package/build/src/controllers/attachments_controller.d.ts.map +1 -1
  12. package/build/src/controllers/attachments_controller.js +74 -74
  13. package/build/src/decorators/attachment.d.ts +3 -3
  14. package/build/src/decorators/attachment.d.ts.map +1 -1
  15. package/build/src/services/record_with_attachment.d.ts.map +1 -1
  16. package/build/src/services/record_with_attachment.js +22 -9
  17. package/build/src/services/variant/variant_generator.d.ts +18 -0
  18. package/build/src/services/variant/variant_generator.d.ts.map +1 -0
  19. package/build/src/services/variant/variant_generator.js +91 -0
  20. package/build/src/services/variant/variant_persister.d.ts +17 -0
  21. package/build/src/services/variant/variant_persister.d.ts.map +1 -0
  22. package/build/src/services/variant/variant_persister.js +54 -0
  23. package/build/src/services/variant/variant_purger.d.ts +9 -0
  24. package/build/src/services/variant/variant_purger.d.ts.map +1 -0
  25. package/build/src/services/variant/variant_purger.js +42 -0
  26. package/build/src/services/variant_service.d.ts +7 -0
  27. package/build/src/services/variant_service.d.ts.map +1 -0
  28. package/build/src/services/variant_service.js +52 -0
  29. package/build/src/types/attachment.d.ts +3 -3
  30. package/build/src/types/attachment.d.ts.map +1 -1
  31. package/build/stubs/config.stub +102 -4
  32. package/build/tsconfig.tsbuildinfo +1 -1
  33. package/package.json +4 -8
  34. package/build/src/converter_manager.d.ts +0 -13
  35. package/build/src/converter_manager.d.ts.map +0 -1
  36. package/build/src/converter_manager.js +0 -121
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jrmc/adonis-attachment",
3
- "version": "5.0.0-beta.2",
3
+ "version": "5.0.0-beta.3",
4
4
  "type": "module",
5
5
  "description": "Turn any field on your Lucid model to an attachment data type",
6
6
  "engines": {
@@ -76,6 +76,7 @@
76
76
  "@adonisjs/assembler": "^7.8.2",
77
77
  "@adonisjs/core": "^6.17.1",
78
78
  "@adonisjs/drive": "^3.2.0",
79
+ "@adonisjs/lock": "^1.1.1",
79
80
  "@adonisjs/lucid": "^21.6.0",
80
81
  "@adonisjs/prettier-config": "^1.4.0",
81
82
  "@adonisjs/tsconfig": "^1.4.0",
@@ -106,13 +107,8 @@
106
107
  "peerDependencies": {
107
108
  "@adonisjs/core": "^6.12.1",
108
109
  "@adonisjs/drive": "^3.2.0",
109
- "@adonisjs/lucid": "^20.6.0 || ^21.0.0",
110
- "sharp": "^0.33.0"
111
- },
112
- "peerDependenciesMeta": {
113
- "sharp": {
114
- "optional": true
115
- }
110
+ "@adonisjs/lock": "^1.1.1",
111
+ "@adonisjs/lucid": "^20.6.0 || ^21.0.0"
116
112
  },
117
113
  "prettier": "@adonisjs/prettier-config",
118
114
  "c8": {
@@ -1,13 +0,0 @@
1
- import type { Converter, ConverterInitializeAttributes } from './types/converter.js';
2
- import type { Attachment, Variant } from './types/attachment.js';
3
- export declare class ConverterManager {
4
- #private;
5
- constructor({ record, attributeName, options, filters }: ConverterInitializeAttributes);
6
- run(): Promise<void>;
7
- static generate({ key, attachment, converter }: {
8
- key: string;
9
- attachment: Attachment;
10
- converter: Converter;
11
- }): Promise<Variant | undefined>;
12
- }
13
- //# sourceMappingURL=converter_manager.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"converter_manager.d.ts","sourceRoot":"","sources":["../../src/converter_manager.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,6BAA6B,EAAE,MAAM,sBAAsB,CAAA;AACpF,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAgB,MAAM,uBAAuB,CAAA;AAS9E,qBAAa,gBAAgB;;gBAQf,EAAE,MAAM,EAAE,aAAa,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,6BAA6B;IAOhF,GAAG;WAyCI,QAAQ,CAAC,EAAE,GAAG,EAAE,UAAU,EAAE,SAAS,EAAE,EAAG;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,UAAU,CAAC;QAAC,SAAS,EAAE,SAAS,CAAA;KAAE;CA6FrH"}
@@ -1,121 +0,0 @@
1
- import logger from '@adonisjs/core/services/logger';
2
- import string from '@adonisjs/core/helpers/string';
3
- import db from '@adonisjs/lucid/services/db';
4
- import attachmentManager from '../services/main.js';
5
- import { streamToTempFile } from './utils/helpers.js';
6
- export class ConverterManager {
7
- #record;
8
- #attributeName;
9
- #options;
10
- #filters;
11
- constructor({ record, attributeName, options, filters }) {
12
- this.#record = record;
13
- this.#attributeName = attributeName;
14
- this.#options = options;
15
- this.#filters = filters;
16
- }
17
- async run() {
18
- const attachments = this.#record.getAttachments({
19
- attributeName: this.#attributeName,
20
- });
21
- const variants = [];
22
- await this.#purge(attachments);
23
- if (!attachments || !this.#options.variants || !this.#options.variants.length) {
24
- return;
25
- }
26
- for await (const key of this.#options.variants) {
27
- if (this.#filters?.variants !== undefined && !this.#filters?.variants?.includes(key)) {
28
- continue;
29
- }
30
- const converter = (await attachmentManager.getConverter(key));
31
- if (converter) {
32
- for await (const attachment of attachments) {
33
- const variant = await ConverterManager.generate({
34
- key,
35
- attachment,
36
- converter
37
- });
38
- if (variant) {
39
- variants.push(variant);
40
- }
41
- }
42
- }
43
- }
44
- return this.#commit(attachments, () => {
45
- for (let i = 0; i < variants.length; i++) {
46
- attachmentManager.remove(variants[i]);
47
- }
48
- });
49
- }
50
- static async generate({ key, attachment, converter }) {
51
- let input = attachment.input;
52
- if (!input) {
53
- input = await streamToTempFile(await attachment.getStream());
54
- }
55
- const output = await converter.handle({
56
- input,
57
- options: converter.options,
58
- });
59
- if (output === undefined) {
60
- // throw new errors.E_CANNOT_PATH_BY_CONVERTER()
61
- return;
62
- }
63
- const variant = await attachment.createVariant(key, output);
64
- if (converter.options.blurhash) {
65
- if ((typeof converter.options.blurhash !== 'boolean' &&
66
- converter.options.blurhash.enabled === true) ||
67
- converter.options.blurhash === true) {
68
- try {
69
- const options = typeof converter.options.blurhash !== 'boolean'
70
- ? converter.options.blurhash
71
- : undefined;
72
- await variant.generateBlurhash(options);
73
- }
74
- catch (error) {
75
- logger.error(error.message);
76
- }
77
- }
78
- }
79
- await attachmentManager.write(variant);
80
- return variant;
81
- }
82
- async #commit(attachments, rollback) {
83
- const Model = this.#record.row.constructor;
84
- const id = this.#record.row.$attributes['id'];
85
- const data = {};
86
- const index = string.snakeCase(this.#attributeName);
87
- if (Array.isArray(this.#record.row.$original[this.#attributeName])) {
88
- data[index] = JSON.stringify(attachments.map((att) => att.toObject()));
89
- }
90
- else {
91
- data[index] = JSON.stringify(attachments[0].toObject());
92
- }
93
- const trx = await db.transaction();
94
- trx.after('rollback', rollback);
95
- try {
96
- await trx.query().from(Model.table).where('id', id).update(data);
97
- return trx.commit();
98
- }
99
- catch (error) {
100
- return trx.rollback();
101
- }
102
- }
103
- async #purge(attachments) {
104
- return Promise.all(attachments.map(async (attachment) => {
105
- if (attachment.variants) {
106
- await Promise.all(attachment.variants.map(async (variant) => {
107
- if (this.#filters?.variants !== undefined && !this.#filters?.variants?.includes(variant.key)) {
108
- return;
109
- }
110
- return attachmentManager.remove(variant);
111
- }));
112
- if (this.#filters?.variants !== undefined) {
113
- attachment.variants = await attachment.variants.filter((variant) => !this.#filters?.variants?.includes(variant.key));
114
- }
115
- else {
116
- attachment.variants = [];
117
- }
118
- }
119
- }));
120
- }
121
- }