72 lines
1.7 KiB
JavaScript
72 lines
1.7 KiB
JavaScript
const path = require("path");
|
|
require("dotenv").config({
|
|
path: path.resolve(
|
|
process.cwd(),
|
|
`.env.${process.env.NODE_ENV || "development"}`
|
|
),
|
|
});
|
|
|
|
var cloudinary = require("cloudinary").v2;
|
|
cloudinary.config(process.env.CLOUDINARY_URL);
|
|
|
|
exports.createSignedUploadURL = (req, res) => {
|
|
console.log("Request to create signed upload URL for Cloudinary.", req.body);
|
|
|
|
res.send(
|
|
cloudinary.utils.api_sign_request(
|
|
req.body,
|
|
process.env.CLOUDINARY_API_SECRET
|
|
)
|
|
);
|
|
};
|
|
|
|
exports.downloadFiles = (req, res) => {
|
|
const { ids } = req.body;
|
|
const url = cloudinary.utils.download_zip_url({
|
|
public_ids: ids,
|
|
flatten_folders: true,
|
|
});
|
|
res.send(url);
|
|
};
|
|
|
|
exports.renameKeys = async (req, res) => {
|
|
const { documents } = req.body;
|
|
//{id: "", from: "", to:""}
|
|
const proms = [];
|
|
console.log("Documents", documents);
|
|
documents.forEach((d) => {
|
|
proms.push(
|
|
(async () => {
|
|
try {
|
|
const res = {
|
|
id: d.id,
|
|
...(await cloudinary.uploader.rename(d.from, d.to, {
|
|
resource_type: DetermineFileType(d.type),
|
|
})),
|
|
};
|
|
return res;
|
|
} catch (error) {
|
|
return { id: d.id, from: d.from, error: error };
|
|
}
|
|
})()
|
|
);
|
|
});
|
|
|
|
let result;
|
|
|
|
result = await Promise.all(proms);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
//Also needs to be updated in upload utility and mobile app.
|
|
function DetermineFileType(filetype) {
|
|
if (!filetype) return "auto";
|
|
else if (filetype.startsWith("image")) return "image";
|
|
else if (filetype.startsWith("video")) return "video";
|
|
else if (filetype.startsWith("application/pdf")) return "image";
|
|
else if (filetype.startsWith("application")) return "raw";
|
|
|
|
return "auto";
|
|
}
|