diff --git a/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/fields/FileUploader/FileUploader.js b/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/fields/FileUploader/FileUploader.js index 1fd582bab..ea2db2bd5 100644 --- a/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/fields/FileUploader/FileUploader.js +++ b/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/fields/FileUploader/FileUploader.js @@ -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. @@ -40,6 +41,7 @@ export const FileUploaderComponent = ({ isFileImportInProgress, decimalSizeDisplay, filesLocked, + allowEmptyFiles, ...uiProps }) => { // We extract the working copy of the draft stored as `values` in formik @@ -80,9 +82,24 @@ export const FileUploaderComponent = ({ const maxFileStorageReached = filesSize + acceptedFilesSize > quota.maxStorage; const filesNames = _map(filesList, "name"); - const duplicateFiles = acceptedFiles.filter((acceptedFile) => - filesNames.includes(acceptedFile.name) - ); + let hasNonEmptyFiles = false; + let hasEmptyFiles = false; + let duplicateFiles = []; + let nonEmptyFiles = []; + let emptyFiles = []; + + for (const file of acceptedFiles) { + if (file.size === 0) { + hasEmptyFiles = true; + emptyFiles.push(file); + } else { + hasNonEmptyFiles = true; + nonEmptyFiles.push(file); + } + if (filesNames.includes(file.name)) { + duplicateFiles.push(file); + } + } if (maxFileNumberReached) { setWarningMsg( @@ -130,7 +147,24 @@ export const FileUploaderComponent = ({ ); } else { - uploadFiles(formikDraft, acceptedFiles); + if (!allowEmptyFiles && hasEmptyFiles) { + setWarningMsg( +
+ +
+ ); + } + + // Proceed with uploading the non-empty files or all files if empty files are allowed + if (allowEmptyFiles || hasNonEmptyFiles) { + uploadFiles(formikDraft, allowEmptyFiles ? acceptedFiles : nonEmptyFiles); + } } }, multiple: true, @@ -348,6 +382,7 @@ FileUploaderComponent.propTypes = { decimalSizeDisplay: PropTypes.bool, filesLocked: PropTypes.bool, permissions: PropTypes.object, + allowEmptyFiles: PropTypes.bool, }; FileUploaderComponent.defaultProps = { @@ -369,4 +404,5 @@ FileUploaderComponent.defaultProps = { importButtonText: i18next.t("Import files"), decimalSizeDisplay: true, filesLocked: false, + allowEmptyFiles: true, }; diff --git a/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/fields/FileUploader/index.js b/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/fields/FileUploader/index.js index deda2eae3..199983675 100644 --- a/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/fields/FileUploader/index.js +++ b/invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/fields/FileUploader/index.js @@ -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. @@ -8,9 +9,11 @@ import { connect } from "react-redux"; import { deleteFile, importParentFiles, uploadFiles } from "../../state/actions"; import { FileUploaderComponent } from "./FileUploader"; +import { getInputFromDOM } from "../../dom"; const mapStateToProps = (state) => { const { links, entries } = state.files; + const allowEmptyFiles = getInputFromDOM("records-resources-allow-empty-files"); return { files: entries, links, @@ -21,6 +24,7 @@ const mapStateToProps = (state) => { hasParentRecord: Boolean( state.deposit.record?.versions?.index && state.deposit.record?.versions?.index > 1 ), + allowEmptyFiles, }; };