add ViewerView decrypt from URL fragment via view store

This commit is contained in:
2026-10-26 19:13:15 +01:00
parent 65bbc4537b
commit 112aa99f1e
3 changed files with 107 additions and 3 deletions
+48
View File
@@ -0,0 +1,48 @@
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { api } from '@/plugins/axios';
import type { GetZkUploadCiphertextResult } from '@/types/upload/GetZkUploadCiphertextResult';
import { base64ToBytes } from '@/utils/encoding/base64ToBytes';
import { toArrayBuffer } from '@/utils/toArrayBuffer';
import { decryptZkUpload } from '@/utils/upload/decryptZkUpload';
export const useViewStore = defineStore('view', () => {
const imageObjectUrl = ref<string | null>(null);
const clearImage = (): void => {
if (imageObjectUrl.value) {
URL.revokeObjectURL(imageObjectUrl.value);
imageObjectUrl.value = null;
}
};
const fetchZkCiphertext = async (uploadId: string): Promise<GetZkUploadCiphertextResult> => {
const { data } = await api.get<GetZkUploadCiphertextResult>(`/uploads/${uploadId}`);
return data;
};
const loadZk = async (payload: GetZkUploadCiphertextResult, fragmentKey: string): Promise<void> => {
clearImage();
const iv = base64ToBytes(payload.iv);
const ciphertext = base64ToBytes(payload.ciphertext);
const plaintext = await decryptZkUpload(ciphertext, iv, fragmentKey);
const buffer = toArrayBuffer(plaintext);
if (!buffer) {
throw new Error('Could not build image blob');
}
imageObjectUrl.value = URL.createObjectURL(new Blob([buffer]));
};
return {
imageObjectUrl,
fetchZkCiphertext,
loadZk,
clearImage
};
});