@andy.ian/dsh-homing 2.3.1 → 2.3.2
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/index.js +16 -12
- package/lib/files-page.mjs +16 -6
- package/lib/files.mjs +20 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -3160,20 +3160,12 @@ a{color:#8ab4f8}
|
|
|
3160
3160
|
return;
|
|
3161
3161
|
}
|
|
3162
3162
|
|
|
3163
|
-
// Markdown
|
|
3163
|
+
// 图片内联返回(Markdown 相对图片 + 文件页图片预览都用它)。
|
|
3164
|
+
// 白名单与 MIME 由 lib/files.mjs 统一维护;SVG 故意不收(同源内联会执行脚本)。
|
|
3164
3165
|
if (url.pathname === '/files/raw' && req.method === 'GET') {
|
|
3165
3166
|
const target = filebrowse.resolvePath(url.searchParams.get('path') ?? '');
|
|
3166
|
-
const
|
|
3167
|
-
|
|
3168
|
-
'.jpg': 'image/jpeg',
|
|
3169
|
-
'.jpeg': 'image/jpeg',
|
|
3170
|
-
'.gif': 'image/gif',
|
|
3171
|
-
'.webp': 'image/webp',
|
|
3172
|
-
'.bmp': 'image/bmp',
|
|
3173
|
-
};
|
|
3174
|
-
const ext = path.extname(target).toLowerCase();
|
|
3175
|
-
const contentType = typeByExt[ext];
|
|
3176
|
-
if (contentType === undefined) {
|
|
3167
|
+
const contentType = filebrowse.imageMimeOf(target);
|
|
3168
|
+
if (contentType === null) {
|
|
3177
3169
|
res.writeHead(415, { 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-store' });
|
|
3178
3170
|
res.end('不支持的图片格式\n');
|
|
3179
3171
|
return;
|
|
@@ -3259,6 +3251,18 @@ a{color:#8ab4f8}
|
|
|
3259
3251
|
res.end();
|
|
3260
3252
|
return;
|
|
3261
3253
|
}
|
|
3254
|
+
// 图片:直接渲染预览页(文件列表里点图片名就走这里);字节由 /files/raw 内联返回
|
|
3255
|
+
if (filebrowse.isImageFile(asked)) {
|
|
3256
|
+
const target = filebrowse.resolvePath(asked);
|
|
3257
|
+
let stat = null;
|
|
3258
|
+
try { stat = fs.statSync(target); } catch { stat = null; }
|
|
3259
|
+
const info = stat !== null && stat.isFile()
|
|
3260
|
+
? { path: target, sizeText: filebrowse.fmtSize(stat.size), mtime: filebrowse.fmtTime(stat.mtime), image: true }
|
|
3261
|
+
: { path: target, error: '文件不存在' };
|
|
3262
|
+
appendAudit(dir, 'file-view', { path: target, kind: 'image', ok: info.image === true });
|
|
3263
|
+
sendHtml(renderFileView({ info }));
|
|
3264
|
+
return;
|
|
3265
|
+
}
|
|
3262
3266
|
const info = filebrowse.readTextFile(asked);
|
|
3263
3267
|
const raw = url.searchParams.get('raw') === '1';
|
|
3264
3268
|
if (!raw && info.text !== undefined && markdown.isMarkdownFile(info.path)) {
|
package/lib/files-page.mjs
CHANGED
|
@@ -425,13 +425,18 @@ export function renderCompressPage({ target, archive, message, isError, done = f
|
|
|
425
425
|
/** 文件内容页(text=源码;rendered=Markdown 渲染结果) */
|
|
426
426
|
export function renderFile({ info, message, isError, rendered = null }) {
|
|
427
427
|
const isMarkdown = /\.(?:md|markdown|mdown|mkd)$/iu.test(info.path)
|
|
428
|
+
const isImage = info.image === true
|
|
428
429
|
const sourceHref = '/files/view?path=' + encodeURIComponent(info.path) + '&raw=1'
|
|
429
430
|
const renderedHref = '/files/view?path=' + encodeURIComponent(info.path)
|
|
430
|
-
const
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
431
|
+
const rawHref = '/files/raw?path=' + encodeURIComponent(info.path)
|
|
432
|
+
const downloadHref = '/files/download?path=' + encodeURIComponent(info.path)
|
|
433
|
+
const toggle = isImage
|
|
434
|
+
? `<div class="chips"><a class="chip" href="${rawHref}" target="_blank" rel="noopener noreferrer">查看原图</a><a class="chip" href="${downloadHref}">下载</a></div>`
|
|
435
|
+
: rendered
|
|
436
|
+
? `<div class="chips"><a class="chip" href="${sourceHref}">查看源码</a></div>`
|
|
437
|
+
: (isMarkdown && info.error === undefined
|
|
438
|
+
? `<div class="chips"><a class="chip" href="${renderedHref}">渲染 Markdown</a></div>`
|
|
439
|
+
: '')
|
|
435
440
|
const body = `
|
|
436
441
|
<div class="bar">
|
|
437
442
|
<h1>${escapeHtml(info.path.split(/[\\/]/u).pop())}</h1>
|
|
@@ -440,7 +445,12 @@ export function renderFile({ info, message, isError, rendered = null }) {
|
|
|
440
445
|
</div>
|
|
441
446
|
${messageHtml(message, isError)}
|
|
442
447
|
${info.error ? `<div class="msg err">${escapeHtml(info.error)}</div>` : ''}
|
|
443
|
-
${
|
|
448
|
+
${isImage && info.error === undefined
|
|
449
|
+
? `<div style="margin:10px 0;padding:10px;border-radius:10px;background:#0f1319;text-align:center">
|
|
450
|
+
<a href="${rawHref}" target="_blank" rel="noopener noreferrer"><img src="${rawHref}" alt="${escapeHtml(info.path.split(/[\\/]/u).pop())}" style="max-width:100%;height:auto;display:block;margin:0 auto;border-radius:6px"></a>
|
|
451
|
+
<p class="crumbs" style="margin-top:8px">点图片可在新标签打开原图${info.sizeText ? ` · ${escapeHtml(info.sizeText)}` : ''}</p>
|
|
452
|
+
</div>`
|
|
453
|
+
: (rendered ? `<article class="md-body">${rendered.html}</article>` : (info.text !== undefined ? `<pre>${escapeHtml(info.text)}</pre>` : ''))}
|
|
444
454
|
<div class="row">
|
|
445
455
|
<a class="btn ghost" href="/files?path=${encodeURIComponent(info.path.replace(/[\\/][^\\/]*$/u, '') || '/')}"
|
|
446
456
|
style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">返回目录</a>
|
package/lib/files.mjs
CHANGED
|
@@ -275,6 +275,26 @@ export function runnableKind(file) {
|
|
|
275
275
|
return null
|
|
276
276
|
}
|
|
277
277
|
|
|
278
|
+
/**
|
|
279
|
+
* 可在线预览的图片类型(白名单)。
|
|
280
|
+
* 只收浏览器原生能渲染、且不执行脚本的格式:SVG 故意不收
|
|
281
|
+
* (直接同源打开 SVG 里的脚本会执行,而 /files/raw 是内联返回)。
|
|
282
|
+
*/
|
|
283
|
+
const IMAGE_MIME = new Map([
|
|
284
|
+
['.png', 'image/png'],
|
|
285
|
+
['.jpg', 'image/jpeg'],
|
|
286
|
+
['.jpeg', 'image/jpeg'],
|
|
287
|
+
['.gif', 'image/gif'],
|
|
288
|
+
['.webp', 'image/webp'],
|
|
289
|
+
['.bmp', 'image/bmp'],
|
|
290
|
+
])
|
|
291
|
+
|
|
292
|
+
/** 是不是可在线预览的图片文件 */
|
|
293
|
+
export const isImageFile = (name) => IMAGE_MIME.has(path.extname(String(name ?? '')).toLowerCase())
|
|
294
|
+
|
|
295
|
+
/** 图片 MIME;不是白名单图片格式返回 null */
|
|
296
|
+
export const imageMimeOf = (name) => IMAGE_MIME.get(path.extname(String(name ?? '')).toLowerCase()) ?? null
|
|
297
|
+
|
|
278
298
|
/** 读文本文件;二进制或过大时不返回正文。 */
|
|
279
299
|
export function readTextFile(absPath, limit = MAX_READ) {
|
|
280
300
|
const target = resolvePath(absPath)
|