Skip to content

Commit

Permalink
恢复系统保护路径 添加文件回收站
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaobaidadada committed Jan 18, 2025
1 parent 3996dff commit 5f17906
Show file tree
Hide file tree
Showing 15 changed files with 340 additions and 125 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "filecat",
"version": "2.0.8",
"version": "2.1.0",
"description": "filecat 文件管理器",
"author": "xiaobaidadada",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion src/common/StringUtil.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {find_sep} from "./path_util";

export class Param {
key: string;
value?: any;
Expand Down Expand Up @@ -140,11 +142,12 @@ export function have_empty_char(str:string) {

// 删除路径后的 \ 字符
export function removeTrailingPath(str) {
const sep = find_sep(str);
// 删除尾部的空白字符
const trimmedStr = str.trimEnd();

// 如果字符串尾部是反斜杠,删除它
if (trimmedStr.endsWith('\\') && !trimmedStr.endsWith(':\\') ) {
if (trimmedStr.endsWith(sep) && !trimmedStr.endsWith(':\\') ) {
return trimmedStr.slice(0, -1);
}

Expand Down
19 changes: 12 additions & 7 deletions src/common/path_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,26 @@ function get_extname(filePath) {
return filePath.slice(lastDotIndex);
}

export function find_sep(path) {
let sep = "\\";
for (const it of path) {
if(it==="/" || it==="\\") {
sep = it;
break;
}
}
return sep;
}

/**
* windows 是 \ linux 是 /
* @param path
* @param filename
* @returns {*|string}
*/
export function path_join(path,filename){
let s_r;
// 先找到系统上的分隔符是啥
for (const it of path) {
if(it==="/" || it==="\\") {
s_r = it;
break;
}
}
let s_r = find_sep(path);
if(filename === '.') {
return path;
} else if(filename === "..") {
Expand Down
6 changes: 6 additions & 0 deletions src/common/req/setting.req.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ export class systemdPojo {
user:string; // 所属用户
isSys:boolean; // 是系统创建的(或者加入到filecat监管的)
}

export const enum status_open {
auth,
cmd,
cyc, // 垃圾站
}
2 changes: 2 additions & 0 deletions src/common/req/user.req.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export enum UserAuth {
role_manage = 29 , //角色管理
env_path_update = 30, // 环境路径管理
pty_cmd_update = 31, // pty cmd 环境更新
sys_protection_dir = 32 ,// 系统保护路径 所有用户都有的
recycle_file_save = 33, // 文件垃圾回收
}

export class UserData extends UserLogin {
Expand Down
5 changes: 4 additions & 1 deletion src/main/domain/data/data_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export enum data_common_key {
files_pre_mulu_key = "files_pre_mulu_key", // 废弃 文件目录
sys_software = "sys_software",
// extra_env_path = "extra_env_path",
// protection_directory = "sys_protection_directory",
protection_directory = "sys_protection_directory", // 系统保护路路径
self_auth_jscode = "self_auth_jscode",
navindex_video_key = "navindex_video_tag_key",
systemd_key = "systemd_key",
Expand All @@ -60,6 +60,9 @@ export enum data_common_key {
self_shell_cmd_jscode = "self_shell_cmd_jscode", // js code 文件名字
extra_env_path_list_key = "extra_env_path_list_key", // 额外环境变量 List
cmd_use_pty_key = "cmd_use_pty_key", // 使用node pty 的变量
recycle_bin_key = "recycle_bin_key", // 文件回收站目录
recycle_bin_status = "recycle_bin_status", // 文件回收站 功能是否开启

}


Expand Down
39 changes: 33 additions & 6 deletions src/main/domain/file/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {RCode} from "../../../common/Result.pojo";
import {FileCompress} from "./file.compress";
import {getFfmpeg} from "../bin/bin";
import {getFileFormat} from "../../../common/FileMenuType";
import {getEditModelType} from "../../../common/StringUtil";
import {getEditModelType, removeTrailingPath} from "../../../common/StringUtil";
import si from "systeminformation";

const archiver = require('archiver');
Expand Down Expand Up @@ -175,11 +175,35 @@ class FileService extends FileCompress {
if (!filePath) {
return Sucess("1");
}
const sysPath = path.join(settingService.getFileRootPath(token), decodeURIComponent(filePath));
let sysPath = path.join(settingService.getFileRootPath(token), decodeURIComponent(filePath));
userService.check_user_path(token,sysPath)
if (userService.protectionCheck(sysPath,token)) {
if (userService.protectionCheck(sysPath,token) || settingService.protectionCheck(sysPath)) {
return Fail("1", RCode.PROTECT_FILE);
}
// 回收站判断
if(settingService.get_recycle_bin_status()) {
let cyc_path = settingService.get_recycle_dir();
cyc_path = removeTrailingPath(cyc_path);
sysPath = removeTrailingPath(sysPath);
if(cyc_path === sysPath) {
throw "cyc dir not to del"; // 回收站不能被删除
}
if(!userService.isSubPath(cyc_path,sysPath) && !!cyc_path) {
// 如果不是回收站内的文件 不做真的删除 而是剪切 且回收站路径存在
const ext_name = path.extname(sysPath);
const fileName = path.basename(filePath, ext_name);
let p = path.join(cyc_path,`${fileName}${ext_name}`);
if(fs.existsSync(p)) {
p = path.join(cyc_path,`${fileName}_${Date.now()}${ext_name}`);
if(fs.existsSync(p)) {
throw "try again";
}
}
this.cut_exec(sysPath,p);
return Sucess("1");
}
}
// 真的删除
const stats = fs.statSync(sysPath);
if (stats.isFile()) {
fs.unlinkSync(sysPath)
Expand Down Expand Up @@ -225,12 +249,15 @@ class FileService extends FileCompress {
userService.check_user_path(token,sysPath)
userService.check_user_path(token,toSysPath)
for (const file of data.files) {
const filePath = decodeURIComponent(path.join(sysPath, file));
fs.renameSync(filePath, decodeURIComponent(path.join(toSysPath, path.basename(file))));
rimraf(filePath);
this.cut_exec(decodeURIComponent(path.join(sysPath, file)),decodeURIComponent(path.join(toSysPath, path.basename(file))))
}
}

public cut_exec(source_path:string,to_file:string) {
fs.renameSync(source_path, to_file);
rimraf(source_path);
}

public async copy(token, data?: cutCopyReq) {
if (!data) {
return;
Expand Down
Loading

0 comments on commit 5f17906

Please sign in to comment.