Skip to content

Commit

Permalink
feat: update query-client to new file plugin and remove unused s3 stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-Torrent committed Dec 3, 2021
1 parent 02b1eef commit 2dc441c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 83 deletions.
8 changes: 2 additions & 6 deletions cypress/support/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ const {
ITEMS_ROUTE,
buildUploadFilesRoute,
buildDownloadFilesRoute,
buildGetS3MetadataRoute,
buildS3UploadFileRoute,
GET_CURRENT_MEMBER_ROUTE,
buildSignInPath,
SIGN_OUT_ROUTE,
Expand Down Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 6 additions & 15 deletions src/components/item/ItemContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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 <Loader />;
}

Expand Down Expand Up @@ -124,7 +115,7 @@ const ItemContent = ({ item, enableEdition }) => {
id={buildS3FileItemId(itemId)}
editCaption={isEditing}
item={item}
content={s3Content}
content={content}
onSaveCaption={onSaveCaption}
saveButtonId={saveButtonId}
/>
Expand Down
66 changes: 9 additions & 57 deletions src/utils/uppy.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -121,7 +76,6 @@ export const configureFileUppy = ({
onFilesAdded,
onError,
onUpload,
s3Upload: uploadItemToS3,
buildEndpoint: (id) =>
`${API_HOST}/${API_ROUTES.buildUploadFilesRoute(id)}`,
});
Expand Down Expand Up @@ -149,7 +103,6 @@ export const configureThumbnailUppy = ({
},
buildEndpoint: (id) =>
`${API_HOST}/${API_ROUTES.buildUploadItemThumbnailRoute(id)}`,
method: UPLOAD_FILES_METHODS.DEFAULT,
});

export const configureAvatarUppy = ({
Expand All @@ -173,5 +126,4 @@ export const configureAvatarUppy = ({
},
buildEndpoint: (id) =>
`${API_HOST}/${API_ROUTES.buildUploadAvatarRoute(id)}`,
method: UPLOAD_FILES_METHODS.DEFAULT,
});
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -2179,7 +2179,7 @@ __metadata:
uuid: 8.3.2
peerDependencies:
react: ^17.0.0
checksum: c6b4b91df05d09acd8e6ea4a5ef75a984f9e375ecdeb5a5a3662a5a63fa3303f62a64b3512462df7e5747cead4413026e3d2b169817fa15a4d9aff4898117216
checksum: bac4fd93b56e3f02ee68e46c23a5525a466a1489048dd66ec689203d80694023b6f36373185e8bc97ed09ab91472922e50603911f60acc512de284754b0cd88e
languageName: node
linkType: hard

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 2dc441c

Please sign in to comment.