Hi everyone,
I’m just starting with Render.
I have a disk and i can upload a file on it. The file is also on a database.
However, i can’t get the files.
Here are the requests i use, to upload a file and to get a file.
Thanks for your help!
//Upload
const mountPath = '/var/data';
exports.upload = async (req, res) => {
let id = Math.floor((1 + Math.random()) * 0x100000000000)
.toString(16)
.substring(1);
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send('No files were uploaded.');
}
const sampleFile = req.files.file;
const destinationPath = path.join(mountPath, sampleFile.name);
sampleFile.mv(destinationPath, (err) => {
if (err) {
console.error('File upload error: ' + err);
return res.status(500).send('File upload error: ' + err);
}
db.query(
`INSERT INTO files (id, user_id, name, type, size, content, uploaded_at) VALUES (?, ?, ?, ?, ?, ?, NOW())`,
[id, req.userData.userId, sampleFile.name, sampleFile.mimetype, sampleFile.size, sampleFile.content],
(dbErr, result) => {
if (dbErr) {
console.error('Database insertion error: ' + dbErr);
return res.status(500).send('Database insertion error: ' + dbErr);
}
res.send('File uploaded and saved to the disk!');
}
);
});
};
//Get file
exports.getFile = (req, res, next) => {
const graphId = req.params.graphId;
const filePath = path.join('/var/data/', graphId)
if (fs.existsSync(filePath)) {
res.sendFile(filePath, (err) => {
if (err) {
console.error(err);
return res.status(500).send('Error while sending the file');
}
});
} else {
res.status(404).send('File not found');
}
};