@adonis-agora/media 0.4.0 → 0.5.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @adonis-agora/media
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#6](https://github.com/DavideCarvalho/adonis-media/pull/6) [`256a11e`](https://github.com/DavideCarvalho/adonis-media/commit/256a11e034118435b2c29a1b7ac7c0c6c05ac5b6) Thanks [@DavideCarvalho](https://github.com/DavideCarvalho)! - Add the `@adonis-agora/media/single-file` helper (`storeSingleFile` / `removeSingleFile` / `isSingleFileStoreAvailable`) for storing exactly one file per owner through a `single: true` collection, returning the stable public URL (plus an optional `thumb` conversion URL). Lets other packages delegate single-file uploads such as avatars to media without taking a hard dependency on it.
8
+
3
9
  ## 0.4.0
4
10
 
5
11
  ### Minor Changes
@@ -0,0 +1,58 @@
1
+ import { MediaManager } from './media_manager.js';
2
+ /**
3
+ * Store a single file for an owner and return stable public URLs. This is a thin, generic seam over
4
+ * {@link MediaManager} for callers (e.g. another Agora package storing an avatar) that only need to
5
+ * "replace this owner's one file and give me a URL", without touching the fuller library surface.
6
+ *
7
+ * Single-slot replacement is a property of the collection: the `collection` named here must be
8
+ * configured `single: true` in the app's `config/media.ts`. It is NOT an attach flag.
9
+ */
10
+ export interface StoreSingleFileInput {
11
+ ownerType: string;
12
+ ownerId: string;
13
+ /** Must be configured `single: true` in the app's media config. */
14
+ collection: string;
15
+ fileName: string;
16
+ mimeType: string;
17
+ contents: Buffer;
18
+ }
19
+ export interface StoredSingleFile {
20
+ /** Public URL of the stored original. */
21
+ url: string;
22
+ /** Public URL of the `thumb` conversion, or `null` when that conversion isn't configured. */
23
+ thumbUrl: string | null;
24
+ }
25
+ export interface RemoveSingleFileInput {
26
+ ownerType: string;
27
+ ownerId: string;
28
+ collection: string;
29
+ }
30
+ /**
31
+ * Core of {@link storeSingleFile} driven by an explicit {@link MediaManager}, so tests can exercise
32
+ * it with an in-memory manager without booting an app. Attaches the file to the owner's collection
33
+ * (single-file replacement comes from the collection config) and resolves the original URL plus,
34
+ * best-effort, a `thumb` conversion URL.
35
+ */
36
+ export declare function storeSingleFileWith(manager: MediaManager, input: StoreSingleFileInput): Promise<StoredSingleFile>;
37
+ /**
38
+ * Core of {@link removeSingleFile} driven by an explicit {@link MediaManager}. Empties the owner's
39
+ * collection by deleting every record it holds (a single-file collection holds at most one).
40
+ */
41
+ export declare function removeSingleFileWith(manager: MediaManager, input: RemoveSingleFileInput): Promise<void>;
42
+ /**
43
+ * Store a single file for an owner via the app-bound {@link MediaManager}. Resolves the manager from
44
+ * the container, then delegates to {@link storeSingleFileWith}. The `collection` must be configured
45
+ * `single: true` in `config/media.ts` for prior files to be replaced.
46
+ */
47
+ export declare function storeSingleFile(input: StoreSingleFileInput): Promise<StoredSingleFile>;
48
+ /**
49
+ * Remove the owner's file in a collection via the app-bound {@link MediaManager}. Resolves the
50
+ * manager from the container, then delegates to {@link removeSingleFileWith}.
51
+ */
52
+ export declare function removeSingleFile(input: RemoveSingleFileInput): Promise<void>;
53
+ /**
54
+ * Whether the single-file store is usable — i.e. the {@link MediaManager} is bound in the app
55
+ * container. Callers use this to feature-detect media before routing a store through it.
56
+ */
57
+ export declare function isSingleFileStoreAvailable(): Promise<boolean>;
58
+ //# sourceMappingURL=single_file_store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"single_file_store.d.ts","sourceRoot":"","sources":["../../src/single_file_store.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD;;;;;;;GAOG;AACH,MAAM,WAAW,oBAAoB;IACnC,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,mEAAmE;IACnE,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,gBAAgB;IAC/B,yCAAyC;IACzC,GAAG,EAAE,MAAM,CAAC;IACZ,6FAA6F;IAC7F,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB;AAED,MAAM,WAAW,qBAAqB;IACpC,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;;;;GAKG;AACH,wBAAsB,mBAAmB,CACvC,OAAO,EAAE,YAAY,EACrB,KAAK,EAAE,oBAAoB,GAC1B,OAAO,CAAC,gBAAgB,CAAC,CAoB3B;AAED;;;GAGG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,YAAY,EACrB,KAAK,EAAE,qBAAqB,GAC3B,OAAO,CAAC,IAAI,CAAC,CAGf;AAED;;;;GAIG;AACH,wBAAsB,eAAe,CAAC,KAAK,EAAE,oBAAoB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAG5F;AAED;;;GAGG;AACH,wBAAsB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAGlF;AAED;;;GAGG;AACH,wBAAsB,0BAA0B,IAAI,OAAO,CAAC,OAAO,CAAC,CAEnE"}
@@ -0,0 +1,61 @@
1
+ import app from '@adonisjs/core/services/app';
2
+ import { MediaManager } from './media_manager.js';
3
+ /**
4
+ * Core of {@link storeSingleFile} driven by an explicit {@link MediaManager}, so tests can exercise
5
+ * it with an in-memory manager without booting an app. Attaches the file to the owner's collection
6
+ * (single-file replacement comes from the collection config) and resolves the original URL plus,
7
+ * best-effort, a `thumb` conversion URL.
8
+ */
9
+ export async function storeSingleFileWith(manager, input) {
10
+ const record = await manager.library.for(input.ownerType, input.ownerId).attach({
11
+ collection: input.collection,
12
+ fileName: input.fileName,
13
+ mimeType: input.mimeType,
14
+ contents: input.contents,
15
+ });
16
+ const url = await manager.library.url(record.id);
17
+ // The `thumb` conversion is optional: if the collection doesn't define it (or it can't be
18
+ // generated), there's simply no thumbnail — return null rather than failing the whole store.
19
+ let thumbUrl = null;
20
+ try {
21
+ thumbUrl = await manager.library.url(record.id, 'thumb');
22
+ }
23
+ catch {
24
+ thumbUrl = null;
25
+ }
26
+ return { url, thumbUrl };
27
+ }
28
+ /**
29
+ * Core of {@link removeSingleFile} driven by an explicit {@link MediaManager}. Empties the owner's
30
+ * collection by deleting every record it holds (a single-file collection holds at most one).
31
+ */
32
+ export async function removeSingleFileWith(manager, input) {
33
+ const records = await manager.library.for(input.ownerType, input.ownerId).list(input.collection);
34
+ for (const record of records)
35
+ await manager.library.delete(record.id);
36
+ }
37
+ /**
38
+ * Store a single file for an owner via the app-bound {@link MediaManager}. Resolves the manager from
39
+ * the container, then delegates to {@link storeSingleFileWith}. The `collection` must be configured
40
+ * `single: true` in `config/media.ts` for prior files to be replaced.
41
+ */
42
+ export async function storeSingleFile(input) {
43
+ const manager = await app.container.make(MediaManager);
44
+ return storeSingleFileWith(manager, input);
45
+ }
46
+ /**
47
+ * Remove the owner's file in a collection via the app-bound {@link MediaManager}. Resolves the
48
+ * manager from the container, then delegates to {@link removeSingleFileWith}.
49
+ */
50
+ export async function removeSingleFile(input) {
51
+ const manager = await app.container.make(MediaManager);
52
+ return removeSingleFileWith(manager, input);
53
+ }
54
+ /**
55
+ * Whether the single-file store is usable — i.e. the {@link MediaManager} is bound in the app
56
+ * container. Callers use this to feature-detect media before routing a store through it.
57
+ */
58
+ export async function isSingleFileStoreAvailable() {
59
+ return app.container.hasBinding(MediaManager);
60
+ }
61
+ //# sourceMappingURL=single_file_store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"single_file_store.js","sourceRoot":"","sources":["../../src/single_file_store.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,6BAA6B,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAiClD;;;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C/D,MAAM,OAAO,GAAG,MAAM,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACvD,OAAO,mBAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;AAC7C,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,KAA4B;IACjE,MAAM,OAAO,GAAG,MAAM,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACvD,OAAO,oBAAoB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;AAC9C,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B;IAC9C,OAAO,GAAG,CAAC,SAAS,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC;AAChD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adonis-agora/media",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Media-library for AdonisJS — owner collections (MIME whitelist, single-file replace, ordering), image conversions (eager/lazy), and column attachments, all on top of @adonisjs/drive. Pluggable MediaStore (in-memory + Lucid) and ImageProcessor (sharp). Part of the Agora ecosystem.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -23,6 +23,11 @@
23
23
  "import": "./dist/src/services/main.js",
24
24
  "default": "./dist/src/services/main.js"
25
25
  },
26
+ "./single-file": {
27
+ "types": "./dist/src/single_file_store.d.ts",
28
+ "import": "./dist/src/single_file_store.js",
29
+ "default": "./dist/src/single_file_store.js"
30
+ },
26
31
  "./media_provider": {
27
32
  "types": "./dist/providers/media_provider.d.ts",
28
33
  "import": "./dist/providers/media_provider.js",