mahdahar 462704fae7 fix(upload): harden activities uploader for Uppy requests
Support POST/OPTIONS handling and robust file discovery in Activities::upload so Uppy submissions are accepted reliably.

Align client upload endpoint/payload config and add clearer upload error handling to surface failures in the editor UI.
2026-04-20 09:51:50 +07:00

144 lines
3.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Uppy, Dashboard, XHRUpload } from "https://releases.transloadit.com/uppy/v3.15.0/uppy.min.mjs";
const uploadEndpoint = window.crmActivitiesUploadEndpoint ?? "/activities/upload";
const attachmentInput = document.querySelector("#attachment");
// var uppy = new Uppy({
// onBeforeFileAdded: (currentFile, files) => {
// //const name = Date.now() + '_' + currentFile.name
// const month = (new Date().getMonth() + 1).toString().padStart(2, '0');
// const day = (new Date().getDate()).toString().padStart(2, '0');
// const name = `${new Date().getFullYear()}_${month}_${day}_${currentFile.name}`;
// console.log(name);
// const modifiedFile = {
// ...currentFile,
// meta: {
// ...currentFile.meta,
// name
// },
// name
// };
// return modifiedFile
// }})
var uppy = new Uppy({
onBeforeFileAdded: (currentFile, files) => {
const now = new Date();
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
const seconds = now.getSeconds().toString().padStart(2, '0');
const milliseconds = now.getMilliseconds().toString().padStart(3, '0');
// Menghilangkan koma (,) dalam nama file
const sanitizedFileName = currentFile.name.replace(/,/g, '');
// Format: HHMMSSmmm_FILENAME
const name = `${hours}${minutes}${seconds}${milliseconds}_${sanitizedFileName}`;
// Membuat objek file yang dimodifikasi
const modifiedFile = {
...currentFile,
meta: {
...currentFile.meta,
name
},
name
};
return modifiedFile;
}
})
.use(Dashboard, {
inline: true,
target: "#drag-drop-area",
width: '100%',
height: 300,
})
.use(XHRUpload, {
endpoint: uploadEndpoint,
method: "post",
fieldName: "file",
formData: true,
});
uppy.on("complete", (result) => {
let array = result.successful;
let arrtext = [];
for (let i = 0; i < array.length; i++) {
let text = result.successful[i].name;
if (result.successful[i].response && result.successful[i].response.body && result.successful[i].response.body.relativePath) {
text = result.successful[i].response.body.relativePath;
}
arrtext.push(text);
}
let text = arrtext.join();
if (attachmentInput) {
$(attachmentInput).val(function(){
if (this.value === "") {
return this.value + text;
}
return this.value + "," + text;
});
}
console.log( "Upload complete! Weve uploaded these files:", result.successful);
});
uppy.on("upload-error", (file, error, response) => {
const fallbackMessage = error?.message ?? "Upload failed";
const responseMessage = response?.body?.message ?? response?.statusText ?? "";
const fileName = file?.name ?? "file";
const message = responseMessage !== "" ? responseMessage : fallbackMessage;
console.error("Uppy upload-error", {
file,
error,
response,
uploadEndpoint,
});
alert("Upload failed for " + fileName + ": " + message);
});
uppy.on("restriction-failed", (file, error) => {
console.error("Uppy restriction-failed", { file, error });
alert(error?.message ?? "File is not allowed.");
});
uppy.on("error", (error) => {
console.error("Uppy error", error);
alert(error?.message ?? "Unexpected uploader error.");
});
/*
const attachmentList = document.querySelector('#attachment').value;
const arrayx = attachmentList ? attachmentList.split(',') : [];
arrayx.forEach((fileName) => {
fetch(`<?=base_url();?>/upload/${fileName}`)
.then((response) => response.blob())
.then((blob) => {
uppy.addFile({
name: fileName.substring(11),
type: blob.type,
data: blob,
});
if (fileName === arrayx[arrayx.length - 1]) {
uppy.getFiles().forEach((file) => {
uppy.setFileState(file.id, {
progress: { uploadComplete: true, uploadStarted: true },
});
});
}
});
});
*/