Feat: add chmod file func

This commit is contained in:
unitwk 2023-02-03 17:56:48 +08:00
parent 427556f004
commit fd7ee790c7
2 changed files with 25 additions and 0 deletions

View File

@ -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 {

View File

@ -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);