Skip to content

Commit f4edfe5

Browse files
committed
fix: call each thumbnail individually
1 parent ba6fcea commit f4edfe5

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

src/packages/media/imaging/imaging.repository.ts

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,74 @@
11
import type { UmbImagingModel } from './types.js';
22
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';
44
import { ImageCropModeModel } from '@umbraco-cms/backoffice/external/backend-api';
5+
import { UmbRepositoryBase } from '@umbraco-cms/backoffice/repository';
56
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
67
import type { UmbApi } from '@umbraco-cms/backoffice/extension-api';
8+
import type { UmbMediaUrlModel } from '@umbraco-cms/backoffice/media';
79

8-
export class UmbImagingRepository extends UmbControllerBase implements UmbApi {
10+
export class UmbImagingRepository extends UmbRepositoryBase implements UmbApi {
11+
#dataStore?: typeof UMB_IMAGING_STORE_CONTEXT.TYPE;
912
#itemSource: UmbImagingServerDataSource;
1013

1114
constructor(host: UmbControllerHost) {
1215
super(host);
1316
this.#itemSource = new UmbImagingServerDataSource(host);
17+
18+
this.consumeContext(UMB_IMAGING_STORE_CONTEXT, (instance) => {
19+
this.#dataStore = instance;
20+
});
1421
}
1522

1623
/**
1724
* Requests the items for the given uniques
1825
* @param {Array<string>} uniques
19-
* @return {*}
2026
* @memberof UmbImagingRepository
2127
*/
22-
async requestResizedItems(uniques: Array<string>, imagingModel?: UmbImagingModel) {
28+
async requestResizedItems(
29+
uniques: Array<string>,
30+
imagingModel?: UmbImagingModel,
31+
): Promise<{ data: UmbMediaUrlModel[] }> {
2332
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+
}
2457

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 })) };
2859
}
2960

3061
/**
3162
* Requests the thumbnail URLs for the given uniques
3263
* @param {Array<string>} uniques
3364
* @param {number} height
3465
* @param {number} width
35-
* @returns {*}
66+
* @param {ImageCropModeModel} mode - The crop mode
3667
* @memberof UmbImagingRepository
3768
*/
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);
4172
}
4273
}
4374

0 commit comments

Comments
 (0)