Hello, my name is Bernardo, I am wanting to implement the Dropbox API to a support system. I use the example that dropbox gives on github to upload files, in my code it works but when I enter the dropbox folder the file is uploaded corrupted and it does not allow me to open or download it, I attach my code, I hope you can help me
const postTicket = async (state, worker, subject, detail, answer, userresolved, user, files) => {
// console.log("file", files)
// console.log("body", state, worker, subject, detail, answer, userresolved, user)
try {
// Creo el ticket vacio para tener el ID que le va a dar nombre a la carpeta
const ticketId = (await Ticket.create()).id;
const folderName = `ticket_${ticketId}`;
// Ruta donde se guardan los archivos en la carpeta "documents", pero ver donde lo guardamos cuando estemos en produccion
const documentsFolderPath = path.join(DEST_FILES);
const folderPath = path.join(__dirname, '../../../../public', folderName);
// Crear la carpeta si no existe
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
}
// Mueve todos los archivos con el prefijo "new_" desde la carpeta "uploads" a la carpeta del ticket
const uploadFolderPath = path.join(__dirname, '../../../../public/uploads');
const filesWithPrefix = fs.readdirSync(uploadFolderPath).filter(file => file.startsWith('new_'));
const filesArray = [];
for (const filename of filesWithPrefix) {
const sourcePath = path.join(uploadFolderPath, filename);
const destinationPath = path.join('/', filename);
// Mover el archivo a la carpeta específica del ticket
await saveInDropbox(files, sourcePath, destinationPath);
const sharedLink = await getSharedLink(destinationPath);
filesArray.push(sharedLink);
}
// Actualiza el registro en la base de datos
let setTicket = await Ticket.findByPk(ticketId)
if(user){
let setUser = await User.findOne({
where:{username: user}
})
if(setUser){
await setTicket.setUser();
await setTicket.setUser(setUser);
}
}
const newTicket = await setTicket.update({
state : state,
worker : worker,
subject : subject,
detail : detail,
answer : answer,
files: filesArray,
userresolved: userresolved
})
return newTicket
} catch (e) {
console.log("Error en controller postTicket", e.message);
throw e;
}
};
module.exports = postTicket;
and the function for uploads
const { Dropbox } = require("dropbox");
const fs = require("fs");
const path = require("path");
const saveInDropbox = async (files, sourcePath, destinationPath) => {
console.log(
"sourcePath",
sourcePath,
"destinationPath",
destinationPath,
"file",
files
);
// const UPLOAD_FILE_SIZE_LIMIT = 150 * 1024 * 1024;
let ACCESS_TOKEN = "x";
let dbx = new Dropbox({ accessToken: ACCESS_TOKEN });
try {
files.map((e) => {
dbx
.filesUpload({ path: "/" + e.filename, contents: e , autorename: true })
.then(function (response) {
console.log("respuesta de dropbox", response);
})
.catch((error) => console.log("error en dropbox", error.message));
});
} catch (error) {
console.log("resolver", error.message);
}
return false;
};
module.exports = saveInDropbox;