From 2dc441c3d06eea33329771d7b281254c515cccc1 Mon Sep 17 00:00:00 2001 From: Julien Torrent Date: Thu, 2 Dec 2021 16:36:24 +0100 Subject: [PATCH] feat: update query-client to new file plugin and remove unused s3 stuff --- cypress/support/server.js | 8 +--- package.json | 2 +- src/components/item/ItemContent.js | 21 +++------- src/utils/uppy.js | 66 ++++-------------------------- yarn.lock | 8 ++-- 5 files changed, 22 insertions(+), 83 deletions(-) diff --git a/cypress/support/server.js b/cypress/support/server.js index 8fe51ab1a..38ede3cb5 100644 --- a/cypress/support/server.js +++ b/cypress/support/server.js @@ -52,8 +52,6 @@ const { ITEMS_ROUTE, buildUploadFilesRoute, buildDownloadFilesRoute, - buildGetS3MetadataRoute, - buildS3UploadFileRoute, GET_CURRENT_MEMBER_ROUTE, buildSignInPath, SIGN_OUT_ROUTE, @@ -713,9 +711,7 @@ export const mockUploadItem = (items, shouldThrowError) => { url: new RegExp( `${API_HOST}/${parseStringToRegExp( buildUploadFilesRoute(ID_FORMAT), - )}$|${API_HOST}/${buildUploadFilesRoute()}$|${API_HOST}/${parseStringToRegExp( - buildS3UploadFileRoute(ID_FORMAT), - )}$|${API_HOST}/${buildS3UploadFileRoute()}$`, + )}$|${API_HOST}/${buildUploadFilesRoute()}`, ), }, ({ reply }) => { @@ -751,7 +747,7 @@ export const mockGetS3Metadata = (items, shouldThrowError) => { cy.intercept( { method: DEFAULT_GET.method, - url: new RegExp(`${API_HOST}/${buildGetS3MetadataRoute(ID_FORMAT)}$`), + url: new RegExp(`${API_HOST}/${buildDownloadFilesRoute(ID_FORMAT)}$`), }, ({ reply, url }) => { if (shouldThrowError) { diff --git a/package.json b/package.json index 26fbb58c8..c1a799557 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "license": "AGPL-3.0-only", "dependencies": { "@graasp/chatbox": "git://github.com/graasp/graasp-chatbox.git#main", - "@graasp/query-client": "git://github.com/graasp/graasp-query-client.git", + "@graasp/query-client": "git://github.com/graasp/graasp-query-client.git#95/newFilePlugin", "@graasp/ui": "git://github.com/graasp/graasp-ui.git", "@material-ui/core": "4.11.2", "@material-ui/icons": "5.0.0-beta.4", diff --git a/src/components/item/ItemContent.js b/src/components/item/ItemContent.js index fcf6361b4..6903baeef 100644 --- a/src/components/item/ItemContent.js +++ b/src/components/item/ItemContent.js @@ -30,7 +30,7 @@ import { buildDocumentExtra, getDocumentExtra } from '../../utils/itemExtra'; import NewItemButton from '../main/NewItemButton'; import { CurrentUserContext } from '../context/CurrentUserContext'; -const { useChildren, useFileContent, useS3FileContent } = hooks; +const { useChildren, useFileContent } = hooks; const useStyles = makeStyles(() => ({ fileWrapper: { @@ -61,23 +61,14 @@ const ItemContent = ({ item, enableEdition }) => { const { data: content, isLoading: isLoadingFileContent } = useFileContent( id, { - enabled: item && itemType === ITEM_TYPES.FILE, + enabled: + item && + (itemType === ITEM_TYPES.FILE || itemType === ITEM_TYPES.S3_FILE), }, ); - const { - data: s3Content, - isLoading: isLoadingS3FileContent, - } = useS3FileContent(id, { - enabled: itemType === ITEM_TYPES.S3_FILE, - }); const isEditing = enableEdition && editingItemId === itemId; - if ( - isLoadingFileContent || - isLoadingS3FileContent || - isLoadingUser || - isLoadingChildren - ) { + if (isLoadingFileContent || isLoadingUser || isLoadingChildren) { return ; } @@ -124,7 +115,7 @@ const ItemContent = ({ item, enableEdition }) => { id={buildS3FileItemId(itemId)} editCaption={isEditing} item={item} - content={s3Content} + content={content} onSaveCaption={onSaveCaption} saveButtonId={saveButtonId} /> diff --git a/src/utils/uppy.js b/src/utils/uppy.js index c4afe5842..9c95e7b6a 100644 --- a/src/utils/uppy.js +++ b/src/utils/uppy.js @@ -1,16 +1,7 @@ import Uppy from '@uppy/core'; -import AwsS3 from '@uppy/aws-s3'; import XHRUpload from '@uppy/xhr-upload'; -import { API_ROUTES, Api } from '@graasp/query-client'; -import { - API_HOST, - FILE_UPLOAD_MAX_FILES, - UPLOAD_METHOD, -} from '../config/constants'; -import { UPLOAD_FILES_METHODS } from '../enums'; -import { DEFAULT_PUT } from '../api/utils'; - -const { uploadItemToS3 } = Api; +import { API_ROUTES } from '@graasp/query-client'; +import { API_HOST, FILE_UPLOAD_MAX_FILES } from '../config/constants'; const configureUppy = ({ itemId, @@ -21,59 +12,23 @@ const configureUppy = ({ onFileAdded, onError, buildEndpoint, - s3Upload, fieldName = 'files', restrictions = { maxNumberOfFiles: FILE_UPLOAD_MAX_FILES, }, - method, }) => { const uppy = new Uppy({ restrictions, autoProceed: true, }); - // define upload method - switch (method ?? UPLOAD_METHOD ?? UPLOAD_FILES_METHODS.DEFAULT) { - case UPLOAD_FILES_METHODS.S3: - uppy.use(AwsS3, { - async getUploadParameters(file) { - // Send a request to s3 - const data = await s3Upload( - { - itemId, - filename: file.name, - contentType: file.type, - }, - { API_HOST }, - ); - - // Return an object in the correct shape. - return { - method: DEFAULT_PUT.method, - url: data.uploadUrl, - fields: [], - // Provide content type header required by S3 - headers: { - 'Content-Type': file.type, - 'Content-disposition': `attachment; filename=${file.name}`, - }, - }; - }, - }); - break; - - case UPLOAD_FILES_METHODS.DEFAULT: - default: - uppy.use(XHRUpload, { - endpoint: buildEndpoint(itemId), - withCredentials: true, - formData: true, - fieldName, - metaFields: [], - }); - break; - } + uppy.use(XHRUpload, { + endpoint: buildEndpoint(itemId), + withCredentials: true, + formData: true, + fieldName, + metaFields: [], + }); uppy.on('file-added', (file) => { onFileAdded?.(file); @@ -121,7 +76,6 @@ export const configureFileUppy = ({ onFilesAdded, onError, onUpload, - s3Upload: uploadItemToS3, buildEndpoint: (id) => `${API_HOST}/${API_ROUTES.buildUploadFilesRoute(id)}`, }); @@ -149,7 +103,6 @@ export const configureThumbnailUppy = ({ }, buildEndpoint: (id) => `${API_HOST}/${API_ROUTES.buildUploadItemThumbnailRoute(id)}`, - method: UPLOAD_FILES_METHODS.DEFAULT, }); export const configureAvatarUppy = ({ @@ -173,5 +126,4 @@ export const configureAvatarUppy = ({ }, buildEndpoint: (id) => `${API_HOST}/${API_ROUTES.buildUploadAvatarRoute(id)}`, - method: UPLOAD_FILES_METHODS.DEFAULT, }); diff --git a/yarn.lock b/yarn.lock index b326dc141..8921f62f9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2166,9 +2166,9 @@ __metadata: languageName: node linkType: hard -"@graasp/query-client@git://github.com/graasp/graasp-query-client.git": +"@graasp/query-client@git://github.com/graasp/graasp-query-client.git#95/newFilePlugin": version: 0.1.0 - resolution: "@graasp/query-client@git://github.com/graasp/graasp-query-client.git#commit=65fab859e8dc13c0e8cd4906cf91b8d90e7f96fa" + resolution: "@graasp/query-client@git://github.com/graasp/graasp-query-client.git#commit=17b89907e583f8232e51facf18554dbfac159e1e" dependencies: axios: 0.21.4 http-status-codes: 2.1.4 @@ -2179,7 +2179,7 @@ __metadata: uuid: 8.3.2 peerDependencies: react: ^17.0.0 - checksum: c6b4b91df05d09acd8e6ea4a5ef75a984f9e375ecdeb5a5a3662a5a63fa3303f62a64b3512462df7e5747cead4413026e3d2b169817fa15a4d9aff4898117216 + checksum: bac4fd93b56e3f02ee68e46c23a5525a466a1489048dd66ec689203d80694023b6f36373185e8bc97ed09ab91472922e50603911f60acc512de284754b0cd88e languageName: node linkType: hard @@ -10299,7 +10299,7 @@ fsevents@^1.2.7: "@cypress/code-coverage": 3.9.2 "@cypress/instrument-cra": 1.4.0 "@graasp/chatbox": "git://github.com/graasp/graasp-chatbox.git#main" - "@graasp/query-client": "git://github.com/graasp/graasp-query-client.git" + "@graasp/query-client": "git://github.com/graasp/graasp-query-client.git#95/newFilePlugin" "@graasp/ui": "git://github.com/graasp/graasp-ui.git" "@graasp/query-client": "git://github.com/graasp/graasp-query-client.git" "@graasp/ui": "git://github.com/graasp/graasp-ui.git"