mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-16 06:58:46 +00:00
프론트엔드에서의 파일 업로드 임시 구현
This commit is contained in:
@@ -7,8 +7,14 @@ import {
|
||||
unwrapDataKey,
|
||||
encryptData,
|
||||
decryptData,
|
||||
digestMessage,
|
||||
signRequestBody,
|
||||
} from "$lib/modules/crypto";
|
||||
import type { DirectroyInfoResponse, DirectoryCreateRequest } from "$lib/server/schemas";
|
||||
import type {
|
||||
DirectroyInfoResponse,
|
||||
DirectoryCreateRequest,
|
||||
FileUploadRequest,
|
||||
} from "$lib/server/schemas";
|
||||
import type { MasterKey } from "$lib/stores";
|
||||
|
||||
export const decryptDirectroyMetadata = async (
|
||||
@@ -43,3 +49,38 @@ export const requestDirectroyCreation = async (
|
||||
signKey,
|
||||
);
|
||||
};
|
||||
|
||||
export const requestFileUpload = async (
|
||||
file: File,
|
||||
parentId: "root" | number,
|
||||
masterKey: MasterKey,
|
||||
signKey: CryptoKey,
|
||||
) => {
|
||||
const { dataKey } = await generateDataKey();
|
||||
const fileEncrypted = await encryptData(await file.arrayBuffer(), dataKey);
|
||||
const fileEncryptedHash = await digestMessage(fileEncrypted.ciphertext);
|
||||
const nameEncrypted = await encryptData(new TextEncoder().encode(file.name), dataKey);
|
||||
|
||||
const form = new FormData();
|
||||
form.set(
|
||||
"metadata",
|
||||
await signRequestBody<FileUploadRequest>(
|
||||
{
|
||||
parentId,
|
||||
mekVersion: masterKey.version,
|
||||
dek: await wrapDataKey(dataKey, masterKey.key),
|
||||
contentHash: encodeToBase64(fileEncryptedHash),
|
||||
contentIv: fileEncrypted.iv,
|
||||
name: encodeToBase64(nameEncrypted.ciphertext),
|
||||
nameIv: nameEncrypted.iv,
|
||||
},
|
||||
signKey,
|
||||
),
|
||||
);
|
||||
form.set("content", new Blob([fileEncrypted.ciphertext]));
|
||||
|
||||
// TODO: Progress, Scheduling, ...
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/api/file/upload");
|
||||
xhr.send(form);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user