feat: rts file validation
This commit is contained in:
85
src/fileValidation/validate.ts
Normal file
85
src/fileValidation/validate.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import fsp from "fs/promises";
|
||||
import axios from "axios";
|
||||
import { rtsModel } from "../rts/rts.schema";
|
||||
import { getChildren } from "../file/file.service";
|
||||
import { getFileUrlS3 } from "../utils/s3";
|
||||
import generateNote from "./ai";
|
||||
import { createNote, createNoteBot } from "../note/note.service";
|
||||
|
||||
async function downloadFile(url: string, downloadPath: string) {
|
||||
try {
|
||||
const res = await axios({
|
||||
url: url,
|
||||
method: "GET",
|
||||
responseType: "stream",
|
||||
});
|
||||
|
||||
await fsp.writeFile(downloadPath, res.data);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
|
||||
async function downloadFileTree(
|
||||
basePath: string,
|
||||
recId: string,
|
||||
tenantId: string
|
||||
) {
|
||||
const rts = await rtsModel.findOne({ pid: recId });
|
||||
|
||||
async function downloadFolder(folderId: string, path: string) {
|
||||
const items = await getChildren(folderId, tenantId);
|
||||
for (const item of items) {
|
||||
if (item.mimeType == "folder") {
|
||||
const newPath = path + `/${item.name}`;
|
||||
await fsp.mkdir(newPath);
|
||||
await downloadFolder(item.pid, newPath);
|
||||
} else {
|
||||
await downloadFile(
|
||||
await getFileUrlS3(item.pid, null, false),
|
||||
path + `/${item.name}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await downloadFolder(recId, basePath);
|
||||
}
|
||||
|
||||
async function deleteFolder(path: string) {
|
||||
await fsp.rm(path, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
export async function validate(recId: string, tenantId: string) {
|
||||
const basePath = process.env.BASE_PATH || "/root/tmp/quickerPermit";
|
||||
const folderPath = basePath + `/${recId}`;
|
||||
|
||||
try {
|
||||
await fsp.mkdir(folderPath);
|
||||
await downloadFileTree(folderPath, recId, tenantId);
|
||||
|
||||
const files = await fsp.readdir(folderPath);
|
||||
|
||||
const notes: { folder: string; note: string }[] = [];
|
||||
for (const file of files) {
|
||||
const filePath = folderPath + `/${file}`;
|
||||
const stats = await fsp.stat(filePath);
|
||||
if (stats.isDirectory()) {
|
||||
const note = await generateNote(filePath);
|
||||
notes.push({ folder: file, note });
|
||||
}
|
||||
}
|
||||
|
||||
let finalNote = "";
|
||||
for (const note of notes) {
|
||||
finalNote += `${note.folder}\n`;
|
||||
finalNote += `${note.note}\n\n`;
|
||||
}
|
||||
|
||||
await createNoteBot(finalNote, recId, tenantId);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
} finally {
|
||||
deleteFolder(folderPath);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user