@lzwme/sserver 0.0.11 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.MD +71 -108
- package/dist/assets/assets/localsend/style.css +2 -0
- package/dist/assets/assets/localsend/vendor/qr-code-styling.js +2 -0
- package/dist/assets/localsend/style.css +2 -0
- package/dist/assets/localsend/vendor/qr-code-styling.js +2 -0
- package/dist/cli.js +9 -6
- package/dist/config.d.ts +38 -1
- package/dist/config.js +54 -9
- package/dist/lib/get-cert.js +1 -1
- package/dist/lib/merge-config.d.ts +9 -0
- package/dist/lib/merge-config.js +66 -0
- package/dist/lib/utils.d.ts +27 -1
- package/dist/lib/utils.js +94 -14
- package/dist/localsend/audit.d.ts +36 -0
- package/dist/localsend/audit.js +104 -0
- package/dist/localsend/auth.d.ts +94 -0
- package/dist/localsend/auth.js +227 -0
- package/dist/localsend/cache.d.ts +34 -0
- package/dist/localsend/cache.js +269 -0
- package/dist/localsend/chunk.d.ts +114 -0
- package/dist/localsend/chunk.js +475 -0
- package/dist/localsend/cleaner.d.ts +19 -0
- package/dist/localsend/cleaner.js +80 -0
- package/dist/localsend/client/app.js +4131 -0
- package/dist/localsend/client/crypto.js +233 -0
- package/dist/localsend/client/favicon.png +0 -0
- package/dist/localsend/client/icons/icon-192.png +0 -0
- package/dist/localsend/client/icons/icon-512.png +0 -0
- package/dist/localsend/client/index.html +451 -0
- package/dist/localsend/client/manifest.webmanifest +33 -0
- package/dist/localsend/client/sw.js +128 -0
- package/dist/localsend/guard.d.ts +58 -0
- package/dist/localsend/guard.js +210 -0
- package/dist/localsend/index.d.ts +52 -0
- package/dist/localsend/index.js +1771 -0
- package/dist/localsend/manage.d.ts +96 -0
- package/dist/localsend/manage.js +304 -0
- package/dist/localsend/metrics.d.ts +26 -0
- package/dist/localsend/metrics.js +58 -0
- package/dist/localsend/paste.d.ts +72 -0
- package/dist/localsend/paste.js +125 -0
- package/dist/localsend/path.d.ts +33 -0
- package/dist/localsend/path.js +151 -0
- package/dist/localsend/query.d.ts +78 -0
- package/dist/localsend/query.js +147 -0
- package/dist/localsend/secret.d.ts +31 -0
- package/dist/localsend/secret.js +96 -0
- package/dist/localsend/settings.d.ts +53 -0
- package/dist/localsend/settings.js +285 -0
- package/dist/localsend/share.d.ts +35 -0
- package/dist/localsend/share.js +117 -0
- package/dist/localsend/temp.d.ts +10 -0
- package/dist/localsend/temp.js +47 -0
- package/dist/localsend/ticket.d.ts +40 -0
- package/dist/localsend/ticket.js +98 -0
- package/dist/localsend/trash.d.ts +39 -0
- package/dist/localsend/trash.js +261 -0
- package/dist/localsend/webdav.d.ts +50 -0
- package/dist/localsend/webdav.js +530 -0
- package/dist/localsend/zip.d.ts +20 -0
- package/dist/localsend/zip.js +186 -0
- package/dist/proxy/php-proxy.js +31 -20
- package/dist/static-server.js +10 -3
- package/package.json +13 -5
- package/dist/localsend/index.html +0 -531
- package/dist/localsend.d.ts +0 -19
- package/dist/localsend.js +0 -217
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/** 批量重命名支持的规则类型 */
|
|
2
|
+
export type RenameMode = 'prefix' | 'suffix' | 'replace' | 'sequence';
|
|
3
|
+
export interface RenameRule {
|
|
4
|
+
/** 规则类型 */
|
|
5
|
+
mode: RenameMode;
|
|
6
|
+
/** prefix:添加到文件名之前 */
|
|
7
|
+
prefix?: string;
|
|
8
|
+
/** suffix:添加到文件名之后 */
|
|
9
|
+
suffix?: string;
|
|
10
|
+
/** replace:查找的文本 */
|
|
11
|
+
find?: string;
|
|
12
|
+
/** replace:替换成的文本 */
|
|
13
|
+
replace?: string;
|
|
14
|
+
/** sequence:起始序号 */
|
|
15
|
+
start?: number;
|
|
16
|
+
/** sequence:序号补零位数 */
|
|
17
|
+
padding?: number;
|
|
18
|
+
/** sequence:名称模板,{n} 代表序号、{name} 代表原文件名,默认「{n}」 */
|
|
19
|
+
template?: string;
|
|
20
|
+
/** 只处理该扩展名的文件(不含点,小写),空表示全部 */
|
|
21
|
+
ext?: string;
|
|
22
|
+
}
|
|
23
|
+
/** 一次成功的文件操作 */
|
|
24
|
+
export interface MoveResult {
|
|
25
|
+
/** 原相对路径 */
|
|
26
|
+
from: string;
|
|
27
|
+
/** 操作后的相对路径 */
|
|
28
|
+
to: string;
|
|
29
|
+
}
|
|
30
|
+
/** 一次失败的文件操作 */
|
|
31
|
+
export interface MoveFailure {
|
|
32
|
+
/** 文件相对路径 */
|
|
33
|
+
name: string;
|
|
34
|
+
/** 失败原因 */
|
|
35
|
+
msg: string;
|
|
36
|
+
}
|
|
37
|
+
/** 批量移动的结果 */
|
|
38
|
+
export interface MoveOutcome {
|
|
39
|
+
moved: MoveResult[];
|
|
40
|
+
failed: MoveFailure[];
|
|
41
|
+
}
|
|
42
|
+
/** 批量重命名的结果 */
|
|
43
|
+
export interface BatchRenameOutcome {
|
|
44
|
+
renamed: MoveResult[];
|
|
45
|
+
/** 名称未变化或不符合扩展名过滤条件而跳过的文件 */
|
|
46
|
+
skipped: string[];
|
|
47
|
+
failed: MoveFailure[];
|
|
48
|
+
}
|
|
49
|
+
/** 本批次已占用的相对路径集合,避免批内重名 */
|
|
50
|
+
export type TakenSet = Set<string>;
|
|
51
|
+
/**
|
|
52
|
+
* 同名文件自动追加 _1/_2 后缀(支持子目录,相对路径用 `/` 分隔)。
|
|
53
|
+
*
|
|
54
|
+
* taken 用于把本批次已经占用的名字也算作冲突,
|
|
55
|
+
* 让批量操作不会把后面的文件顶掉前面的。
|
|
56
|
+
*/
|
|
57
|
+
export declare function uniqueName(baseDir: string, relName: string, taken?: TakenSet): string;
|
|
58
|
+
/**
|
|
59
|
+
* 解析批量重命名规则,非法字段一律收敛到安全默认值。
|
|
60
|
+
*/
|
|
61
|
+
export declare function parseRenameRule(raw: unknown): RenameRule;
|
|
62
|
+
/**
|
|
63
|
+
* 把重命名规则应用到文件名(不含扩展名)上。
|
|
64
|
+
*
|
|
65
|
+
* sequence 模式的模板支持两个占位符:
|
|
66
|
+
* - {n}:序号(按 padding 补零)
|
|
67
|
+
* - {name}:原文件名
|
|
68
|
+
*/
|
|
69
|
+
export declare function applyRenameRule(base: string, rule: RenameRule, index: number): string;
|
|
70
|
+
/**
|
|
71
|
+
* 重命名(或移动)单个文件。
|
|
72
|
+
*
|
|
73
|
+
* to 可以是「同目录下的新文件名」,也可以是「目录/新文件名」,
|
|
74
|
+
* 后者即跨目录移动;目标已存在时自动追加 `_1` 后缀。
|
|
75
|
+
*/
|
|
76
|
+
export declare function renameFile(baseDir: string, from: string, to: string): {
|
|
77
|
+
code: number;
|
|
78
|
+
msg?: string;
|
|
79
|
+
from?: string;
|
|
80
|
+
to?: string;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* 把多个文件移动到指定目录。
|
|
84
|
+
*
|
|
85
|
+
* dir 为空或 ROOT_DIR 表示移动到根目录,目标目录不存在时自动创建;
|
|
86
|
+
* 同名文件自动追加 `_1` 后缀。
|
|
87
|
+
*/
|
|
88
|
+
export declare function moveFiles(baseDir: string, names: string[], dir: string): MoveOutcome;
|
|
89
|
+
/**
|
|
90
|
+
* 按规则批量重命名。
|
|
91
|
+
*
|
|
92
|
+
* 先把所有文件改成一个临时名,再改回最终名,
|
|
93
|
+
* 这样「a→b、b→c」这类互相覆盖的情况也能正确处理,
|
|
94
|
+
* 中途异常时临时名会被既有的临时文件清理机制回收。
|
|
95
|
+
*/
|
|
96
|
+
export declare function batchRename(baseDir: string, names: string[], rule: RenameRule): BatchRenameOutcome;
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uniqueName = uniqueName;
|
|
4
|
+
exports.parseRenameRule = parseRenameRule;
|
|
5
|
+
exports.applyRenameRule = applyRenameRule;
|
|
6
|
+
exports.renameFile = renameFile;
|
|
7
|
+
exports.moveFiles = moveFiles;
|
|
8
|
+
exports.batchRename = batchRename;
|
|
9
|
+
/*
|
|
10
|
+
* LocalSend 的文件整理(重命名 / 移动 / 批量重命名)
|
|
11
|
+
*
|
|
12
|
+
* 这里只做「纯文件操作」:整理结果直接体现在真实的目录结构上,
|
|
13
|
+
* 不引入任何元数据存储,回收站、分享、WebDAV 等既有能力都自动跟随。
|
|
14
|
+
*
|
|
15
|
+
* 所有操作都以「存储目录内的相对路径」为单位,越界路径一律拒绝;
|
|
16
|
+
* 目标重名时自动追加 `_1` 后缀,与上传时的处理保持一致。
|
|
17
|
+
*/
|
|
18
|
+
const node_crypto_1 = require("node:crypto");
|
|
19
|
+
const node_fs_1 = require("node:fs");
|
|
20
|
+
const node_path_1 = require("node:path");
|
|
21
|
+
const path_1 = require("./path");
|
|
22
|
+
const query_1 = require("./query");
|
|
23
|
+
/**
|
|
24
|
+
* 同名文件自动追加 _1/_2 后缀(支持子目录,相对路径用 `/` 分隔)。
|
|
25
|
+
*
|
|
26
|
+
* taken 用于把本批次已经占用的名字也算作冲突,
|
|
27
|
+
* 让批量操作不会把后面的文件顶掉前面的。
|
|
28
|
+
*/
|
|
29
|
+
function uniqueName(baseDir, relName, taken) {
|
|
30
|
+
const rel = (0, path_1.safeRelPath)(relName);
|
|
31
|
+
if (!rel)
|
|
32
|
+
return '';
|
|
33
|
+
const parts = rel.split('/');
|
|
34
|
+
const fileName = parts.pop();
|
|
35
|
+
const subDir = (0, node_path_1.join)(baseDir, ...parts);
|
|
36
|
+
const ext = (0, node_path_1.extname)(fileName);
|
|
37
|
+
const base = fileName.slice(0, fileName.length - ext.length);
|
|
38
|
+
let finalName = fileName;
|
|
39
|
+
let i = 1;
|
|
40
|
+
while ((0, node_fs_1.existsSync)((0, node_path_1.join)(subDir, finalName)) || (taken?.has(parts.length ? `${parts.join('/')}/${finalName}` : finalName) ?? false)) {
|
|
41
|
+
finalName = `${base}_${i}${ext}`;
|
|
42
|
+
i += 1;
|
|
43
|
+
}
|
|
44
|
+
return parts.length ? `${parts.join('/')}/${finalName}` : finalName;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* 解析批量重命名规则,非法字段一律收敛到安全默认值。
|
|
48
|
+
*/
|
|
49
|
+
function parseRenameRule(raw) {
|
|
50
|
+
const input = (raw ?? {});
|
|
51
|
+
const modeRaw = String(input.mode ?? 'prefix');
|
|
52
|
+
const mode = modeRaw === 'suffix' || modeRaw === 'replace' || modeRaw === 'sequence' ? modeRaw : 'prefix';
|
|
53
|
+
const start = Number(input.start);
|
|
54
|
+
const padding = Number(input.padding);
|
|
55
|
+
return {
|
|
56
|
+
mode,
|
|
57
|
+
prefix: String(input.prefix ?? ''),
|
|
58
|
+
suffix: String(input.suffix ?? ''),
|
|
59
|
+
find: String(input.find ?? ''),
|
|
60
|
+
replace: String(input.replace ?? ''),
|
|
61
|
+
start: Number.isFinite(start) ? Math.trunc(start) : 1,
|
|
62
|
+
padding: Number.isFinite(padding) ? Math.min(Math.max(Math.trunc(padding), 0), 10) : 0,
|
|
63
|
+
template: String(input.template ?? '{n}'),
|
|
64
|
+
ext: String(input.ext ?? '')
|
|
65
|
+
.trim()
|
|
66
|
+
.replace(/^\./, '')
|
|
67
|
+
.toLowerCase(),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 把重命名规则应用到文件名(不含扩展名)上。
|
|
72
|
+
*
|
|
73
|
+
* sequence 模式的模板支持两个占位符:
|
|
74
|
+
* - {n}:序号(按 padding 补零)
|
|
75
|
+
* - {name}:原文件名
|
|
76
|
+
*/
|
|
77
|
+
function applyRenameRule(base, rule, index) {
|
|
78
|
+
switch (rule.mode) {
|
|
79
|
+
case 'prefix':
|
|
80
|
+
return `${rule.prefix ?? ''}${base}`;
|
|
81
|
+
case 'suffix':
|
|
82
|
+
return `${base}${rule.suffix ?? ''}`;
|
|
83
|
+
case 'replace': {
|
|
84
|
+
const find = rule.find ?? '';
|
|
85
|
+
// 查找为空时不做替换,避免把整段名字清掉
|
|
86
|
+
if (!find)
|
|
87
|
+
return base;
|
|
88
|
+
return base.split(find).join(rule.replace ?? '');
|
|
89
|
+
}
|
|
90
|
+
case 'sequence': {
|
|
91
|
+
const start = rule.start ?? 1;
|
|
92
|
+
const num = rule.padding ? String(start + index).padStart(rule.padding, '0') : String(start + index);
|
|
93
|
+
return (rule.template || '{n}').replace('{n}', num).replace('{name}', base);
|
|
94
|
+
}
|
|
95
|
+
default:
|
|
96
|
+
return base;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 重命名(或移动)单个文件。
|
|
101
|
+
*
|
|
102
|
+
* to 可以是「同目录下的新文件名」,也可以是「目录/新文件名」,
|
|
103
|
+
* 后者即跨目录移动;目标已存在时自动追加 `_1` 后缀。
|
|
104
|
+
*/
|
|
105
|
+
function renameFile(baseDir, from, to) {
|
|
106
|
+
const srcRel = (0, path_1.safeRelPath)(from);
|
|
107
|
+
if (!srcRel)
|
|
108
|
+
return { code: 1, msg: '非法的文件路径' };
|
|
109
|
+
const srcAbs = (0, path_1.resolveInDir)(baseDir, srcRel);
|
|
110
|
+
if (!srcAbs || !(0, node_fs_1.existsSync)(srcAbs))
|
|
111
|
+
return { code: 1, msg: '文件不存在' };
|
|
112
|
+
const rawTo = String(to ?? '').trim();
|
|
113
|
+
if (!rawTo)
|
|
114
|
+
return { code: 1, msg: '请填写新的名称' };
|
|
115
|
+
let targetRel = (0, path_1.safeRelPath)(rawTo);
|
|
116
|
+
if (!targetRel)
|
|
117
|
+
return { code: 1, msg: '非法的目标名称' };
|
|
118
|
+
// 目标以分隔符结尾,或指向一个已存在的目录时,视为「移动到该目录」
|
|
119
|
+
const looksLikeDir = rawTo.endsWith('/') || rawTo.endsWith('\\');
|
|
120
|
+
const candidateAbs = (0, path_1.resolveInDir)(baseDir, targetRel);
|
|
121
|
+
if (looksLikeDir || (candidateAbs && (0, node_fs_1.existsSync)(candidateAbs) && (0, node_fs_1.statSync)(candidateAbs).isDirectory())) {
|
|
122
|
+
targetRel = `${targetRel}/${(0, node_path_1.basename)(srcRel)}`;
|
|
123
|
+
}
|
|
124
|
+
const dstAbs = (0, path_1.resolveInDir)(baseDir, targetRel);
|
|
125
|
+
if (!dstAbs)
|
|
126
|
+
return { code: 1, msg: '非法的目标路径' };
|
|
127
|
+
// 名称没有变化时不做任何事
|
|
128
|
+
if (dstAbs === srcAbs)
|
|
129
|
+
return { code: 0, from: srcRel, to: srcRel };
|
|
130
|
+
const finalRel = uniqueName(baseDir, targetRel);
|
|
131
|
+
const finalAbs = (0, path_1.resolveInDir)(baseDir, finalRel);
|
|
132
|
+
if (!finalAbs)
|
|
133
|
+
return { code: 1, msg: '非法的目标路径' };
|
|
134
|
+
try {
|
|
135
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(finalAbs), { recursive: true });
|
|
136
|
+
(0, node_fs_1.renameSync)(srcAbs, finalAbs);
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return { code: 1, msg: '重命名失败' };
|
|
140
|
+
}
|
|
141
|
+
return { code: 0, from: srcRel, to: finalRel };
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* 把多个文件移动到指定目录。
|
|
145
|
+
*
|
|
146
|
+
* dir 为空或 ROOT_DIR 表示移动到根目录,目标目录不存在时自动创建;
|
|
147
|
+
* 同名文件自动追加 `_1` 后缀。
|
|
148
|
+
*/
|
|
149
|
+
function moveFiles(baseDir, names, dir) {
|
|
150
|
+
const moved = [];
|
|
151
|
+
const failed = [];
|
|
152
|
+
const rawDir = String(dir ?? '').trim();
|
|
153
|
+
const targetDir = rawDir && rawDir !== query_1.ROOT_DIR ? (0, path_1.safeRelPath)(rawDir) : '';
|
|
154
|
+
if (rawDir && rawDir !== query_1.ROOT_DIR && !targetDir) {
|
|
155
|
+
return { moved, failed: names.map((n) => ({ name: String(n), msg: '非法的目标目录' })) };
|
|
156
|
+
}
|
|
157
|
+
const taken = new Set();
|
|
158
|
+
for (const raw of names) {
|
|
159
|
+
const rel = (0, path_1.safeRelPath)(raw);
|
|
160
|
+
if (!rel) {
|
|
161
|
+
failed.push({ name: String(raw), msg: '非法的文件路径' });
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const src = (0, path_1.resolveInDir)(baseDir, rel);
|
|
165
|
+
if (!src || !(0, node_fs_1.existsSync)(src)) {
|
|
166
|
+
failed.push({ name: rel, msg: '文件不存在' });
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const fileName = (0, node_path_1.basename)(rel);
|
|
170
|
+
const destRel = targetDir ? `${targetDir}/${fileName}` : fileName;
|
|
171
|
+
const destAbs = (0, path_1.resolveInDir)(baseDir, destRel);
|
|
172
|
+
if (!destAbs) {
|
|
173
|
+
failed.push({ name: rel, msg: '非法的目标路径' });
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
// 已经在目标目录里,无需移动
|
|
177
|
+
if (destAbs === src) {
|
|
178
|
+
moved.push({ from: rel, to: rel });
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
const finalRel = uniqueName(baseDir, destRel, taken);
|
|
182
|
+
const finalAbs = (0, path_1.resolveInDir)(baseDir, finalRel);
|
|
183
|
+
if (!finalAbs) {
|
|
184
|
+
failed.push({ name: rel, msg: '非法的目标路径' });
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(finalAbs), { recursive: true });
|
|
189
|
+
(0, node_fs_1.renameSync)(src, finalAbs);
|
|
190
|
+
taken.add(finalRel);
|
|
191
|
+
moved.push({ from: rel, to: finalRel });
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
failed.push({ name: rel, msg: '移动失败' });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return { moved, failed };
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* 按规则批量重命名。
|
|
201
|
+
*
|
|
202
|
+
* 先把所有文件改成一个临时名,再改回最终名,
|
|
203
|
+
* 这样「a→b、b→c」这类互相覆盖的情况也能正确处理,
|
|
204
|
+
* 中途异常时临时名会被既有的临时文件清理机制回收。
|
|
205
|
+
*/
|
|
206
|
+
function batchRename(baseDir, names, rule) {
|
|
207
|
+
const renamed = [];
|
|
208
|
+
const skipped = [];
|
|
209
|
+
const failed = [];
|
|
210
|
+
const items = [];
|
|
211
|
+
// 先筛出可以处理的文件
|
|
212
|
+
for (const raw of names) {
|
|
213
|
+
const rel = (0, path_1.safeRelPath)(raw);
|
|
214
|
+
if (!rel) {
|
|
215
|
+
failed.push({ name: String(raw), msg: '非法的文件路径' });
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
const abs = (0, path_1.resolveInDir)(baseDir, rel);
|
|
219
|
+
if (!abs || !(0, node_fs_1.existsSync)(abs)) {
|
|
220
|
+
failed.push({ name: rel, msg: '文件不存在' });
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
const fileName = (0, node_path_1.basename)(rel);
|
|
224
|
+
if (rule.ext && !fileName.toLowerCase().endsWith(`.${rule.ext}`)) {
|
|
225
|
+
skipped.push(rel);
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
items.push({ rel, abs, finalRel: rel });
|
|
229
|
+
}
|
|
230
|
+
// 计算每个文件的新名字
|
|
231
|
+
let seq = 0;
|
|
232
|
+
for (const item of items) {
|
|
233
|
+
const fileName = (0, node_path_1.basename)(item.rel);
|
|
234
|
+
const ext = (0, node_path_1.extname)(fileName);
|
|
235
|
+
const base = fileName.slice(0, fileName.length - ext.length);
|
|
236
|
+
const newBase = applyRenameRule(base, rule, seq);
|
|
237
|
+
seq += 1;
|
|
238
|
+
const parent = (0, node_path_1.dirname)(item.rel);
|
|
239
|
+
const dirPrefix = parent === '.' ? '' : `${parent}/`;
|
|
240
|
+
const targetRel = `${dirPrefix}${newBase}${ext}`;
|
|
241
|
+
// 名字没变就不动它
|
|
242
|
+
if (targetRel === item.rel) {
|
|
243
|
+
skipped.push(item.rel);
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
item.finalRel = targetRel;
|
|
247
|
+
}
|
|
248
|
+
const targets = items.filter((i) => i.finalRel !== i.rel);
|
|
249
|
+
if (!targets.length)
|
|
250
|
+
return { renamed, skipped, failed };
|
|
251
|
+
// 最终名要同时避开批内重名与批外的既有文件
|
|
252
|
+
const currentNames = new Set(targets.map((i) => i.rel));
|
|
253
|
+
const taken = new Set();
|
|
254
|
+
for (const item of targets) {
|
|
255
|
+
const parts = item.finalRel.split('/');
|
|
256
|
+
const fileName = parts.pop();
|
|
257
|
+
const ext = (0, node_path_1.extname)(fileName);
|
|
258
|
+
const base = fileName.slice(0, fileName.length - ext.length);
|
|
259
|
+
const dirPrefix = parts.length ? `${parts.join('/')}/` : '';
|
|
260
|
+
// 批内文件会先被改成临时名,因此只把「批外已存在的文件」视为冲突
|
|
261
|
+
const existsOutsideBatch = (rel) => {
|
|
262
|
+
const name = rel.slice(rel.lastIndexOf('/') + 1);
|
|
263
|
+
return (0, node_fs_1.existsSync)((0, node_path_1.join)(baseDir, ...parts, name)) && !currentNames.has(rel);
|
|
264
|
+
};
|
|
265
|
+
let candidate = `${dirPrefix}${fileName}`;
|
|
266
|
+
let i = 1;
|
|
267
|
+
while (taken.has(candidate) || existsOutsideBatch(candidate)) {
|
|
268
|
+
candidate = `${dirPrefix}${base}_${i}${ext}`;
|
|
269
|
+
i += 1;
|
|
270
|
+
}
|
|
271
|
+
item.finalRel = candidate;
|
|
272
|
+
taken.add(candidate);
|
|
273
|
+
}
|
|
274
|
+
// 第一步:统一改成临时名
|
|
275
|
+
const staged = [];
|
|
276
|
+
for (const item of targets) {
|
|
277
|
+
const tempName = (0, node_crypto_1.randomBytes)(16).toString('hex');
|
|
278
|
+
const tempAbs = (0, node_path_1.join)((0, node_path_1.dirname)(item.abs), tempName);
|
|
279
|
+
try {
|
|
280
|
+
(0, node_fs_1.renameSync)(item.abs, tempAbs);
|
|
281
|
+
staged.push({ tempAbs, finalAbs: (0, path_1.resolveInDir)(baseDir, item.finalRel), item });
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
failed.push({ name: item.rel, msg: '重命名失败' });
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
// 第二步:改回最终名
|
|
288
|
+
for (const stage of staged) {
|
|
289
|
+
const finalAbs = stage.finalAbs;
|
|
290
|
+
if (!finalAbs) {
|
|
291
|
+
failed.push({ name: stage.item.rel, msg: '非法的目标路径' });
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
try {
|
|
295
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(finalAbs), { recursive: true });
|
|
296
|
+
(0, node_fs_1.renameSync)(stage.tempAbs, finalAbs);
|
|
297
|
+
renamed.push({ from: stage.item.rel, to: stage.item.finalRel });
|
|
298
|
+
}
|
|
299
|
+
catch {
|
|
300
|
+
failed.push({ name: stage.item.rel, msg: '重命名失败' });
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return { renamed, skipped, failed };
|
|
304
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** 一组耗时的概要统计 */
|
|
2
|
+
export interface TimerSummary {
|
|
3
|
+
count: number;
|
|
4
|
+
/** 最近采样的中位数(毫秒) */
|
|
5
|
+
p50: number;
|
|
6
|
+
/** 最近采样的 95 分位(毫秒) */
|
|
7
|
+
p95: number;
|
|
8
|
+
max: number;
|
|
9
|
+
}
|
|
10
|
+
export interface MetricsSnapshot {
|
|
11
|
+
/** 服务启动时间戳 */
|
|
12
|
+
startedAt: number;
|
|
13
|
+
/** 计数器 */
|
|
14
|
+
counters: Record<string, number>;
|
|
15
|
+
/** 耗时统计 */
|
|
16
|
+
timers: Record<string, TimerSummary>;
|
|
17
|
+
}
|
|
18
|
+
export interface Metrics {
|
|
19
|
+
/** 计数 +1 或 +n */
|
|
20
|
+
inc: (name: string, value?: number) => void;
|
|
21
|
+
/** 记录一次耗时(毫秒) */
|
|
22
|
+
observe: (name: string, ms: number) => void;
|
|
23
|
+
/** 读取快照 */
|
|
24
|
+
snapshot: () => MetricsSnapshot;
|
|
25
|
+
}
|
|
26
|
+
export declare function createMetrics(): Metrics;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* LocalSend 运行指标
|
|
4
|
+
*
|
|
5
|
+
* 只做最朴素的两件事:计数与耗时分布。
|
|
6
|
+
* 目的不是做监控系统,而是让「为什么大家传不上去」「合并慢不慢」这类问题
|
|
7
|
+
* 能用一个接口回答,而不是靠用户口述。
|
|
8
|
+
*
|
|
9
|
+
* 耗时只保留最近 N 次采样(环形裁剪),因此内存占用恒定。
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.createMetrics = createMetrics;
|
|
13
|
+
/** 每个耗时指标的采样上限:足够算分位,又不会无限增长 */
|
|
14
|
+
const MAX_SAMPLES = 200;
|
|
15
|
+
/** 计算分位(入参需已排序) */
|
|
16
|
+
function percentile(sorted, p) {
|
|
17
|
+
if (!sorted.length)
|
|
18
|
+
return 0;
|
|
19
|
+
const index = Math.min(sorted.length - 1, Math.max(0, Math.ceil((p / 100) * sorted.length) - 1));
|
|
20
|
+
return Math.round(sorted[index] * 10) / 10;
|
|
21
|
+
}
|
|
22
|
+
function createMetrics() {
|
|
23
|
+
const counters = new Map();
|
|
24
|
+
const timers = new Map();
|
|
25
|
+
const startedAt = Date.now();
|
|
26
|
+
return {
|
|
27
|
+
inc(name, value = 1) {
|
|
28
|
+
counters.set(name, (counters.get(name) || 0) + value);
|
|
29
|
+
},
|
|
30
|
+
observe(name, ms) {
|
|
31
|
+
if (!Number.isFinite(ms) || ms < 0)
|
|
32
|
+
return;
|
|
33
|
+
const list = timers.get(name) || [];
|
|
34
|
+
list.push(ms);
|
|
35
|
+
// 超出上限时丢掉最早的采样,保持内存恒定
|
|
36
|
+
if (list.length > MAX_SAMPLES)
|
|
37
|
+
list.splice(0, list.length - MAX_SAMPLES);
|
|
38
|
+
timers.set(name, list);
|
|
39
|
+
},
|
|
40
|
+
snapshot() {
|
|
41
|
+
const timerOut = {};
|
|
42
|
+
for (const [name, list] of timers) {
|
|
43
|
+
const sorted = list.slice().sort((a, b) => a - b);
|
|
44
|
+
timerOut[name] = {
|
|
45
|
+
count: list.length,
|
|
46
|
+
p50: percentile(sorted, 50),
|
|
47
|
+
p95: percentile(sorted, 95),
|
|
48
|
+
max: Math.round((sorted[sorted.length - 1] || 0) * 10) / 10,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
startedAt,
|
|
53
|
+
counters: Object.fromEntries(counters),
|
|
54
|
+
timers: timerOut,
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/** 剪贴板内容的类型 */
|
|
2
|
+
export type PasteKind = 'text' | 'image';
|
|
3
|
+
/** 文件名生成策略 */
|
|
4
|
+
export type NameStrategy = 'hash' | 'timestamp' | 'custom';
|
|
5
|
+
/** 纯文本内容的大小上限(字节),避免把整份日志塞进来 */
|
|
6
|
+
export declare const MAX_TEXT_SIZE: number;
|
|
7
|
+
/** 文本落盘时使用的默认扩展名 */
|
|
8
|
+
export declare const TEXT_EXT = "txt";
|
|
9
|
+
export interface PastePayload {
|
|
10
|
+
/** 内容类型 */
|
|
11
|
+
kind: PasteKind;
|
|
12
|
+
/** 文本内容,或图片的 data URL */
|
|
13
|
+
content: string;
|
|
14
|
+
/** 自定义文件名(strategy 为 custom 时生效) */
|
|
15
|
+
name?: string;
|
|
16
|
+
/** 存放目录(相对存储目录),空表示根目录 */
|
|
17
|
+
dir?: string;
|
|
18
|
+
/** 命名策略 */
|
|
19
|
+
strategy?: NameStrategy;
|
|
20
|
+
}
|
|
21
|
+
/** 解析后的落盘内容 */
|
|
22
|
+
export interface ParsedPaste {
|
|
23
|
+
/** 文件内容 */
|
|
24
|
+
buffer: Buffer;
|
|
25
|
+
/** 扩展名(不含点) */
|
|
26
|
+
ext: string;
|
|
27
|
+
/** MIME 类型 */
|
|
28
|
+
mime: string;
|
|
29
|
+
/** 内容哈希(md5 前 8 位) */
|
|
30
|
+
hash: string;
|
|
31
|
+
}
|
|
32
|
+
/** 取 MIME 对应的扩展名,不支持的类型返回空串 */
|
|
33
|
+
export declare function extOfMime(mime: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* 解析 data URL(形如 `data:image/png;base64,xxxx`)。
|
|
36
|
+
* 解析失败返回 null。
|
|
37
|
+
*/
|
|
38
|
+
export declare function parseDataUrl(dataUrl: string): {
|
|
39
|
+
mime: string;
|
|
40
|
+
buffer: Buffer;
|
|
41
|
+
} | null;
|
|
42
|
+
/** 内容哈希:md5 前 8 位,够短又足够区分不同内容 */
|
|
43
|
+
export declare function contentHash(buffer: Buffer): string;
|
|
44
|
+
/** 时间戳文件名片段,如 `20261118-221530` */
|
|
45
|
+
export declare function timeStampTag(date?: Date): string;
|
|
46
|
+
/**
|
|
47
|
+
* 解析落盘请求。
|
|
48
|
+
*
|
|
49
|
+
* 返回 `error` 时说明请求不合法,调用方据此给出提示。
|
|
50
|
+
*/
|
|
51
|
+
export declare function parsePaste(raw: unknown): {
|
|
52
|
+
error?: string;
|
|
53
|
+
kind: PasteKind;
|
|
54
|
+
buffer: Buffer;
|
|
55
|
+
ext: string;
|
|
56
|
+
mime: string;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* 按策略生成文件名(不含目录)。
|
|
60
|
+
*
|
|
61
|
+
* custom 策略下若没给名字,会回退到 hash 策略,
|
|
62
|
+
* 保证任何情况下都有一个合法且不会互相覆盖的名字。
|
|
63
|
+
*/
|
|
64
|
+
export declare function buildPasteName(options: {
|
|
65
|
+
kind: PasteKind;
|
|
66
|
+
hash: string;
|
|
67
|
+
stamp: string;
|
|
68
|
+
name?: string;
|
|
69
|
+
strategy: NameStrategy;
|
|
70
|
+
}, ext: string): string;
|
|
71
|
+
/** 规范化命名策略,非法值回退到 hash */
|
|
72
|
+
export declare function parseStrategy(raw: unknown): NameStrategy;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TEXT_EXT = exports.MAX_TEXT_SIZE = void 0;
|
|
4
|
+
exports.extOfMime = extOfMime;
|
|
5
|
+
exports.parseDataUrl = parseDataUrl;
|
|
6
|
+
exports.contentHash = contentHash;
|
|
7
|
+
exports.timeStampTag = timeStampTag;
|
|
8
|
+
exports.parsePaste = parsePaste;
|
|
9
|
+
exports.buildPasteName = buildPasteName;
|
|
10
|
+
exports.parseStrategy = parseStrategy;
|
|
11
|
+
/*
|
|
12
|
+
* LocalSend 剪贴板内容落盘
|
|
13
|
+
*
|
|
14
|
+
* 分享目前只能分享「已经存在于存储目录里的文件」,
|
|
15
|
+
* 想分享一段文字或一张截图,必须先手动存成文件才行。
|
|
16
|
+
*
|
|
17
|
+
* 这里把剪贴板内容(纯文本 / 截图)直接落盘成普通文件,
|
|
18
|
+
* 这样就能复用既有的上传、列表、回收站、批量打包、WebDAV 等全部能力,
|
|
19
|
+
* 前端拿到文件名后用现成的分享接口即可生成链接。
|
|
20
|
+
*
|
|
21
|
+
* 命名策略:
|
|
22
|
+
* - hash(默认):内容 md5 前 8 位,同内容天然同名,便于识别与复用
|
|
23
|
+
* - timestamp:时间戳,便于一眼看出粘贴时间
|
|
24
|
+
* - custom:完全由调用方指定
|
|
25
|
+
*/
|
|
26
|
+
const node_crypto_1 = require("node:crypto");
|
|
27
|
+
const node_path_1 = require("node:path");
|
|
28
|
+
const path_1 = require("./path");
|
|
29
|
+
/** 允许粘贴的图片类型(不含 svg:脚本内容存在安全风险) */
|
|
30
|
+
const MIME_EXT = {
|
|
31
|
+
'image/png': 'png',
|
|
32
|
+
'image/jpeg': 'jpg',
|
|
33
|
+
'image/jpg': 'jpg',
|
|
34
|
+
'image/webp': 'webp',
|
|
35
|
+
'image/gif': 'gif',
|
|
36
|
+
'image/bmp': 'bmp',
|
|
37
|
+
'image/avif': 'avif',
|
|
38
|
+
};
|
|
39
|
+
/** 纯文本内容的大小上限(字节),避免把整份日志塞进来 */
|
|
40
|
+
exports.MAX_TEXT_SIZE = 1024 * 1024;
|
|
41
|
+
/** 文本落盘时使用的默认扩展名 */
|
|
42
|
+
exports.TEXT_EXT = 'txt';
|
|
43
|
+
/** 取 MIME 对应的扩展名,不支持的类型返回空串 */
|
|
44
|
+
function extOfMime(mime) {
|
|
45
|
+
return MIME_EXT[String(mime || '').toLowerCase()] ?? '';
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 解析 data URL(形如 `data:image/png;base64,xxxx`)。
|
|
49
|
+
* 解析失败返回 null。
|
|
50
|
+
*/
|
|
51
|
+
function parseDataUrl(dataUrl) {
|
|
52
|
+
const matched = /^data:([\w./+-]+);base64,([\s\S]*)$/i.exec(String(dataUrl || '').trim());
|
|
53
|
+
if (!matched)
|
|
54
|
+
return null;
|
|
55
|
+
const buffer = Buffer.from(matched[2], 'base64');
|
|
56
|
+
if (!buffer.length)
|
|
57
|
+
return null;
|
|
58
|
+
return { mime: matched[1].toLowerCase(), buffer };
|
|
59
|
+
}
|
|
60
|
+
/** 内容哈希:md5 前 8 位,够短又足够区分不同内容 */
|
|
61
|
+
function contentHash(buffer) {
|
|
62
|
+
return (0, node_crypto_1.createHash)('md5').update(buffer).digest('hex').slice(0, 8);
|
|
63
|
+
}
|
|
64
|
+
/** 时间戳文件名片段,如 `20261118-221530` */
|
|
65
|
+
function timeStampTag(date = new Date()) {
|
|
66
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
67
|
+
const y = date.getFullYear();
|
|
68
|
+
const mo = pad(date.getMonth() + 1);
|
|
69
|
+
const d = pad(date.getDate());
|
|
70
|
+
const h = pad(date.getHours());
|
|
71
|
+
const mi = pad(date.getMinutes());
|
|
72
|
+
const s = pad(date.getSeconds());
|
|
73
|
+
return `${y}${mo}${d}-${h}${mi}${s}`;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* 解析落盘请求。
|
|
77
|
+
*
|
|
78
|
+
* 返回 `error` 时说明请求不合法,调用方据此给出提示。
|
|
79
|
+
*/
|
|
80
|
+
function parsePaste(raw) {
|
|
81
|
+
const input = (raw ?? {});
|
|
82
|
+
const kind = String(input.kind ?? 'text') === 'image' ? 'image' : 'text';
|
|
83
|
+
const content = String(input.content ?? '');
|
|
84
|
+
if (kind === 'image') {
|
|
85
|
+
const parsed = parseDataUrl(content);
|
|
86
|
+
if (!parsed)
|
|
87
|
+
return { kind, buffer: Buffer.alloc(0), ext: '', mime: '', error: '不是有效的图片数据' };
|
|
88
|
+
const ext = extOfMime(parsed.mime);
|
|
89
|
+
if (!ext)
|
|
90
|
+
return { kind, buffer: parsed.buffer, ext: '', mime: parsed.mime, error: `不支持的图片格式:${parsed.mime}` };
|
|
91
|
+
return { kind, buffer: parsed.buffer, ext, mime: parsed.mime };
|
|
92
|
+
}
|
|
93
|
+
if (!content)
|
|
94
|
+
return { kind, buffer: Buffer.alloc(0), ext: exports.TEXT_EXT, mime: 'text/plain', error: '内容为空' };
|
|
95
|
+
const buffer = Buffer.from(content, 'utf8');
|
|
96
|
+
if (buffer.length > exports.MAX_TEXT_SIZE) {
|
|
97
|
+
return { kind, buffer, ext: exports.TEXT_EXT, mime: 'text/plain', error: `文本内容过大(上限 ${Math.floor(exports.MAX_TEXT_SIZE / 1024)}KB)` };
|
|
98
|
+
}
|
|
99
|
+
return { kind, buffer, ext: exports.TEXT_EXT, mime: 'text/plain' };
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* 按策略生成文件名(不含目录)。
|
|
103
|
+
*
|
|
104
|
+
* custom 策略下若没给名字,会回退到 hash 策略,
|
|
105
|
+
* 保证任何情况下都有一个合法且不会互相覆盖的名字。
|
|
106
|
+
*/
|
|
107
|
+
function buildPasteName(options, ext) {
|
|
108
|
+
const prefix = options.kind === 'image' ? '截图' : '文本';
|
|
109
|
+
if (options.strategy === 'custom' && options.name) {
|
|
110
|
+
const clean = (0, path_1.safeRelPath)(options.name);
|
|
111
|
+
if (clean) {
|
|
112
|
+
// 自定义名字只取文件名部分,目录由 dir 参数决定
|
|
113
|
+
const file = (0, node_path_1.basename)(clean);
|
|
114
|
+
return (0, node_path_1.extname)(file) ? file : `${file}.${ext}`;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (options.strategy === 'timestamp')
|
|
118
|
+
return `${prefix}-${options.stamp}.${ext}`;
|
|
119
|
+
return `${prefix}-${options.hash}.${ext}`;
|
|
120
|
+
}
|
|
121
|
+
/** 规范化命名策略,非法值回退到 hash */
|
|
122
|
+
function parseStrategy(raw) {
|
|
123
|
+
const value = String(raw ?? 'hash');
|
|
124
|
+
return value === 'timestamp' || value === 'custom' ? value : 'hash';
|
|
125
|
+
}
|