한글뷰어 기능수정
This commit is contained in:
BIN
libs/hwp.js
BIN
libs/hwp.js
Binary file not shown.
@@ -1,12 +1,4 @@
|
||||
const pool = require("../db/pool.js");
|
||||
const { DeleteObjectCommand } = require("@aws-sdk/client-s3");
|
||||
const onPremiseClient = require('../config/onPremiseClient.js');
|
||||
const cloudClient = require('../config/cloudClient.js');
|
||||
const storageClients = {
|
||||
'ONPREMISE': onPremiseClient,
|
||||
'CLOUD': cloudClient
|
||||
};
|
||||
const s3 = storageClients[process.env.DEPLOYMENT_TYPE || 'ONPREMISE'];
|
||||
const env = process.env.NODE_ENV;
|
||||
const tbData = env === 'production' ? 'tb_data' : '_test_tb_data';
|
||||
|
||||
@@ -24,67 +16,67 @@ async function runAutoClean() {
|
||||
|
||||
const { limit_file_count, limit_days } = policyRes.rows[0];
|
||||
|
||||
// 2. 삭제 대상 파일 검색 (각 현장의 파일 개수가 limit_file_count 미만이고, limit_days 일이 지난 파일)
|
||||
// 2. 삭제 대상 3단계 폴더 검색
|
||||
// - is_folder = true 이고 depth = 3 이며 아직 삭제되지 않은 폴더
|
||||
// - 하위의 유효 파일 개수가 limit_file_count 미만
|
||||
// - last_folder_act_date 가 limit_days 일 이상 경과
|
||||
const targetQuery = `
|
||||
SELECT data_id, project_id, object_key, preview_key, popup_key, thumbnail_key,
|
||||
path1, path2, path3, path4, path5, path6, path7, path8, data_depth, is_folder
|
||||
FROM ver4.${tbData}
|
||||
WHERE is_folder = false AND is_removed = false
|
||||
AND project_id IN (
|
||||
SELECT project_id
|
||||
FROM ver4.${tbData}
|
||||
WHERE is_folder = false AND is_removed = false
|
||||
GROUP BY project_id
|
||||
HAVING COUNT(*) < $1
|
||||
)
|
||||
AND create_date < NOW() - CAST($2 || ' days' AS INTERVAL);
|
||||
SELECT data_id, project_id, path1, path2, path3, last_folder_act_date
|
||||
FROM ver4.${tbData} f
|
||||
WHERE f.is_folder = true
|
||||
AND f.data_depth = 3
|
||||
AND f.is_removed = false
|
||||
AND f.last_folder_act_date < NOW() - CAST($1 || ' days' AS INTERVAL)
|
||||
AND (
|
||||
SELECT COUNT(*)
|
||||
FROM ver4.${tbData} files
|
||||
WHERE files.project_id = f.project_id
|
||||
AND files.path1 = f.path1
|
||||
AND files.path2 = f.path2
|
||||
AND files.path3 = f.path3
|
||||
AND files.is_folder = false
|
||||
AND files.is_removed = false
|
||||
) < $2;
|
||||
`;
|
||||
const targets = await client.query(targetQuery, [limit_file_count, limit_days]);
|
||||
const targets = await client.query(targetQuery, [limit_days, limit_file_count]);
|
||||
|
||||
console.log(`⏰ Found ${targets.rows.length} files to clean up.`);
|
||||
console.log(`⏰ Found ${targets.rows.length} folders to clean up.`);
|
||||
|
||||
for (const file of targets.rows) {
|
||||
for (const folder of targets.rows) {
|
||||
let success = true;
|
||||
let keysToDelete = [];
|
||||
if (file.object_key) keysToDelete.push(file.object_key);
|
||||
if (file.preview_key) keysToDelete.push(file.preview_key);
|
||||
if (file.popup_key) keysToDelete.push(file.popup_key);
|
||||
if (file.thumbnail_key) keysToDelete.push(file.thumbnail_key);
|
||||
|
||||
// S3 실물 파일 삭제
|
||||
for (const key of keysToDelete) {
|
||||
try {
|
||||
const command = new DeleteObjectCommand({
|
||||
Bucket: file.project_id, // 현장 ID를 버킷 명으로 사용함
|
||||
Key: key
|
||||
});
|
||||
await s3.send(command);
|
||||
} catch (s3Err) {
|
||||
console.error(`❌ S3 Delete Error [Key: ${key}]:`, s3Err.message);
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
|
||||
// DB 메타 정보 완전 제거
|
||||
try {
|
||||
await client.query(`DELETE FROM ver4.${tbData} WHERE data_id = $1`, [file.data_id]);
|
||||
} catch (dbErr) {
|
||||
console.error(`❌ DB Delete Error [DataID: ${file.data_id}]:`, dbErr.message);
|
||||
await client.query('BEGIN');
|
||||
|
||||
// 폴더 자체 및 그 하위 파일/폴더들을 전부 Soft-Delete (is_removed = true)
|
||||
const updateQuery = `
|
||||
UPDATE ver4.${tbData}
|
||||
SET is_removed = true,
|
||||
mod_date = CURRENT_TIMESTAMP,
|
||||
mod_user_id = 'SYSTEM'
|
||||
WHERE project_id = $1
|
||||
AND path1 = $2
|
||||
AND path2 = $3
|
||||
AND path3 = $4
|
||||
AND is_removed = false;
|
||||
`;
|
||||
await client.query(updateQuery, [folder.project_id, folder.path1, folder.path2, folder.path3]);
|
||||
|
||||
await client.query('COMMIT');
|
||||
} catch (err) {
|
||||
await client.query('ROLLBACK');
|
||||
console.error(`❌ Auto Clean Folder Error [Folder ID: ${folder.data_id}]:`, err.message);
|
||||
success = false;
|
||||
}
|
||||
|
||||
// 삭제 경로 조합
|
||||
let cleanPath = '';
|
||||
for (let i = 1; i <= file.data_depth; i++) {
|
||||
if (file[`path${i}`]) cleanPath += '/' + file[`path${i}`];
|
||||
}
|
||||
|
||||
// 로그 기록
|
||||
// 삭제 경로 조합 (/path1/path2/path3)
|
||||
const cleanPath = `/${folder.path1}/${folder.path2}/${folder.path3}`;
|
||||
const criteria = `보관수량 ${limit_file_count}개 미만 / 기한 ${limit_days}일 경과`;
|
||||
|
||||
// 로그 기록
|
||||
await client.query(`
|
||||
INSERT INTO ver4.tb_auto_clean_log (clean_date, project_id, clean_path, criteria_info, result_status)
|
||||
VALUES (CURRENT_TIMESTAMP, 'SYSTEM', $1, $2, $3);
|
||||
`, [cleanPath, criteria, success ? 'SUCCESS' : 'FAILED']);
|
||||
VALUES (CURRENT_TIMESTAMP, $1, $2, $3, $4);
|
||||
`, [folder.project_id, cleanPath, criteria, success ? 'SUCCESS' : 'FAILED']);
|
||||
}
|
||||
console.log("⏰ Auto clean batch job finished successfully.");
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user