Browse Source

文件上传的路径问题,本地测试可以了

master
ma-zhongxu 10 months ago
parent
commit
cbbf5e2877
  1. 6
      collector/index.js
  2. 4
      collector/utils/constants.js
  3. 4
      frontend/src/utils/constants.js
  4. 5
      server/endpoints/workspaces.js
  5. 1
      server/models/workspace.js
  6. 8
      server/utils/files/index.js
  7. 18
      server/utils/files/multer.js

6
collector/index.js

@ -72,7 +72,11 @@ app.post(
const targetFilename = path const targetFilename = path
.normalize(filename) .normalize(filename)
.replace(/^(\.\.(\/|\\|$))+/, ""); .replace(/^(\.\.(\/|\\|$))+/, "");
const inputPath = path.resolve("./hotdir");
// const inputPath = path.resolve("./hotdir");
const inputPath = process.env.NODE_ENV === "development"
? path.resolve("../server/storage/hotdir")
: path.resolve("/app/server/storage/hotdir");
console.log("输入路径:(((((((((((((((((((((((((((((((((((((((((:", inputPath,filename);
const sourceFile = path.join(inputPath, filename); // 拼接文件路径 const sourceFile = path.join(inputPath, filename); // 拼接文件路径
console.log("源文件路径:", sourceFile); console.log("源文件路径:", sourceFile);

4
collector/utils/constants.js

@ -1,4 +1,6 @@
const WATCH_DIRECTORY = require("path").resolve(__dirname, "../hotdir");
const WATCH_DIRECTORY = process.env.NODE_ENV === "development"
? require("path").resolve(__dirname, "../../server/storage/hotdir")
: require("path").resolve(__dirname, "../../../../app/server/storage/hotdir");
const ACCEPTED_MIMES = { const ACCEPTED_MIMES = {
"text/plain": [".txt", ".md", ".org", ".adoc", ".rst"], "text/plain": [".txt", ".md", ".org", ".adoc", ".rst"],

4
frontend/src/utils/constants.js

@ -1,5 +1,5 @@
// export const API_BASE = import.meta.env.VITE_API_BASE || "/api";
export const API_BASE = 'http://172.16.2.9:3001/api'
export const API_BASE = import.meta.env.VITE_API_BASE || "/api";
// export const API_BASE = 'http://172.16.2.9:3001/api'
export const ONBOARDING_SURVEY_URL = "https://onboarding.anythingllm.com"; export const ONBOARDING_SURVEY_URL = "https://onboarding.anythingllm.com";
export const AUTH_USER = "anythingllm_user"; export const AUTH_USER = "anythingllm_user";

5
server/endpoints/workspaces.js

@ -283,7 +283,6 @@ function workspaceEndpoints(app) {
process.env.NODE_ENV === "development" process.env.NODE_ENV === "development"
? path.resolve(__dirname, "../../server/storage/localFile") ? path.resolve(__dirname, "../../server/storage/localFile")
: path.resolve(process.env.STORAGE_DIR, "localFile"); : path.resolve(process.env.STORAGE_DIR, "localFile");
// 确保目标目录存在 // 确保目标目录存在
if (!fs.existsSync(targetDir)) { if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true }); // 递归创建目录 fs.mkdirSync(targetDir, { recursive: true }); // 递归创建目录
@ -294,7 +293,6 @@ function workspaceEndpoints(app) {
const fileBuffer = Buffer.from(fileContent, "base64"); const fileBuffer = Buffer.from(fileContent, "base64");
fs.writeFileSync(filePath, fileBuffer); fs.writeFileSync(filePath, fileBuffer);
console.log(`文件保存成功!路径:${filePath}`);
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// 假设路径字符串 // 假设路径字符串
const location = documents[0].location; const location = documents[0].location;
@ -305,9 +303,7 @@ function workspaceEndpoints(app) {
// 提取文件名 // 提取文件名
const parsedFileName = unixStylePath.substring(lastIndex + 1); const parsedFileName = unixStylePath.substring(lastIndex + 1);
const fileExtension = path.extname(request.file.path).toLowerCase(); const fileExtension = path.extname(request.file.path).toLowerCase();
const sourceFile = path.resolve(__dirname, request.file.destination, request.file.originalname);
const newFileName = uuidv4() + fileExtension; // 新文件名 const newFileName = uuidv4() + fileExtension; // 新文件名
moveAndRenameFile(sourceFile, targetDir, newFileName);
const deptDocData = { const deptDocData = {
deptId: deptUserRecord.deptUser.deptId, deptId: deptUserRecord.deptUser.deptId,
parsedFileName: parsedFileName, parsedFileName: parsedFileName,
@ -543,7 +539,6 @@ function workspaceEndpoints(app) {
const workspace = multiUserMode(response) const workspace = multiUserMode(response)
? await Workspace.getWithUser(user, { slug }) ? await Workspace.getWithUser(user, { slug })
: await Workspace.get({ slug }); : await Workspace.get({ slug });
console.log("workspace++++++++++++++++++++++++++++", workspace);
response.status(200).json({ workspace }); response.status(200).json({ workspace });
} catch (e) { } catch (e) {
console.error(e.message, e); console.error(e.message, e);

1
server/models/workspace.js

@ -278,7 +278,6 @@ const Workspace = {
documents: true, documents: true,
}, },
}); });
console.log("0000000000000000000000000000000000000000000000000000000000000000",workspace);
return workspace || null; return workspace || null;
} catch (error) { } catch (error) {

8
server/utils/files/index.js

@ -202,8 +202,12 @@ async function viewLocalFiles(deptId) {
// 遍历 deptDocuments // 遍历 deptDocuments
for (const doc of deptDocuments) { for (const doc of deptDocuments) {
try { try {
const filePath = doc.parsedFilePath; // 获取文件路径
if (!fs.existsSync(filePath)) continue; // 如果文件不存在,跳过
let filePath = doc.parsedFilePath; // 获取文件路径
if (!fs.existsSync(filePath)) {
filePath = process.env.NODE_ENV === "development"
? filePath
: path.resolve(process.env.STORAGE_DIR, "documents", filePath);
}
// 读取文件内容 // 读取文件内容
const rawData = fs.readFileSync(filePath, 'utf8'); const rawData = fs.readFileSync(filePath, 'utf8');

18
server/utils/files/multer.js

@ -10,12 +10,10 @@ const { normalizePath } = require(".");
*/ */
const fileUploadStorage = multer.diskStorage({ const fileUploadStorage = multer.diskStorage({
destination: function (_, __, cb) { destination: function (_, __, cb) {
console.log("进来了进来了进来了::::",process.env.NODE_ENV);
const uploadOutput = const uploadOutput =
process.env.NODE_ENV === "development"
? path.resolve(__dirname, `../../../collector/hotdir`)
: path.resolve(process.env.STORAGE_DIR, `../../collector/hotdir`);
console.log("又来了又来了又来了:::",uploadOutput)
process.env.NODE_ENV ==="development"
? path.resolve(__dirname, "../../storage/hotdir")
: path.resolve(process.env.STORAGE_DIR, "hotdir");
cb(null, uploadOutput); cb(null, uploadOutput);
}, },
filename: function (_, file, cb) { filename: function (_, file, cb) {
@ -33,9 +31,9 @@ const fileUploadStorage = multer.diskStorage({
const fileAPIUploadStorage = multer.diskStorage({ const fileAPIUploadStorage = multer.diskStorage({
destination: function (_, __, cb) { destination: function (_, __, cb) {
const uploadOutput = const uploadOutput =
process.env.NODE_ENV === "development"
? path.resolve(__dirname, `../../../collector/hotdir`)
: path.resolve(process.env.STORAGE_DIR, `../../collector/hotdir`);
process.env.NODE_ENV ==="development"
? path.resolve(__dirname, "../../storage/hotdir")
: path.resolve(process.env.STORAGE_DIR, "hotdir");
cb(null, uploadOutput); cb(null, uploadOutput);
}, },
filename: function (_, file, cb) { filename: function (_, file, cb) {
@ -174,13 +172,10 @@ function handlePfpUpload(request, response, next) {
const commUploadStorage = multer.diskStorage({ const commUploadStorage = multer.diskStorage({
destination: async function (_, __, cb) { destination: async function (_, __, cb) {
try { try {
// const uploadOutput = "D:\\code2\\anything-llm-master\\server\\storage\\real";
console.log("进来了进来了进来了::::",process.env.NODE_ENV);
const uploadOutput = const uploadOutput =
process.env.NODE_ENV === "development" process.env.NODE_ENV === "development"
? path.resolve(__dirname, `../../../server/storage/localFile`) ? path.resolve(__dirname, `../../../server/storage/localFile`)
: path.resolve(process.env.STORAGE_DIR, `../../server/storage/localFile`); : path.resolve(process.env.STORAGE_DIR, `../../server/storage/localFile`);
console.log("又来了又来了又来了:::",uploadOutput)
if (process.env.NODE_ENV !== "development" && !process.env.STORAGE_DIR) { if (process.env.NODE_ENV !== "development" && !process.env.STORAGE_DIR) {
throw new Error("STORAGE_DIR environment variable is required in production."); throw new Error("STORAGE_DIR environment variable is required in production.");
@ -201,7 +196,6 @@ const commUploadStorage = multer.diskStorage({
// 文件上传中间件 // 文件上传中间件
function handleCommUpload(request, response, next) { function handleCommUpload(request, response, next) {
console.log("进来了进来了");
const upload = multer({ storage: commUploadStorage, limits: { fileSize: 100 * 1024 * 1024 }, }).single("file"); const upload = multer({ storage: commUploadStorage, limits: { fileSize: 100 * 1024 * 1024 }, }).single("file");
upload(request, response, function (err) { upload(request, response, function (err) {

Loading…
Cancel
Save