|
1 | 1 | import type { UmbImagingModel } from './types.js';
|
2 | 2 | import { UmbImagingServerDataSource } from './imaging.server.data.js';
|
3 |
| -import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api'; |
| 3 | +import { UMB_IMAGING_STORE_CONTEXT } from './imaging.store.token.js'; |
4 | 4 | import { ImageCropModeModel } from '@umbraco-cms/backoffice/external/backend-api';
|
| 5 | +import { UmbRepositoryBase } from '@umbraco-cms/backoffice/repository'; |
5 | 6 | import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
|
6 | 7 | import type { UmbApi } from '@umbraco-cms/backoffice/extension-api';
|
| 8 | +import type { UmbMediaUrlModel } from '@umbraco-cms/backoffice/media'; |
7 | 9 |
|
8 |
| -export class UmbImagingRepository extends UmbControllerBase implements UmbApi { |
| 10 | +export class UmbImagingRepository extends UmbRepositoryBase implements UmbApi { |
| 11 | + #dataStore?: typeof UMB_IMAGING_STORE_CONTEXT.TYPE; |
9 | 12 | #itemSource: UmbImagingServerDataSource;
|
10 | 13 |
|
11 | 14 | constructor(host: UmbControllerHost) {
|
12 | 15 | super(host);
|
13 | 16 | this.#itemSource = new UmbImagingServerDataSource(host);
|
| 17 | + |
| 18 | + this.consumeContext(UMB_IMAGING_STORE_CONTEXT, (instance) => { |
| 19 | + this.#dataStore = instance; |
| 20 | + }); |
14 | 21 | }
|
15 | 22 |
|
16 | 23 | /**
|
17 | 24 | * Requests the items for the given uniques
|
18 | 25 | * @param {Array<string>} uniques
|
19 |
| - * @return {*} |
20 | 26 | * @memberof UmbImagingRepository
|
21 | 27 | */
|
22 |
| - async requestResizedItems(uniques: Array<string>, imagingModel?: UmbImagingModel) { |
| 28 | + async requestResizedItems( |
| 29 | + uniques: Array<string>, |
| 30 | + imagingModel?: UmbImagingModel, |
| 31 | + ): Promise<{ data: UmbMediaUrlModel[] }> { |
23 | 32 | if (!uniques.length) throw new Error('Uniques are missing');
|
| 33 | + if (!this.#dataStore) throw new Error('Data store is missing'); |
| 34 | + |
| 35 | + const urls = new Map<string, string>(); |
| 36 | + |
| 37 | + for (const unique of uniques) { |
| 38 | + const existingCrop = this.#dataStore.getCrop(unique, imagingModel); |
| 39 | + if (existingCrop) { |
| 40 | + urls.set(unique, existingCrop); |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + const { data: urlModels, error } = await this.#itemSource.getItems([unique], imagingModel); |
| 45 | + |
| 46 | + if (error) { |
| 47 | + console.error('[UmbImagingRepository] Error fetching items', error); |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + if (urlModels?.[0].url) { |
| 52 | + const url = urlModels[0].url; |
| 53 | + this.#dataStore.addCrop(unique, url, imagingModel); |
| 54 | + urls.set(unique, url); |
| 55 | + } |
| 56 | + } |
24 | 57 |
|
25 |
| - const { data, error: _error } = await this.#itemSource.getItems(uniques, imagingModel); |
26 |
| - const error: any = _error; |
27 |
| - return { data, error }; |
| 58 | + return { data: Array.from(urls).map(([unique, url]) => ({ unique, url })) }; |
28 | 59 | }
|
29 | 60 |
|
30 | 61 | /**
|
31 | 62 | * Requests the thumbnail URLs for the given uniques
|
32 | 63 | * @param {Array<string>} uniques
|
33 | 64 | * @param {number} height
|
34 | 65 | * @param {number} width
|
35 |
| - * @returns {*} |
| 66 | + * @param {ImageCropModeModel} mode - The crop mode |
36 | 67 | * @memberof UmbImagingRepository
|
37 | 68 | */
|
38 |
| - async requestThumbnailUrls(uniques: Array<string>, height: number, width: number) { |
39 |
| - const imagingModel = { height: height, width: width, mode: ImageCropModeModel.MIN }; |
40 |
| - return await this.requestResizedItems(uniques, imagingModel); |
| 69 | + async requestThumbnailUrls(uniques: Array<string>, height: number, width: number, mode = ImageCropModeModel.MIN) { |
| 70 | + const imagingModel = { height: height, width: width, mode }; |
| 71 | + return this.requestResizedItems(uniques, imagingModel); |
41 | 72 | }
|
42 | 73 | }
|
43 | 74 |
|
|
0 commit comments