mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-11-11 01:10:11 +01:00
04399b1328
* WIP workspace pfp, CRUD functions complete * implement fetching workspace pfp in UserIcon component * update UI for workspace settings pfp * minor css refactor * WIP fixes to workspace pfp * create responseCache for workspace pfp blob to improve performance * fix cache not clearing when removing workspace pfp and remove unneeded util * load workspace image once, dont reload --------- Co-authored-by: timothycarambat <rambat1010@gmail.com>
60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
const path = require("path");
|
|
const fs = require("fs");
|
|
const { getType } = require("mime");
|
|
const { User } = require("../../models/user");
|
|
const { normalizePath } = require(".");
|
|
const { Workspace } = require("../../models/workspace");
|
|
|
|
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 });
|
|
const pfpFilename = user?.pfpFilename || null;
|
|
if (!pfpFilename) return null;
|
|
|
|
const basePath = process.env.STORAGE_DIR
|
|
? path.join(process.env.STORAGE_DIR, "assets/pfp")
|
|
: path.join(__dirname, "../../storage/assets/pfp");
|
|
const pfpFilepath = path.join(basePath, normalizePath(pfpFilename));
|
|
if (!fs.existsSync(pfpFilepath)) return null;
|
|
return pfpFilepath;
|
|
}
|
|
|
|
async function determineWorkspacePfpFilepath(slug) {
|
|
const workspace = await Workspace.get({ slug });
|
|
const pfpFilename = workspace?.pfpFilename || null;
|
|
if (!pfpFilename) return null;
|
|
|
|
const basePath = process.env.STORAGE_DIR
|
|
? path.join(process.env.STORAGE_DIR, "assets/pfp")
|
|
: path.join(__dirname, "../../storage/assets/pfp");
|
|
const pfpFilepath = path.join(basePath, normalizePath(pfpFilename));
|
|
if (!fs.existsSync(pfpFilepath)) return null;
|
|
return pfpFilepath;
|
|
}
|
|
|
|
module.exports = {
|
|
fetchPfp,
|
|
determinePfpFilepath,
|
|
determineWorkspacePfpFilepath,
|
|
};
|