feat: implement direct file upload for sends with JWT token validation

- Added `processSendFileUpload` function to handle file uploads for sends.
- Integrated JWT token creation and verification for secure file uploads.
- Updated `handleCreateFileSendV2` and `handleGetSendFileUpload` to use new upload URL generation.
- Refactored upload handling in `handleUploadSendFile` and `handlePublicUploadSendFile` to utilize the new upload process.
- Introduced `uploadDirectEncryptedPayload` for handling direct uploads with progress tracking.
- Enhanced API routes to support both POST and PUT methods for attachment uploads.
- Added localization strings for upload progress messages.
- Created utility functions for direct upload URL building and payload parsing.
This commit is contained in:
shuaiplus
2026-03-18 02:26:10 +08:00
parent 3204eeb9ab
commit bb3fe41330
17 changed files with 666 additions and 127 deletions
+11 -10
View File
@@ -1,6 +1,6 @@
import { base64ToBytes, bytesToBase64, decryptBw, decryptBwFileData, decryptStr, encryptBw, encryptBwFileData, hkdf, pbkdf2 } from '../crypto';
import type { Send, SendDraft, SessionState } from '../types';
import { chunkArray, createApiError, parseErrorMessage, parseJson, type AuthedFetch } from './shared';
import { chunkArray, createApiError, parseErrorMessage, parseJson, uploadDirectEncryptedPayload, type AuthedFetch } from './shared';
function toIsoDateFromDays(value: string, required: boolean): string | null {
const raw = String(value || '').trim();
@@ -70,7 +70,8 @@ export async function getSends(authedFetch: AuthedFetch): Promise<Send[]> {
export async function createSend(
authedFetch: AuthedFetch,
session: SessionState,
draft: SendDraft
draft: SendDraft,
onProgress?: (percent: number | null) => void
): Promise<Send> {
if (!session.symEncKey || !session.symMacKey) throw new Error('Vault key unavailable');
const userEnc = base64ToBytes(session.symEncKey);
@@ -148,16 +149,16 @@ export async function createSend(
});
if (!fileResp.ok) throw new Error(await parseErrorMessage(fileResp, 'Create file send failed'));
const uploadInfo = await parseJson<{ url?: string; sendResponse?: Send }>(fileResp);
const uploadInfo = await parseJson<{ url?: string; sendResponse?: Send; fileUploadType?: number }>(fileResp);
const uploadUrl = uploadInfo?.url;
if (!uploadUrl) throw new Error('Create file send failed: missing upload URL');
const formData = new FormData();
const encryptedBlob = new Blob([encryptedFileBytes as unknown as BlobPart], { type: 'application/octet-stream' });
formData.set('data', encryptedBlob, fileNameCipher);
const uploadResp = await authedFetch(uploadUrl, {
method: 'POST',
body: formData,
const uploadResp = await uploadDirectEncryptedPayload({
accessToken: session.accessToken,
uploadUrl,
payload: encryptedFileBytes,
fileUploadType: uploadInfo?.fileUploadType,
unsupportedMessage: 'Unsupported send upload type',
onProgress,
});
if (!uploadResp.ok) throw new Error(await parseErrorMessage(uploadResp, 'Upload send file failed'));
if (!uploadInfo?.sendResponse?.id) throw new Error('Create file send failed');
+96
View File
@@ -58,3 +58,99 @@ export function createApiError(message: string, status?: number): Error & { stat
export function requiredError(messageKey: string): never {
throw new Error(t(messageKey));
}
interface UploadWithProgressOptions {
accessToken?: string;
method?: string;
headers?: HeadersInit;
body?: Document | XMLHttpRequestBodyInit | null;
onProgress?: (percent: number | null) => void;
}
interface DirectEncryptedUploadOptions {
accessToken: string;
uploadUrl: string;
payload: ArrayBuffer | Uint8Array;
fileUploadType: number | null | undefined;
unsupportedMessage: string;
onProgress?: (percent: number | null) => void;
}
function toAbsoluteUrl(input: string): string {
if (typeof window === 'undefined') return input;
return new URL(input, window.location.origin).toString();
}
function parseXhrHeaders(raw: string): Headers {
const headers = new Headers();
for (const line of raw.split(/\r?\n/)) {
const index = line.indexOf(':');
if (index <= 0) continue;
const name = line.slice(0, index).trim();
const value = line.slice(index + 1).trim();
if (name) headers.append(name, value);
}
return headers;
}
export async function uploadWithProgress(input: string, options: UploadWithProgressOptions = {}): Promise<Response> {
if (typeof XMLHttpRequest === 'undefined') {
const headers = new Headers(options.headers || {});
if (options.accessToken) headers.set('Authorization', `Bearer ${options.accessToken}`);
return fetch(input, {
method: options.method || 'POST',
headers,
body: options.body ?? null,
});
}
return new Promise<Response>((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(options.method || 'POST', toAbsoluteUrl(input), true);
const headers = new Headers(options.headers || {});
if (options.accessToken) headers.set('Authorization', `Bearer ${options.accessToken}`);
headers.forEach((value, key) => xhr.setRequestHeader(key, value));
xhr.upload.onprogress = (event) => {
if (!options.onProgress) return;
if (!event.lengthComputable || event.total <= 0) {
options.onProgress(null);
return;
}
options.onProgress(Math.max(0, Math.min(100, Math.round((event.loaded / event.total) * 100))));
};
xhr.onerror = () => reject(new Error('Network error'));
xhr.onabort = () => reject(new Error('Upload aborted'));
xhr.onload = () => {
options.onProgress?.(100);
resolve(
new Response(xhr.responseText || null, {
status: xhr.status,
statusText: xhr.statusText,
headers: parseXhrHeaders(xhr.getAllResponseHeaders()),
})
);
};
xhr.send(options.body ?? null);
});
}
export async function uploadDirectEncryptedPayload(options: DirectEncryptedUploadOptions): Promise<Response> {
if (options.fileUploadType !== 1) {
throw new Error(options.unsupportedMessage);
}
return uploadWithProgress(options.uploadUrl, {
accessToken: options.accessToken,
method: 'PUT',
headers: {
'Content-Type': 'application/octet-stream',
'x-ms-blob-type': 'BlockBlob',
},
body: options.payload,
onProgress: options.onProgress,
});
}
+11 -7
View File
@@ -12,6 +12,7 @@ import {
chunkArray,
parseErrorMessage,
parseJson,
uploadDirectEncryptedPayload,
type AuthedFetch,
} from './shared';
import { readResponseBytesWithProgress } from '../download';
@@ -199,7 +200,8 @@ export async function uploadCipherAttachment(
session: SessionState,
cipherId: string,
file: File,
cipherForKey?: Cipher | null
cipherForKey?: Cipher | null,
onProgress?: (percent: number | null) => void
): Promise<void> {
if (!session.symEncKey || !session.symMacKey) throw new Error('Vault key unavailable');
const id = String(cipherId || '').trim();
@@ -233,6 +235,7 @@ export async function uploadCipherAttachment(
(await parseJson<{
attachmentId?: string;
url?: string;
fileUploadType?: number;
}>(metaResp)) || {};
const attachmentId = String(meta.attachmentId || '').trim();
const uploadUrl = String(meta.url || '').trim();
@@ -240,12 +243,13 @@ export async function uploadCipherAttachment(
const payload = new ArrayBuffer(encryptedBytes.byteLength);
new Uint8Array(payload).set(encryptedBytes);
const formData = new FormData();
formData.set('data', new Blob([payload], { type: 'application/octet-stream' }), encryptedFileName);
const uploadResp = await authedFetch(uploadUrl, {
method: 'POST',
body: formData,
const uploadResp = await uploadDirectEncryptedPayload({
accessToken: session.accessToken,
uploadUrl,
payload,
fileUploadType: meta.fileUploadType,
unsupportedMessage: 'Unsupported attachment upload type',
onProgress,
});
if (!uploadResp.ok) {
try {