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

Block upload if size exceeds capacity #2053

Merged
merged 19 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions packages/common-components/src/FileInput/FileInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ const useStyles = makeStyles(({ constants, palette, overrides }: ITheme) =>
},
error: {
color: palette.error.main,
backgroundColor: palette.additional["gray"][3],
paddingLeft: 40,
paddingBottom: 4,
FSM1 marked this conversation as resolved.
Show resolved Hide resolved
...overrides?.FileInput?.error
},
item: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { parseFileContentResponse } from "../../../Utils/Helpers"
import getFilesFromDataTransferItems from "../../../Utils/getFilesFromDataTransferItems"

const CSFFileBrowser: React.FC<IFileBrowserModuleProps> = () => {
const { downloadFile, uploadFiles, buckets } = useFiles()
const { downloadFile, uploadFiles, buckets, storageSummary } = useFiles()
const { accountRestricted } = useFilesApi()
const { filesApiClient } = useFilesApi()
const { addToast } = useToasts()
Expand Down Expand Up @@ -169,12 +169,23 @@ const CSFFileBrowser: React.FC<IFileBrowserModuleProps> = () => {
return
}

const availableStorage = storageSummary?.available_storage || 0
const uploadSize = files?.reduce((total: number, file: File) => total += file.size, 0) || 0

if (uploadSize > availableStorage) {
addToast({
type: "error",
title: t`Upload size exceeds plan capacity`,
subtitle: t`Please select fewer files to upload`
})
return
}
const flattenedFiles = await getFilesFromDataTransferItems(fileItems)
const paths = [...new Set(flattenedFiles.map(f => f.filepath))]
paths.forEach(p => {
uploadFiles(bucket, flattenedFiles.filter(f => f.filepath === p), getPathWithFile(path, p))
})
}, [uploadFiles, bucket, accountRestricted, addToast])
}, [bucket, accountRestricted, storageSummary, addToast, uploadFiles])

const viewFolder = useCallback((cid: string) => {
const fileSystemItem = pathContents.find(f => f.cid === cid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const useStyles = makeStyles(({ constants, palette }: CSFTheme) =>
}))

const SharedFileBrowser = () => {
const { downloadFile, uploadFiles, buckets, getStorageSummary, refreshBuckets } = useFiles()
const { downloadFile, uploadFiles, buckets, getStorageSummary, refreshBuckets, storageSummary } = useFiles()
const { filesApiClient, accountRestricted } = useFilesApi()
const classes = useStyles()
const { addToast } = useToasts()
Expand Down Expand Up @@ -201,20 +201,31 @@ const SharedFileBrowser = () => {

const handleUploadOnDrop = useCallback(async (files: File[], fileItems: DataTransferItemList, path: string) => {
if (!bucket) return
if (accountRestricted) {
if (accountRestricted && bucket.permission === "owner") {
addToast({
type:"error",
title: t`Uploads disabled`,
subtitle: t`Your account is restricted. Until you&apos;ve settled up, you can&apos;t upload any new content.`
})
return
}
const availableStorage = storageSummary?.available_storage || 0
const uploadSize = files?.reduce((total: number, file: File) => total += file.size, 0) || 0

if (bucket.permission === "owner" && uploadSize > availableStorage) {
addToast({
type: "error",
title: t`Upload size exceeds plan capacity`,
subtitle: t`Please select fewer files to upload`
})
return
}
const flattenedFiles = await getFilesFromDataTransferItems(fileItems)
const paths = [...new Set(flattenedFiles.map(f => f.filepath))]
paths.forEach(p => {
uploadFiles(bucket, flattenedFiles.filter(f => f.filepath === p), getPathWithFile(path, p))
})
}, [uploadFiles, bucket, accountRestricted, addToast])
}, [bucket, accountRestricted, storageSummary, addToast, uploadFiles])

const bulkOperations: IBulkOperations = useMemo(() => ({
[CONTENT_TYPES.Directory]: ["download", "move", "delete", "share"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, FileInput } from "@chainsafe/common-components"
import { useFiles } from "../../../Contexts/FilesContext"
import { createStyles, makeStyles } from "@chainsafe/common-theme"
import React, { useCallback, useState } from "react"
import React, { useCallback, useMemo, useState } from "react"
import { Formik, Form } from "formik"
import { array, object } from "yup"
import CustomModal from "../../Elements/CustomModal"
Expand Down Expand Up @@ -83,15 +83,27 @@ const UploadFileModule = ({ modalOpen, close }: IUploadFileModuleProps) => {
const [isDoneDisabled, setIsDoneDisabled] = useState(true)
const { uploadFiles } = useFiles()
const { currentPath, refreshContents, bucket } = useFileBrowser()
const { storageSummary } = useFiles()

const UploadSchema = object().shape({ files: array().required(t`Please select a file to upload`) })
const UploadSchema = useMemo(() => object().shape({
files: array().required(t`Please select a file to upload`)
.test("Upload Size",
t`Upload size exceeds plan capacity`,
(files) => {
const isOwner = bucket?.permission === "owner"
const availableStorage = storageSummary?.available_storage || 0
const uploadSize = files?.reduce((total: number, file: File) => total += file.size, 0) || 0
return !(isOwner && uploadSize > availableStorage)
FSM1 marked this conversation as resolved.
Show resolved Hide resolved
})
}), [bucket, storageSummary])

const onFileNumberChange = useCallback((filesNumber: number) => {
setIsDoneDisabled(filesNumber === 0)
}, [])

const onSubmit = useCallback(async (values: {files: Array<File & {path: string}>}, helpers) => {
if (!bucket) return

helpers.setSubmitting(true)
try {
close()
Expand All @@ -106,7 +118,7 @@ const UploadFileModule = ({ modalOpen, close }: IUploadFileModuleProps) => {
console.error(error)
}
helpers.setSubmitting(false)
}, [close, currentPath, uploadFiles, refreshContents, bucket])
}, [bucket, close, refreshContents, uploadFiles, currentPath])

return (
<CustomModal
Expand All @@ -122,49 +134,51 @@ const UploadFileModule = ({ modalOpen, close }: IUploadFileModuleProps) => {
validationSchema={UploadSchema}
onSubmit={onSubmit}
>
<Form
data-cy="form-upload-file"
className={classes.root}
>
<FileInput
multiple={true}
className={classes.input}
classNames={{
closeIcon: classes.closeIcon,
filelist: classes.fileList,
item: classes.item,
addFiles: classes.addFiles
}}
label={t`Click or drag to upload files`}
moreFilesLabel={t`Add more files`}
maxSize={2 * 1024 ** 3}
name="files"
onFileNumberChange={onFileNumberChange}
testId="fileUpload"
/>
<footer className={classes.footer}>
<Button
testId="cancel-upload"
onClick={close}
size="medium"
className={classes.cancelButton}
variant="outline"
type="reset"
>
<Trans>Cancel</Trans>
</Button>
<Button
testId="start-upload"
size="medium"
type="submit"
variant="primary"
className={clsx(classes.okButton, "wide")}
disabled={isDoneDisabled}
>
<Trans>Start Upload</Trans>
</Button>
</footer>
</Form>
{({ isValid }) => (
FSM1 marked this conversation as resolved.
Show resolved Hide resolved
<Form
data-cy="form-upload-file"
className={classes.root}
>
<FileInput
multiple={true}
className={classes.input}
classNames={{
closeIcon: classes.closeIcon,
filelist: classes.fileList,
item: classes.item,
addFiles: classes.addFiles
}}
label={t`Click or drag to upload files`}
moreFilesLabel={t`Add more files`}
maxSize={2 * 1024 ** 3}
name="files"
onFileNumberChange={onFileNumberChange}
testId="fileUpload"
/>
<footer className={classes.footer}>
<Button
testId="cancel-upload"
onClick={close}
size="medium"
className={classes.cancelButton}
variant="outline"
type="reset"
>
<Trans>Cancel</Trans>
</Button>
<Button
testId="start-upload"
size="medium"
type="submit"
variant="primary"
className={clsx(classes.okButton, "wide")}
disabled={isDoneDisabled || !isValid}
>
<Trans>Start Upload</Trans>
</Button>
</footer>
</Form>
)}
</Formik>
</CustomModal>
)
Expand Down
6 changes: 6 additions & 0 deletions packages/files-ui/src/locales/de/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,9 @@ msgstr "Bitte geben Sie ein Passwort an"
msgid "Please select a file to upload"
msgstr "Bitte wählen Sie eine Datei zum Hochladen"

msgid "Please select fewer files to upload"
msgstr ""

msgid "Preview"
msgstr "Vorschau"

Expand Down Expand Up @@ -1033,6 +1036,9 @@ msgstr ""
msgid "Upload"
msgstr ""

msgid "Upload size exceeds plan capacity"
msgstr ""

msgid "Uploads cancelled"
msgstr ""

Expand Down
6 changes: 6 additions & 0 deletions packages/files-ui/src/locales/en/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,9 @@ msgstr "Please provide a password"
msgid "Please select a file to upload"
msgstr "Please select a file to upload"

msgid "Please select fewer files to upload"
msgstr "Please select fewer files to upload"

msgid "Preview"
msgstr "Preview"

Expand Down Expand Up @@ -1036,6 +1039,9 @@ msgstr "Update your credit card"
msgid "Upload"
msgstr "Upload"

msgid "Upload size exceeds plan capacity"
msgstr "Upload size exceeds plan capacity"

msgid "Uploads cancelled"
msgstr "Uploads cancelled"

Expand Down
6 changes: 6 additions & 0 deletions packages/files-ui/src/locales/es/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,9 @@ msgstr "Por favor ingrese una contraseña"
msgid "Please select a file to upload"
msgstr ""

msgid "Please select fewer files to upload"
msgstr ""

msgid "Preview"
msgstr "Avance"

Expand Down Expand Up @@ -1037,6 +1040,9 @@ msgstr ""
msgid "Upload"
msgstr ""

msgid "Upload size exceeds plan capacity"
msgstr ""

msgid "Uploads cancelled"
msgstr ""

Expand Down
18 changes: 9 additions & 9 deletions packages/files-ui/src/locales/fr/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ msgstr ""
"POT-Creation-Date: 2021-04-23 11:05+0200\n"
"PO-Revision-Date: 2022-03-26 13:08+0000\n"
"Last-Translator: J. Lavoie <j.lavoie@net-c.ca>\n"
"Language-Team: French <https://hosted.weblate.org/projects/chainsafe-files/"
"chainsafe-files-user-interface/fr/>\n"
"Language-Team: French <https://hosted.weblate.org/projects/chainsafe-files/chainsafe-files-user-interface/fr/>\n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
Expand Down Expand Up @@ -265,10 +264,7 @@ msgid "Credit Card is expiring soon"
msgstr "La carte de crédit va bientôt expirer"

msgid "Crypto payments may take a few minutes to be processed. The subscription update will reflect on next login."
msgstr ""
"Le traitement des paiements en cryptomonnaie peut prendre quelques minutes. "
"La mise à jour de l'abonnement sera prise en compte lors de la prochaine "
"connexion."
msgstr "Le traitement des paiements en cryptomonnaie peut prendre quelques minutes. La mise à jour de l'abonnement sera prise en compte lors de la prochaine connexion."

msgid "DAI, USDC, ETH or BTC"
msgstr "DAI, USDC, ETH ou BTC"
Expand Down Expand Up @@ -705,6 +701,9 @@ msgstr "Merci de donner un mot de passe"
msgid "Please select a file to upload"
msgstr "Merci de sélectionner un fichier à téléverser"

msgid "Please select fewer files to upload"
msgstr ""

msgid "Preview"
msgstr "Aperçu"

Expand Down Expand Up @@ -1041,6 +1040,9 @@ msgstr "Mettre à jour votre carte de crédit"
msgid "Upload"
msgstr "Téléverser"

msgid "Upload size exceeds plan capacity"
msgstr ""

msgid "Uploads cancelled"
msgstr "Téléversements annulés"

Expand Down Expand Up @@ -1153,9 +1155,7 @@ msgid "You've got a payment due. Until you've settled up, we've placed your acco
msgstr "Vous avez un paiement en retard. Tant qu'il n'a pas été réglé, l'accès à votre compte a été restreint"

msgid "Your account is restricted. Until you&apos;ve settled up, you can&apos;t upload any new content."
msgstr ""
"Votre compte est restreint. Merci de consulter les paramètres de paiement."
"Vous ne pouvez pas téléverser de nouveau contenu."
msgstr "Votre compte est restreint. Merci de consulter les paramètres de paiement.Vous ne pouvez pas téléverser de nouveau contenu."

msgid "Your content exceeds the {planStorageCapacity} storage capacity for this plan."
msgstr "Votre contenu dépasse la capacité de stockage de {planStorageCapacity} de cette formule."
Expand Down
6 changes: 6 additions & 0 deletions packages/files-ui/src/locales/no/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,9 @@ msgstr "Angi et passord"
msgid "Please select a file to upload"
msgstr "Velg en fil å laste opp"

msgid "Please select fewer files to upload"
msgstr ""

msgid "Preview"
msgstr "Forhåndsvis"

Expand Down Expand Up @@ -1033,6 +1036,9 @@ msgstr ""
msgid "Upload"
msgstr ""

msgid "Upload size exceeds plan capacity"
msgstr ""

msgid "Uploads cancelled"
msgstr ""

Expand Down