Skip to content

Commit

Permalink
WIP: handle empty files and upload non-empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
Samk13 committed Aug 27, 2024
1 parent cc0f7ca commit 400d2c8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright (C) 2020-2022 Northwestern University.
// Copyright (C) 2022 Graz University of Technology.
// Copyright (C) 2022 TU Wien.
// Copyright (C) 2024 KTH Royal Institute of Technology.
//
// Invenio-RDM-Records is free software; you can redistribute it and/or modify it
// under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -40,13 +41,17 @@ export const FileUploaderComponent = ({
isFileImportInProgress,
decimalSizeDisplay,
filesLocked,
allowEmptyFiles,
...uiProps
}) => {
// We extract the working copy of the draft stored as `values` in formik
const { values: formikDraft } = useFormikContext();
const filesEnabled = _get(formikDraft, "files.enabled", false);
const [warningMsg, setWarningMsg] = useState();
const lockFileUploader = !isDraftRecord && filesLocked;
const uploadErrors = _map(files, (fileState) => fileState.errors).filter(
(errors) => errors
);

const filesList = Object.values(files).map((fileState) => {
return {
Expand Down Expand Up @@ -79,6 +84,9 @@ export const FileUploaderComponent = ({
);
const maxFileStorageReached = filesSize + acceptedFilesSize > quota.maxStorage;

const emptyFiles = acceptedFiles.filter((file) => file.size === 0);
const nonEmptyFiles = acceptedFiles.filter((file) => file.size > 0);

const filesNames = _map(filesList, "name");
const duplicateFiles = acceptedFiles.filter((acceptedFile) =>
filesNames.includes(acceptedFile.name)
Expand Down Expand Up @@ -130,7 +138,26 @@ export const FileUploaderComponent = ({
</div>
);
} else {
uploadFiles(formikDraft, acceptedFiles);
if (!_isEmpty(emptyFiles) && !allowEmptyFiles) {
setWarningMsg(
<div className="content">
<Message
warning
icon="warning circle"
header="Could not upload files."
content={i18next.t(
"Empty files were not uploaded and will be removed."
)}
list={_map(emptyFiles, "name")}
/>
</div>
);
}

// Proceed with uploading the non-empty files or all files if empty files are allowed
if (allowEmptyFiles || !_isEmpty(nonEmptyFiles)) {
uploadFiles(formikDraft, allowEmptyFiles ? acceptedFiles : nonEmptyFiles);
}
}
},
multiple: true,
Expand Down Expand Up @@ -175,6 +202,20 @@ export const FileUploaderComponent = ({
>
<>
<Grid className="file-uploader">
{!_isEmpty(uploadErrors) && (
<Grid.Row>
<Grid.Column width={16}>
<Message
error
icon="warning circle"
header={i18next.t("File upload error")}
list={uploadErrors.map((error) => (
<div key={error.fileName}>{error.message}</div>
))}
/>
</Grid.Column>
</Grid.Row>
)}
<Grid.Row className="pt-10 pb-5">
{!lockFileUploader && (
<FileUploaderToolbar
Expand Down Expand Up @@ -348,6 +389,7 @@ FileUploaderComponent.propTypes = {
decimalSizeDisplay: PropTypes.bool,
filesLocked: PropTypes.bool,
permissions: PropTypes.object,
allowEmptyFiles: PropTypes.bool,
};

FileUploaderComponent.defaultProps = {
Expand All @@ -369,4 +411,5 @@ FileUploaderComponent.defaultProps = {
importButtonText: i18next.t("Import files"),
decimalSizeDisplay: true,
filesLocked: false,
allowEmptyFiles: true,
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This file is part of Invenio-RDM-Records
// Copyright (C) 2020-2023 CERN.
// Copyright (C) 2020-2022 Northwestern University.
// Copyright (C) 2024 KTH Royal Institute of Technology.
//
// Invenio-RDM-Records is free software; you can redistribute it and/or modify it
// under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -11,6 +12,7 @@ import { FileUploaderComponent } from "./FileUploader";

const mapStateToProps = (state) => {
const { links, entries } = state.files;
const allowEmptyFiles = document.getElementById("allowEmptyFiles")?.value === "true";
return {
files: entries,
links,
Expand All @@ -21,6 +23,7 @@ const mapStateToProps = (state) => {
hasParentRecord: Boolean(
state.deposit.record?.versions?.index && state.deposit.record?.versions?.index > 1
),
allowEmptyFiles,
};
};

Expand Down

0 comments on commit 400d2c8

Please sign in to comment.