From fd7ee790c774ce2c5c09e4448b8e45574f22272d Mon Sep 17 00:00:00 2001 From: unitwk Date: Fri, 3 Feb 2023 17:56:48 +0800 Subject: [PATCH] Feat: add chmod file func --- src/routers/file_router.ts | 13 +++++++++++++ src/service/system_file.ts | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/routers/file_router.ts b/src/routers/file_router.ts index 3a8ac39..254ced9 100755 --- a/src/routers/file_router.ts +++ b/src/routers/file_router.ts @@ -7,6 +7,7 @@ import InstanceSubsystem from "../service/system_instance"; import { getFileManager } from "../service/file_router_service"; import { globalConfiguration, globalEnv } from "../entity/config"; import os from "os"; + // Some routers operate router authentication middleware routerApp.use((event, ctx, data, next) => { if (event.startsWith("file/")) { @@ -34,6 +35,18 @@ routerApp.on("file/list", (ctx, data) => { } }); +// File chmod (only Linux) +routerApp.on("file/chmod", async (ctx, data) => { + try { + const fileManager = getFileManager(data.instanceUuid); + const { chmod, target, isDeep } = data; + await fileManager.chmod(target, chmod, isDeep); + protocol.response(ctx, true); + } catch (error) { + protocol.responseError(ctx, error); + } +}); + // Query the status of the file management system routerApp.on("file/status", (ctx, data) => { try { diff --git a/src/service/system_file.ts b/src/service/system_file.ts index 50b87e2..47e77e9 100755 --- a/src/service/system_file.ts +++ b/src/service/system_file.ts @@ -6,6 +6,7 @@ import fs from "fs-extra"; import { compress, decompress } from "../common/compress"; import iconv from "iconv-lite"; import { globalConfiguration } from "../entity/config"; +import { processWrapper } from "../common/process_tools"; const ERROR_MSG_01 = $t("system_file.illegalAccess"); const MAX_EDIT_SIZE = 1024 * 1024 * 4; @@ -92,6 +93,17 @@ export default class FileManager { }; } + async chmod(fileName: string, chmodValue: number, deep: boolean) { + if (!this.check(fileName) || isNaN(parseInt(chmodValue as any))) throw new Error(ERROR_MSG_01); + const defaultPath = "/bin/chmod"; + let file = "chmod"; + if (fs.existsSync(defaultPath)) file = defaultPath; + const params: string[] = []; + if (deep) params.push("-R"); + params.push(String(chmodValue)); + return await new processWrapper(file, params, ".", 60 * 10).start(); + } + async readFile(fileName: string) { if (!this.check(fileName)) throw new Error(ERROR_MSG_01); const absPath = this.toAbsolutePath(fileName);