Skip to content

Commit

Permalink
Merge pull request #342 from mkoushan/master
Browse files Browse the repository at this point in the history
Add batch upload script to ui
  • Loading branch information
ddvk authored Nov 22, 2024
2 parents cf4bd69 + c2e1e2e commit 97ce715
Showing 1 changed file with 46 additions and 22 deletions.
68 changes: 46 additions & 22 deletions ui/src/pages/Documents/Upload.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { useMemo, useState } from 'react';
import { useDropzone } from 'react-dropzone';
import Spinner from 'react-bootstrap/Spinner';
import { useMemo, useState } from "react";
import { useDropzone } from "react-dropzone";
import Spinner from "react-bootstrap/Spinner";
import { toast } from "react-toastify";
import apiservice from "../../services/api.service";

import styles from "./Documents.module.scss";

async function uploadFilesInBatches(files, uploadFolder, batchSize = 100) {
const totalFiles = files.length;
let uploadedCount = 0;

while (uploadedCount < totalFiles) {
const batch = files.slice(uploadedCount, uploadedCount + batchSize);
try {
await apiservice.upload(uploadFolder, batch);
uploadedCount += batchSize;
} catch (e) {
console.error("Batch upload error:", e);
throw e;
}
}
}

export default function StyledDropzone(props) {
const [uploading, setUploading] = useState(false);
const uploadFolder = props.uploadFolder;
Expand All @@ -14,41 +30,49 @@ export default function StyledDropzone(props) {
try {
setUploading(true);
// TODO: add loading and error handling
await apiservice.upload(uploadFolder, acceptedFiles)
await uploadFilesInBatches(acceptedFiles, uploadFolder);
// await delay(100)
props.filesUploaded();
} catch (e) {
toast.error('upload error' + e.toString())
}
finally{
toast.error("upload error" + e.toString());
} finally {
setUploading(false);
}
}
};

const {
getRootProps,
getInputProps,
isDragActive,
isDragAccept,
isDragReject
} = useDropzone({ accept: 'application/pdf, application/zip, application/epub+zip', onDropAccepted: onDrop });
isDragReject,
} = useDropzone({
accept: "application/pdf, application/zip, application/epub+zip",
onDropAccepted: onDrop,
maxSize: 1 * 1024 * 1024 * 1024, // 1GB
});

const className = useMemo(() => {
return `${styles.upload}
${isDragActive ? styles.uploadActive : ''}
${isDragAccept ? styles.uploadAccept : ''}
${isDragReject ? styles.uploadReject : ''}`
}, [isDragActive, isDragReject, isDragAccept])
return `${styles.upload}
${isDragActive ? styles.uploadActive : ""}
${isDragAccept ? styles.uploadAccept : ""}
${isDragReject ? styles.uploadReject : ""}`;
}, [isDragActive, isDragReject, isDragAccept]);

const hint = "Drag 'n' drop some files here, or click to select files to upload"
const hint =
"Drag 'n' drop some files here, or click to select files to upload";
if (!uploading) {
return (
<div {...getRootProps({ className })}>
<input {...getInputProps()} />
<p>{ hint}</p>
</div>
)
<div {...getRootProps({ className })}>
<input {...getInputProps()} />
<p>{hint}</p>
</div>
);
} else {
return <div><Spinner animation="grow" /></div>
return (
<div>
<Spinner animation="grow" />
</div>
);
}
}

0 comments on commit 97ce715

Please sign in to comment.