@jrmc/adonis-attachment 3.3.0-beta.2 → 3.3.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.
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ encode(pixels: Uint8ClampedArray, width: number, height: number, componentX: number, componentY: number): Promise<string>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,6 @@
1
+ import { encode } from 'blurhash';
2
+ export default {
3
+ async encode(pixels, width, height, componentX, componentY) {
4
+ return encode(pixels, width, height, componentX, componentY);
5
+ },
6
+ };
@@ -129,6 +129,7 @@ export class Attachment extends AttachmentBase {
129
129
  mimeType: v.mimeType,
130
130
  meta: v.meta,
131
131
  size: v.size,
132
+ blurhash: v.blurhash,
132
133
  };
133
134
  });
134
135
  }
@@ -1,8 +1,9 @@
1
+ import logger from '@adonisjs/core/services/logger';
1
2
  import string from '@adonisjs/core/helpers/string';
2
3
  import db from '@adonisjs/lucid/services/db';
3
4
  import attachmentManager from '../services/main.js';
4
5
  import * as errors from './errors.js';
5
- import { encodeImageToBlurhash } from './utils/helpers.js';
6
+ import { imageToBlurhash } from './utils/helpers.js';
6
7
  export class ConverterManager {
7
8
  #record;
8
9
  #attributeName;
@@ -20,11 +21,11 @@ export class ConverterManager {
20
21
  const id = this.#record.model.$attributes['id'];
21
22
  const data = {};
22
23
  if (this.#options.variants) {
23
- for (const option of this.#options.variants) {
24
+ for await (const option of this.#options.variants) {
24
25
  const converter = (await attachmentManager.getConverter(option));
25
26
  if (attachments && converter) {
26
- for (let i = 0; i < attachments.length; i++) {
27
- const input = attachments[i].input;
27
+ for await (const attachment of attachments) {
28
+ const input = attachment.input;
28
29
  const output = await converter.handle({
29
30
  input,
30
31
  options: converter.options,
@@ -32,10 +33,21 @@ export class ConverterManager {
32
33
  if (output === undefined) {
33
34
  throw new errors.E_CANNOT_PATH_BY_CONVERTER();
34
35
  }
35
- const variant = await attachments[i].createVariant(option, output);
36
+ const variant = await attachment.createVariant(option, output);
36
37
  if (converter.options.blurhash) {
37
- const options = typeof converter.options.blurhash !== 'boolean' ? converter.options.blurhash : undefined;
38
- variant.blurhash = await encodeImageToBlurhash(variant.input, options);
38
+ if ((typeof converter.options.blurhash !== 'boolean' &&
39
+ converter.options.blurhash.enabled === true) ||
40
+ converter.options.blurhash === true) {
41
+ try {
42
+ const options = typeof converter.options.blurhash !== 'boolean'
43
+ ? converter.options.blurhash
44
+ : undefined;
45
+ variant.blurhash = await imageToBlurhash(variant.input, options);
46
+ }
47
+ catch (error) {
48
+ logger.error(error.message);
49
+ }
50
+ }
39
51
  }
40
52
  await attachmentManager.save(variant);
41
53
  }
@@ -58,10 +70,10 @@ export class ConverterManager {
58
70
  });
59
71
  try {
60
72
  await trx.query().from(Model.table).where('id', id).update(data);
61
- return await trx.commit();
73
+ return trx.commit();
62
74
  }
63
75
  catch (error) {
64
- return await trx.rollback();
76
+ return trx.rollback();
65
77
  }
66
78
  }
67
79
  }
@@ -106,7 +106,7 @@ export default class Record {
106
106
  * For all properties Attachment
107
107
  * Launch async generation variants
108
108
  */
109
- await Promise.allSettled(attachmentAttributeNames.map((name) => {
109
+ for await (const name of attachmentAttributeNames) {
110
110
  const record = this;
111
111
  attachmentManager.queue.push({
112
112
  name: `${this.#model.constructor.name}-${name}`,
@@ -124,7 +124,7 @@ export default class Record {
124
124
  }
125
125
  },
126
126
  });
127
- }));
127
+ }
128
128
  }
129
129
  async detach() {
130
130
  const attachmentAttributeNames = this.#getDirtyAttributeNamesOfAttachment();
@@ -214,7 +214,9 @@ export default class Record {
214
214
  const dirtyValue = this.#model.$dirty[name];
215
215
  const originalValue = this.#model.$original[name]; // if dirtyValue is null, check original type
216
216
  const isDirtyAttachment = dirtyValue instanceof Attachment ||
217
- (Array.isArray(dirtyValue) && dirtyValue.every((item) => item instanceof Attachment) && dirtyValue.length);
217
+ (Array.isArray(dirtyValue) &&
218
+ dirtyValue.every((item) => item instanceof Attachment) &&
219
+ dirtyValue.length);
218
220
  const isOriginalAttachment = originalValue instanceof Attachment ||
219
221
  (Array.isArray(originalValue) && originalValue.every((item) => item instanceof Attachment));
220
222
  return isDirtyAttachment || isOriginalAttachment;
@@ -24,8 +24,8 @@ export type ConverterAttributes = {
24
24
  };
25
25
  export type BlurhashOptions = {
26
26
  enabled: boolean;
27
- componentX: number;
28
- componentY: number;
27
+ componentX: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
28
+ componentY: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
29
29
  };
30
30
  type jpeg = {
31
31
  format: 'jpeg';
@@ -12,4 +12,4 @@ export declare function bufferToTempFile(input: Buffer): Promise<string>;
12
12
  export declare function streamToTempFile(input: NodeJS.ReadableStream): Promise<string>;
13
13
  export declare function downloadToTempFile(input: URL): Promise<string>;
14
14
  export declare function isBase64(str: string): boolean;
15
- export declare function encodeImageToBlurhash(input: Input, options?: BlurhashOptions): Promise<string>;
15
+ export declare function imageToBlurhash(input: Input, options?: BlurhashOptions): Promise<string>;
@@ -11,7 +11,7 @@ import fs from 'node:fs/promises';
11
11
  import { pipeline } from 'node:stream';
12
12
  import { promisify } from 'node:util';
13
13
  import { createWriteStream } from 'node:fs';
14
- import { encode } from 'blurhash';
14
+ import BlurhashAdapter from '../adapters/blurhash.js';
15
15
  import * as errors from '../errors.js';
16
16
  const streamPipeline = promisify(pipeline);
17
17
  export function cleanObject(obj) {
@@ -92,7 +92,7 @@ export function isBase64(str) {
92
92
  return false;
93
93
  }
94
94
  }
95
- export function encodeImageToBlurhash(input, options) {
95
+ export function imageToBlurhash(input, options) {
96
96
  const { componentX, componentY } = options || { componentX: 4, componentY: 4 };
97
97
  return new Promise(async (resolve, reject) => {
98
98
  try {
@@ -102,11 +102,10 @@ export function encodeImageToBlurhash(input, options) {
102
102
  .raw()
103
103
  .ensureAlpha()
104
104
  .toBuffer({ resolveWithObject: true });
105
- return resolve(encode(new Uint8ClampedArray(pixels), metadata.width, metadata.height, componentX, componentY));
105
+ const blurhash = BlurhashAdapter.encode(new Uint8ClampedArray(pixels), metadata.width, metadata.height, componentX, componentY);
106
+ return resolve(blurhash);
106
107
  }
107
108
  catch (error) {
108
- console.log('--------------');
109
- console.log(error);
110
109
  return reject(error);
111
110
  }
112
111
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jrmc/adonis-attachment",
3
- "version": "3.3.0-beta.2",
3
+ "version": "3.3.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": {
@@ -96,6 +96,7 @@
96
96
  "luxon": "^3.5.0",
97
97
  "prettier": "^3.4.2",
98
98
  "sharp": "^0.33.5",
99
+ "sinon": "^19.0.2",
99
100
  "ts-node": "^10.9.2",
100
101
  "typescript": "^5.7.3",
101
102
  "vitepress": "^1.5.0"