|
|
|
@ -244,6 +244,102 @@ function workspaceEndpoints(app) { |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.post( |
|
|
|
"/workspace/:slug/upload", |
|
|
|
[ |
|
|
|
validatedRequest, |
|
|
|
flexUserRoleValid([ROLES.admin, ROLES.manager]), |
|
|
|
handleFileUpload, |
|
|
|
], |
|
|
|
async function (request, response) { |
|
|
|
try { |
|
|
|
const deptUserRecord = await DeptUsers.get({ userId: user.id }); |
|
|
|
if (!deptUserRecord.deptUser) { |
|
|
|
return response.status(500).json({ success: false, error: "没有发现用户组织机构" }); |
|
|
|
} |
|
|
|
const Collector = new CollectorApi(); |
|
|
|
const { originalname } = request.file; |
|
|
|
const processingOnline = await Collector.online(); |
|
|
|
|
|
|
|
if (!processingOnline) { |
|
|
|
return response.status(500).json({ |
|
|
|
success: false, |
|
|
|
error: `Document processing API is not online. Document ${originalname} will not be processed automatically.`, |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
// 处理文档
|
|
|
|
const { success, reason, documents, fileContent } = |
|
|
|
await Collector.processDocument(originalname); |
|
|
|
if (!success) { |
|
|
|
return response.status(500).json({ success: false, error: reason }); |
|
|
|
} |
|
|
|
|
|
|
|
// 确定目标目录
|
|
|
|
const targetDir = |
|
|
|
process.env.NODE_ENV === "development" |
|
|
|
? path.resolve(__dirname, "../../server/storage/localFile") |
|
|
|
: path.resolve(process.env.STORAGE_DIR, "localFile"); |
|
|
|
|
|
|
|
// 确保目标目录存在
|
|
|
|
if (!fs.existsSync(targetDir)) { |
|
|
|
fs.mkdirSync(targetDir, { recursive: true }); // 递归创建目录
|
|
|
|
} |
|
|
|
|
|
|
|
// 保存文件
|
|
|
|
const filePath = path.join(targetDir, originalname); // 使用原始文件名
|
|
|
|
const fileBuffer = Buffer.from(fileContent, "base64"); |
|
|
|
fs.writeFileSync(filePath, fileBuffer); |
|
|
|
|
|
|
|
console.log(`文件保存成功!路径:${filePath}`); |
|
|
|
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
// 假设路径字符串
|
|
|
|
const location = documents[0].location; |
|
|
|
// 将路径中的反斜杠替换为正斜杠(可选,但通常更通用)
|
|
|
|
const unixStylePath = location.replace(/\\/g, '/'); |
|
|
|
// 找到最后一个目录分隔符的位置
|
|
|
|
const lastIndex = unixStylePath.lastIndexOf('/'); |
|
|
|
// 提取文件名
|
|
|
|
const parsedFileName = unixStylePath.substring(lastIndex + 1); |
|
|
|
const fileExtension = path.extname(request.file.path).toLowerCase(); |
|
|
|
const sourceFile = path.resolve(__dirname, request.file.destination, request.file.originalname); |
|
|
|
const newFileName = uuidv4() + fileExtension; // 新文件名
|
|
|
|
moveAndRenameFile(sourceFile, targetDir, newFileName); |
|
|
|
const deptDocData = { |
|
|
|
deptId: deptUserRecord.deptUser.deptId, |
|
|
|
parsedFileName: parsedFileName, |
|
|
|
parsedFilePath: location, |
|
|
|
realFileName: originalname, |
|
|
|
realFileAlias: newFileName, |
|
|
|
realFilePath: targetDir, |
|
|
|
}; |
|
|
|
await DeptDocument.create(deptDocData); |
|
|
|
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
|
|
|
// 记录日志和发送遥测
|
|
|
|
Collector.log( |
|
|
|
`Document ${originalname} uploaded, processed, and saved successfully. It is now available in documents.` |
|
|
|
); |
|
|
|
await Telemetry.sendTelemetry("document_uploaded"); |
|
|
|
await EventLogs.logEvent( |
|
|
|
"document_uploaded", |
|
|
|
{ |
|
|
|
documentName: originalname, |
|
|
|
}, |
|
|
|
response.locals?.user?.id |
|
|
|
); |
|
|
|
|
|
|
|
// 返回成功响应
|
|
|
|
response.status(200).json({ success: true, error: null }); |
|
|
|
} catch (e) { |
|
|
|
console.error(e.message, e); |
|
|
|
response.status(500).json({ success: false, error: e.message }); |
|
|
|
} |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
app.post( |
|
|
|
"/workspace/:slug/upload-link", |
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.admin, ROLES.manager])], |
|
|
|
|