Skip to content

Commit

Permalink
Support multiple file uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostya Bats committed Feb 14, 2024
1 parent ab3fac1 commit c22d4ca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
7 changes: 3 additions & 4 deletions src/backend/src/main/kotlin/org/icpclive/admin/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import org.icpclive.cds.tunning.toAdvancedProperties
import org.icpclive.data.Controllers
import org.icpclive.data.DataBus
import org.icpclive.util.sendFlow
import org.icpclive.util.sendJsonFlow
import java.nio.file.Files

fun Route.configureAdminApiRouting() {
Expand Down Expand Up @@ -130,18 +129,18 @@ fun Route.configureAdminApiRouting() {

post("/upload") {
call.adminApiAction {
var uploadedFileUrl: String? = null
val uploadedFileUrls = mutableListOf<String>()
val multipart = call.receiveMultipart()
multipart.forEachPart { partData ->
if (partData is PartData.FileItem) {
Files.write(
Config.mediaDirectory.resolve(partData.storeName),
partData.streamProvider().readBytes()
)
uploadedFileUrl = partData.storeName
uploadedFileUrls += partData.storeName
}
}
uploadedFileUrl
uploadedFileUrls
}
}
}
Expand Down
35 changes: 18 additions & 17 deletions src/frontend/admin/src/components/MediaFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,47 +43,48 @@ function MediaFiles() {

const apiGet = createApiGet(BASE_URL_BACKEND + "/media");
const [mediaFiles, setMediaFiles] = useState([]);
const [uploadedFileUrl, setUploadedFileUrl] = useState(null);
const [uploadedFileUrls, setUploadedFileUrls] = useState(null);

const loadFiles = () => {
apiGet("").then(f => setMediaFiles(f));
};

useEffect(() => {
loadFiles();
}, []);
}, [uploadedFileUrls]);

const uploadNewFile = (file) => {
// const files = e.target.files;
// if (files.length < 1) {
// errorHandler("No media file selected");
// return;
// }
const uploadNewFile = (files) => {
files = [...files];
const formData = new FormData();
formData.append("file", file);
files.forEach(file => formData.append("file", file));
fetch(BASE_URL_BACKEND + "/media/upload", {
method: "POST",
body: formData,
}).then(r => r.json()).then(r => {
if (r.status === "error") {
errorHandler("Failed to uplaod file: " + file);
errorHandler("Failed to upload files " + files.map(f => f.name).join(","));
} else if (r.status !== "ok" && !r.response) {
errorHandler("Failed to uplaod file");
errorHandler("Failed to upload files");
}
setUploadedFileUrl(r.status === "ok" && r.response ? r.response : null);
setUploadedFileUrls(r.status === "ok" && r.response ? r.response : null);
loadFiles();
});
};

return (
<Container maxWidth="lg" sx={{ pt: 2 }}>
<FileUploader handleChange={uploadNewFile} name="file" classes="media-files-uploader" />
<FileUploader
handleChange={uploadNewFile}
name="file"
classes="media-files-uploader"
multiple
/>

{uploadedFileUrl && (
<Box sx={{ pt: 1 }}>
<FileItem fileName={uploadedFileUrl} highlight />
{uploadedFileUrls && uploadedFileUrls.map(file => (
<Box sx={{ pt: 1 }} key={file}>
<FileItem fileName={file} highlight />
</Box>
)}
))}

<Box sx={{
pt: 1,
Expand Down

0 comments on commit c22d4ca

Please sign in to comment.