add ViewerView decrypt from URL fragment via view store
This commit is contained in:
@@ -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
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user