Skip to content

Commit

Permalink
Added support to skip payload encryption but keep encrypting metadata…
Browse files Browse the repository at this point in the history
… and isEncrypted flag; (#231)
  • Loading branch information
stef-coenen authored Sep 2, 2024
1 parent a89638b commit 5b7d03e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/js-lib/src/core/DriveData/File/DriveFileTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface PayloadFile {
payload: File | Blob;
previewThumbnail?: EmbeddedThumb;
descriptorContent?: string;
skipEncryption?: boolean;
}

type None = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export const uploadFile = async (
thumbnails?: ThumbnailFile[],
encrypt = true,
onVersionConflict?: () => Promise<void | UploadResult> | void,
axiosConfig?: AxiosRequestConfig
options?: {
axiosConfig?: AxiosRequestConfig;
keyHeader?: KeyHeader | undefined;
}
): Promise<UploadResult | void> => {
isDebug &&
console.debug('request', new URL(`${dotYouClient.getEndpoint()}/drive/files/upload`).pathname, {
Expand All @@ -47,9 +50,9 @@ export const uploadFile = async (
});

// Force isEncrypted on the metadata to match the encrypt flag
metadata.isEncrypted = encrypt;
metadata.isEncrypted = encrypt || !!options?.keyHeader;

const keyHeader = encrypt ? GenerateKeyHeader() : undefined;
const keyHeader = encrypt ? options?.keyHeader || GenerateKeyHeader() : undefined;

const { systemFileType, ...strippedInstructions } = instructions;

Expand Down Expand Up @@ -83,7 +86,7 @@ export const uploadFile = async (
data,
systemFileType,
onVersionConflict,
axiosConfig
options?.axiosConfig
);

if (!uploadResult) return;
Expand Down Expand Up @@ -266,6 +269,8 @@ export const reUploadFile = async (
thumbnails,
encrypt,
undefined,
axiosConfig
{
axiosConfig,
}
);
};
17 changes: 9 additions & 8 deletions packages/js-lib/src/core/DriveData/Upload/UploadHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,15 @@ export const buildFormData = async (
if (payloads) {
for (let i = 0; i < payloads.length; i++) {
const payload = payloads[i];
const encryptedPayload = keyHeader
? await encryptWithKeyheader(payload.payload, {
...keyHeader,
iv:
manifest?.PayloadDescriptors?.find((p) => p.payloadKey === payload.key)?.iv ||
keyHeader.iv,
})
: payload.payload;
const encryptedPayload =
keyHeader && !payload.skipEncryption
? await encryptWithKeyheader(payload.payload, {
...keyHeader,
iv:
manifest?.PayloadDescriptors?.find((p) => p.payloadKey === payload.key)?.iv ||
keyHeader.iv,
})
: payload.payload;

data.append('payload', encryptedPayload, payload.key);
}
Expand Down

0 comments on commit 5b7d03e

Please sign in to comment.