Re-added file-uploader function body.

Moved file-uploader addAssetToAlbum call so as not to invoke it for every single file.
This commit is contained in:
Art051
2024-05-13 20:36:52 +01:00
parent 4967577576
commit 4ecff84f37
3 changed files with 25 additions and 10 deletions

View File

@@ -25,13 +25,28 @@ import { get } from 'svelte/store';
import { handleError } from './handle-error'; import { handleError } from './handle-error';
export const addAssetsToAlbum = async (albumId: string, assetIds: string[]) => { export const addAssetsToAlbum = async (albumId: string, assetIds: string[]) => {
await addAssets({ const result = await addAssets({
id: albumId, id: albumId,
bulkIdsDto: { bulkIdsDto: {
ids: assetIds, ids: assetIds,
}, },
key: getKey(), key: getKey(),
}); });
const count = result.filter(({ success }) => success).length;
notificationController.show({
type: NotificationType.Info,
timeout: 5000,
message:
count > 0
? `Added ${count} asset${count === 1 ? '' : 's'} to the album`
: `Asset${assetIds.length === 1 ? ' was' : 's were'} already part of the album`,
button: {
text: 'View Album',
onClick() {
return goto(`${AppRoute.ALBUMS}/${albumId}`);
},
},
});
}; };
export const addAssetsToNewAlbum = async (albumName: string, assetIds: string[]) => { export const addAssetsToNewAlbum = async (albumName: string, assetIds: string[]) => {

View File

@@ -60,11 +60,17 @@ export const fileUploadHandler = async (files: File[], albumId: string | undefin
const name = file.name.toLowerCase(); const name = file.name.toLowerCase();
if (extensions.some((extension) => name.endsWith(extension))) { if (extensions.some((extension) => name.endsWith(extension))) {
uploadAssetsStore.addNewUploadAsset({ id: getDeviceAssetId(file), file, albumId }); uploadAssetsStore.addNewUploadAsset({ id: getDeviceAssetId(file), file, albumId });
promises.push(uploadExecutionQueue.addTask(() => fileUploader(file, albumId))); promises.push(uploadExecutionQueue.addTask(() => fileUploader(file)));
} }
} }
const results = await Promise.all(promises); const results = await Promise.all(promises);
const assetIds = results.filter((result): result is string => !!result);
if (albumId && assetIds.length > 0) {
await addAssetsToAlbum(albumId, assetIds);
}
return results.filter((result): result is string => !!result); return results.filter((result): result is string => !!result);
}; };
@@ -73,7 +79,7 @@ function getDeviceAssetId(asset: File) {
} }
// TODO: should probably use the @api SDK // TODO: should probably use the @api SDK
async function fileUploader(asset: File, albumId: string | undefined = undefined): Promise<string | undefined> { async function fileUploader(asset: File): Promise<string | undefined> {
const fileCreatedAt = new Date(asset.lastModified).toISOString(); const fileCreatedAt = new Date(asset.lastModified).toISOString();
const deviceAssetId = getDeviceAssetId(asset); const deviceAssetId = getDeviceAssetId(asset);
@@ -136,12 +142,6 @@ async function fileUploader(asset: File, albumId: string | undefined = undefined
uploadAssetsStore.successCounter.update((c) => c + 1); uploadAssetsStore.successCounter.update((c) => c + 1);
} }
if (albumId && assetId) {
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Adding to album...' });
await addAssetsToAlbum(albumId, [assetId]);
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Added to album' });
}
uploadAssetsStore.updateAsset(deviceAssetId, { state: duplicate ? UploadState.DUPLICATED : UploadState.DONE }); uploadAssetsStore.updateAsset(deviceAssetId, { state: duplicate ? UploadState.DUPLICATED : UploadState.DONE });
setTimeout(() => { setTimeout(() => {

View File

@@ -291,7 +291,7 @@
const count = results.filter(({ success }) => success).length; const count = results.filter(({ success }) => success).length;
notificationController.show({ notificationController.show({
type: NotificationType.Info, type: NotificationType.Info,
message: `Added ${count} asset${count === 1 ? '' : 's'}`, message: `Added ${count} asset${count > 1 ? 's' : ''} successfully, refresh the page to see newly added asset${count > 1 ? 's' : ''}.`,
}); });
await refreshAlbum(); await refreshAlbum();