From 5db1a1f2372a559dbc6f2c99742921cf5613b0e5 Mon Sep 17 00:00:00 2001 From: Wilson Wong Date: Tue, 15 Aug 2023 10:41:42 -0700 Subject: [PATCH 1/7] SC-3303: Align BCBox with V0.6 file upload changes --- frontend/src/services/objectService.ts | 25 +++++++++++++++---------- frontend/src/utils/utils.ts | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/frontend/src/services/objectService.ts b/frontend/src/services/objectService.ts index 3d6a03f5..7c7b8ddd 100644 --- a/frontend/src/services/objectService.ts +++ b/frontend/src/services/objectService.ts @@ -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'; @@ -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 }>, @@ -26,7 +27,10 @@ export default { axiosOptions?: AxiosRequestConfig ) { const config = { - headers: { 'Content-Type': 'multipart/form-data' }, + headers: { + 'Content-Type': 'application/octet-stream', + 'Content-Disposition': setDispositionHeader(object.name) + }, params: { bucketId: params.bucketId, tagset: {} @@ -48,9 +52,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); }, /** @@ -262,7 +265,7 @@ export default { * @param {AxiosRequestConfig} axiosOptions Axios request config options * @returns {Promise} An axios response */ - updateObject( + async updateObject( objectId: string, object: any, headers: { @@ -274,7 +277,10 @@ export default { axiosOptions?: AxiosRequestConfig ) { const config = { - headers: { 'Content-Type': 'multipart/form-data' }, + headers: { + 'Content-Type': 'application/octet-stream', + 'Content-Disposition': setDispositionHeader(object.name) + }, params: { tagset: {} }, @@ -295,8 +301,7 @@ export default { ); } - const fd = new FormData(); - fd.append('file', object); - return comsAxios(axiosOptions).post(`${PATH}/${objectId}`, fd, config); + const fd = await object.arrayBuffer() + return comsAxios(axiosOptions).put(`${PATH}/${objectId}`, fd, config); }, }; diff --git a/frontend/src/utils/utils.ts b/frontend/src/utils/utils.ts index d5796b31..c2c80f44 100644 --- a/frontend/src/utils/utils.ts +++ b/frontend/src/utils/utils.ts @@ -62,3 +62,20 @@ export function partition( [[], []] as [Array, Array] ); } + +/** + * @function setDispositionHeader + * Constructs a value for http request header 'Content-Disposition' + * @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}`); + } +} From 247f9677bdee9cd61fb90352208f821931d0c3b0 Mon Sep 17 00:00:00 2001 From: Wilson Wong Date: Tue, 15 Aug 2023 11:29:50 -0700 Subject: [PATCH 2/7] SC-3303: Fix linting --- frontend/src/services/objectService.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/frontend/src/services/objectService.ts b/frontend/src/services/objectService.ts index 7c7b8ddd..e790154d 100644 --- a/frontend/src/services/objectService.ts +++ b/frontend/src/services/objectService.ts @@ -27,7 +27,7 @@ export default { axiosOptions?: AxiosRequestConfig ) { const config = { - headers: { + headers: { 'Content-Type': 'application/octet-stream', 'Content-Disposition': setDispositionHeader(object.name) }, @@ -52,7 +52,7 @@ export default { ); } - const fd = await object.arrayBuffer() + const fd = await object.arrayBuffer(); return comsAxios(axiosOptions).put(PATH, fd, config); }, @@ -206,7 +206,7 @@ export default { const config = { headers: {}, }; - + // Map the metadata if required if (headers.metadata) { config.headers = { @@ -214,7 +214,7 @@ export default { }; } return comsAxios().get(`${PATH}/metadata`, config); - }, + }, /** * @function searchObjects @@ -277,7 +277,7 @@ export default { axiosOptions?: AxiosRequestConfig ) { const config = { - headers: { + headers: { 'Content-Type': 'application/octet-stream', 'Content-Disposition': setDispositionHeader(object.name) }, @@ -301,7 +301,7 @@ export default { ); } - const fd = await object.arrayBuffer() + const fd = await object.arrayBuffer(); return comsAxios(axiosOptions).put(`${PATH}/${objectId}`, fd, config); }, }; From c81a001d72733b02edda008ad0d6d6fec49a7cd6 Mon Sep 17 00:00:00 2001 From: Wilson Wong Date: Wed, 16 Aug 2023 09:52:33 -0700 Subject: [PATCH 3/7] SC-3303, docs: JS doc update and comments --- frontend/src/services/objectService.ts | 5 +++-- frontend/src/utils/utils.ts | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/frontend/src/services/objectService.ts b/frontend/src/services/objectService.ts index e790154d..4a2fa10b 100644 --- a/frontend/src/services/objectService.ts +++ b/frontend/src/services/objectService.ts @@ -28,8 +28,9 @@ export default { ) { const config = { headers: { - 'Content-Type': 'application/octet-stream', 'Content-Disposition': setDispositionHeader(object.name) + // Content-Type header/MIME type detection needs TBD + // Also in uploadObject call below }, params: { bucketId: params.bucketId, @@ -278,8 +279,8 @@ export default { ) { const config = { headers: { - 'Content-Type': 'application/octet-stream', 'Content-Disposition': setDispositionHeader(object.name) + // Content-Type header/MIME type detection needs TBD }, params: { tagset: {} diff --git a/frontend/src/utils/utils.ts b/frontend/src/utils/utils.ts index c2c80f44..57d054be 100644 --- a/frontend/src/utils/utils.ts +++ b/frontend/src/utils/utils.ts @@ -65,7 +65,8 @@ export function partition( /** * @function setDispositionHeader - * Constructs a value for http request header 'Content-Disposition' + * 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' */ From 27695189a1302325ee46c6065516082f6328b60c Mon Sep 17 00:00:00 2001 From: Wilson Wong Date: Wed, 16 Aug 2023 10:50:20 -0700 Subject: [PATCH 4/7] SC-3303, fix: Added content-type header back --- frontend/src/services/objectService.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/frontend/src/services/objectService.ts b/frontend/src/services/objectService.ts index 4a2fa10b..1f9b19d0 100644 --- a/frontend/src/services/objectService.ts +++ b/frontend/src/services/objectService.ts @@ -28,7 +28,8 @@ export default { ) { const config = { headers: { - 'Content-Disposition': setDispositionHeader(object.name) + 'Content-Disposition': setDispositionHeader(object.name), + 'Content-Type': 'application/octet-stream' // Content-Type header/MIME type detection needs TBD // Also in uploadObject call below }, @@ -279,7 +280,8 @@ export default { ) { const config = { headers: { - 'Content-Disposition': setDispositionHeader(object.name) + 'Content-Disposition': setDispositionHeader(object.name), + 'Content-Type': 'application/octet-stream' // Content-Type header/MIME type detection needs TBD }, params: { From 9cc09fde7b24aebf838f171a89c3e2d01498d2af Mon Sep 17 00:00:00 2001 From: Wilson Wong Date: Wed, 16 Aug 2023 16:16:06 -0700 Subject: [PATCH 5/7] SC-3303, fix: Set content-type based on file extension --- frontend/src/services/objectService.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/frontend/src/services/objectService.ts b/frontend/src/services/objectService.ts index 1f9b19d0..b8ffd608 100644 --- a/frontend/src/services/objectService.ts +++ b/frontend/src/services/objectService.ts @@ -30,8 +30,6 @@ export default { headers: { 'Content-Disposition': setDispositionHeader(object.name), 'Content-Type': 'application/octet-stream' - // Content-Type header/MIME type detection needs TBD - // Also in uploadObject call below }, params: { bucketId: params.bucketId, @@ -39,6 +37,12 @@ export default { }, }; + // Detect MIME type from File object and assign to header + // Fall back to application/octet-stream as above + if (object?.type) { + config.headers['Content-Type'] = object.type; + } + // Map the metadata if required if (headers.metadata) { config.headers = { @@ -282,13 +286,18 @@ export default { headers: { 'Content-Disposition': setDispositionHeader(object.name), 'Content-Type': 'application/octet-stream' - // Content-Type header/MIME type detection needs TBD }, params: { tagset: {} }, }; + // Detect MIME type from File object and assign to header + // Fall back to application/octet-stream as above + if (object?.type) { + config.headers['Content-Type'] = object.type; + } + // Map the metadata if required if (headers.metadata) { config.headers = { From 0a87506e76fc00e66566ad8643dc17ee58e533a9 Mon Sep 17 00:00:00 2001 From: Wilson Wong Date: Fri, 18 Aug 2023 16:32:11 -0700 Subject: [PATCH 6/7] SC-3303, fix: streamline content-type header in objectService --- frontend/src/services/objectService.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/frontend/src/services/objectService.ts b/frontend/src/services/objectService.ts index b8ffd608..71311dbb 100644 --- a/frontend/src/services/objectService.ts +++ b/frontend/src/services/objectService.ts @@ -26,10 +26,12 @@ export default { }, axiosOptions?: AxiosRequestConfig ) { + // setDispositionHeader constructs header based on file name + // Content-Type defaults octet-stream if MIME type unavailable const config = { headers: { 'Content-Disposition': setDispositionHeader(object.name), - 'Content-Type': 'application/octet-stream' + 'Content-Type': object?.type ?? 'application/octet-stream' }, params: { bucketId: params.bucketId, @@ -37,12 +39,6 @@ export default { }, }; - // Detect MIME type from File object and assign to header - // Fall back to application/octet-stream as above - if (object?.type) { - config.headers['Content-Type'] = object.type; - } - // Map the metadata if required if (headers.metadata) { config.headers = { @@ -282,10 +278,12 @@ export default { }, axiosOptions?: AxiosRequestConfig ) { + // setDispositionHeader constructs header based on file name + // Content-Type defaults octet-stream if MIME type unavailable const config = { headers: { 'Content-Disposition': setDispositionHeader(object.name), - 'Content-Type': 'application/octet-stream' + 'Content-Type': object?.type ?? 'application/octet-stream' }, params: { tagset: {} From 6f82824500d37c85d89365abd743c06464f59d16 Mon Sep 17 00:00:00 2001 From: Wilson Wong Date: Fri, 18 Aug 2023 16:39:31 -0700 Subject: [PATCH 7/7] SC-3303, fix: remove redundant code in objectService --- frontend/src/services/objectService.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/frontend/src/services/objectService.ts b/frontend/src/services/objectService.ts index 71311dbb..587e1f54 100644 --- a/frontend/src/services/objectService.ts +++ b/frontend/src/services/objectService.ts @@ -290,12 +290,6 @@ export default { }, }; - // Detect MIME type from File object and assign to header - // Fall back to application/octet-stream as above - if (object?.type) { - config.headers['Content-Type'] = object.type; - } - // Map the metadata if required if (headers.metadata) { config.headers = {