Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SC-3303: Align BCBox with V0.6 file upload changes #111

Merged
merged 7 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions frontend/src/services/objectService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { comsAxios } from './interceptors';
import { setDispositionHeader } from '@/utils/utils';

import type { AxiosRequestConfig } from 'axios';
import type { GetMetadataOptions, GetObjectTaggingOptions, MetadataPair, SearchObjectsOptions, Tag } from '@/types';
Expand All @@ -14,7 +15,7 @@ export default {
* @param {AxiosRequestConfig} axiosOptions Axios request config options
* @returns {Promise} An axios response
*/
createObject(
async createObject(
object: any,
headers: {
metadata?: Array<{ key: string; value: string }>,
Expand All @@ -25,8 +26,13 @@ export default {
},
axiosOptions?: AxiosRequestConfig
) {
// setDispositionHeader constructs header based on file name
// Content-Type defaults octet-stream if MIME type unavailable
const config = {
headers: { 'Content-Type': 'multipart/form-data' },
headers: {
'Content-Disposition': setDispositionHeader(object.name),
'Content-Type': object?.type ?? 'application/octet-stream'
},
params: {
bucketId: params.bucketId,
tagset: {}
Expand All @@ -48,9 +54,8 @@ export default {
);
}

const fd = new FormData();
fd.append('file', object);
return comsAxios(axiosOptions).post(PATH, fd, config);
const fd = await object.arrayBuffer();
return comsAxios(axiosOptions).put(PATH, fd, config);
},

/**
Expand Down Expand Up @@ -203,15 +208,15 @@ export default {
const config = {
headers: {},
};

// Map the metadata if required
if (headers.metadata) {
config.headers = {
...Object.fromEntries((headers.metadata.map((x: { key: string; value: string }) => ([x.key, x.value]))))
};
}
return comsAxios().get(`${PATH}/metadata`, config);
},
},

/**
* @function searchObjects
Expand Down Expand Up @@ -262,7 +267,7 @@ export default {
* @param {AxiosRequestConfig} axiosOptions Axios request config options
* @returns {Promise} An axios response
*/
updateObject(
async updateObject(
objectId: string,
object: any,
headers: {
Expand All @@ -273,8 +278,13 @@ export default {
},
axiosOptions?: AxiosRequestConfig
) {
// setDispositionHeader constructs header based on file name
// Content-Type defaults octet-stream if MIME type unavailable
const config = {
headers: { 'Content-Type': 'multipart/form-data' },
headers: {
'Content-Disposition': setDispositionHeader(object.name),
'Content-Type': object?.type ?? 'application/octet-stream'
},
params: {
tagset: {}
},
Expand All @@ -295,8 +305,7 @@ export default {
);
}

const fd = new FormData();
fd.append('file', object);
return comsAxios(axiosOptions).post(`${PATH}/${objectId}`, fd, config);
const fd = await object.arrayBuffer();
jujaga marked this conversation as resolved.
Show resolved Hide resolved
return comsAxios(axiosOptions).put(`${PATH}/${objectId}`, fd, config);
},
};
18 changes: 18 additions & 0 deletions frontend/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,21 @@ export function partition<T>(
[[], []] as [Array<T>, Array<T>]
);
}

/**
* @function setDispositionHeader
* Constructs a valid RFC 6266 'Content-Disposition' header
* and optionally handles RFC 8187 UTF-8 encoding when necessary
* @param {string} filename The file name to check if encoding is needed
* @returns {string} The value for the key 'Content-Disposition'
*/
export function setDispositionHeader(filename: string) {
const dispositionHeader = `attachment; filename="${filename}"`;
const encodedFilename = encodeURIComponent(filename);

if (filename === encodedFilename) {
return dispositionHeader;
} else {
return dispositionHeader.concat(`; filename*=UTF-8''${encodedFilename}`);
}
}