diff --git a/src/app/shared/services/image-store/image-store.service.ts b/src/app/shared/services/image-store/image-store.service.ts index da4f0273e..8bbbf6f7f 100644 --- a/src/app/shared/services/image-store/image-store.service.ts +++ b/src/app/shared/services/image-store/image-store.service.ts @@ -66,8 +66,24 @@ export class ImageStore { return result.data; } - async write(base64: string, mimeType: MimeType) { + async write( + base64: string, + mimeType: MimeType, + onWriteExistStrategy = OnWriteExistStrategy.REPLACE + ) { const index = await sha256WithBase64(base64); + await this.initialize(); + if (onWriteExistStrategy === OnWriteExistStrategy.REPLACE) { + return this._write(index, base64, mimeType); + } + const exists = await this.exists(index); + if (exists) { + return index; + } + return this._write(index, base64, mimeType); + } + + private async _write(index: string, base64: string, mimeType: MimeType) { await this.initialize(); return this.mutex.runExclusive(async () => { const imageExtension = await this.setImageExtension(index, mimeType); @@ -226,3 +242,8 @@ interface Thumbnail extends Tuple { readonly imageIndex: string; readonly thumbnailIndex: string; } + +export const enum OnWriteExistStrategy { + IGNORE, + REPLACE, +} diff --git a/src/app/shared/services/repositories/proof/proof.ts b/src/app/shared/services/repositories/proof/proof.ts index bb39ff718..e0223b648 100644 --- a/src/app/shared/services/repositories/proof/proof.ts +++ b/src/app/shared/services/repositories/proof/proof.ts @@ -3,7 +3,10 @@ import { sortObjectDeeplyByKey } from '../../../../utils/immutable/immutable'; import { MimeType } from '../../../../utils/mime-type'; import { toDataUrl } from '../../../../utils/url'; import { Tuple } from '../../database/table/table'; -import { ImageStore } from '../../image-store/image-store.service'; +import { + ImageStore, + OnWriteExistStrategy, +} from '../../image-store/image-store.service'; export class Proof { diaBackendAssetId?: string = undefined; @@ -35,7 +38,11 @@ export class Proof { async setAssets(assets: Assets) { const indexedAssetEntries: [string, AssetMeta][] = []; for (const [base64, meta] of Object.entries(assets)) { - const index = await this.imageStore.write(base64, meta.mimeType); + const index = await this.imageStore.write( + base64, + meta.mimeType, + OnWriteExistStrategy.IGNORE + ); indexedAssetEntries.push([index, meta]); }