2023-12-07 23:11:51 +01:00
|
|
|
const path = require("path");
|
|
|
|
const fs = require("fs");
|
|
|
|
const { getType } = require("mime");
|
|
|
|
const { User } = require("../../models/user");
|
2024-01-15 01:53:44 +01:00
|
|
|
const { normalizePath } = require(".");
|
2023-12-07 23:11:51 +01:00
|
|
|
|
|
|
|
function fetchPfp(pfpPath) {
|
|
|
|
if (!fs.existsSync(pfpPath)) {
|
|
|
|
return {
|
|
|
|
found: false,
|
|
|
|
buffer: null,
|
|
|
|
size: 0,
|
|
|
|
mime: "none/none",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const mime = getType(pfpPath);
|
|
|
|
const buffer = fs.readFileSync(pfpPath);
|
|
|
|
return {
|
|
|
|
found: true,
|
|
|
|
buffer,
|
|
|
|
size: buffer.length,
|
|
|
|
mime,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function determinePfpFilepath(id) {
|
|
|
|
const numberId = Number(id);
|
|
|
|
const user = await User.get({ id: numberId });
|
2023-12-12 22:11:32 +01:00
|
|
|
const pfpFilename = user?.pfpFilename || null;
|
2023-12-07 23:11:51 +01:00
|
|
|
if (!pfpFilename) return null;
|
|
|
|
|
|
|
|
const basePath = process.env.STORAGE_DIR
|
|
|
|
? path.join(process.env.STORAGE_DIR, "assets/pfp")
|
|
|
|
: path.join(__dirname, "../../storage/assets/pfp");
|
2024-01-15 01:53:44 +01:00
|
|
|
const pfpFilepath = path.join(basePath, normalizePath(pfpFilename));
|
2023-12-07 23:11:51 +01:00
|
|
|
if (!fs.existsSync(pfpFilepath)) return null;
|
|
|
|
return pfpFilepath;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
fetchPfp,
|
|
|
|
determinePfpFilepath,
|
|
|
|
};
|