file-handle 0.0.3 → 0.0.4

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 CHANGED
@@ -79,6 +79,7 @@ file-handle
79
79
  ## License
80
80
 
81
81
  Copyright [gaoyuan]
82
+
82
83
  Link: www.npmjs.com/package/file-handle
83
84
 
84
85
  Licensed under the Apache License, Version 2.0 (the "License");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "file-handle",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "author": "jsonhandle@gmail.com",
5
5
  "description": "A simple local web file server with a user-friendly interface, allowing easy file upload, download, preview, and management through a browser. \n一个简易的文件服务器,方便通过浏览器来浏览、上传或管理目录和文件. ",
6
6
  "main": "server/index.js",
@@ -33,7 +33,7 @@
33
33
  "test": "echo \"Error: no test specified\" && exit 1"
34
34
  },
35
35
  "dependencies": {
36
- "@electron/asar": "^3.3.1",
36
+ "unzipper": "^0.12.3",
37
37
  "archiver": "^7.0.1",
38
38
  "cli-color": "^2.0.4",
39
39
  "cookie-parser": "^1.4.7",
@@ -1,23 +1,22 @@
1
1
  import { createProxyMiddleware } from 'http-proxy-middleware';
2
2
  import { loadConfig } from '../config.js';
3
- import path from 'path';
4
3
  import { resolve } from 'path';
5
4
  import { debug } from '../utils/logger.js';
6
- import asar from '@electron/asar';
7
5
  import mime from 'mime-types';
6
+ import unzip from 'unzipper';
8
7
  const isDevelopment = process.env.NODE_ENV === 'development';
9
8
  let resMap;
10
9
  export function buildPreviewerHtml(html) {
11
10
  const modifiedBody = html.replace('<base id="baseUrl" />', `<base id="baseUrl" href="/fh/" />`);
12
11
  return modifiedBody;
13
12
  }
14
- export function sendFeReq(uri, req, res, next, sendBefore) {
13
+ export async function sendFeReq(uri, req, res, next, sendBefore) {
15
14
  const config = loadConfig();
16
15
  // 获取node入口目录的绝对路径
17
16
  const fhDir = config.mdDir;
18
17
  debug('sendFeReq uri: ' + uri + ' | path: ' + req.path);
19
18
  if (!isDevelopment && !resMap) {
20
- resMap = getRes(resolve(fhDir, './fh.app.dat'));
19
+ resMap = await getRes(resolve(fhDir, './fh.app.dat'));
21
20
  }
22
21
  const bCb = !!sendBefore;
23
22
  const filePath = req.path;
@@ -70,21 +69,27 @@ export function sendFeReq(uri, req, res, next, sendBefore) {
70
69
  debug('req.path: ' + req.path);
71
70
  debug('resFilePath: ' + resFilePath);
72
71
  res.set('Content-Type', mime.lookup(resFilePath) || 'application/octet-stream');
73
- res.end(resMap.get((resFilePath).replace(/\//g, '\\')));
72
+ res.end(resMap.get((resFilePath).replace(/\\/g, '\/')));
74
73
  }
75
74
  }
76
- // 获取asar资源
77
- function getRes(asarPath) {
78
- const aFilePath = asar.listPackage(asarPath, { isPack: false });
75
+ // 获取zip资源
76
+ async function getRes(zipPath) {
77
+ const directory = await unzip.Open.file(zipPath);
79
78
  // 创建资源映射
80
79
  const resourceMap = new Map();
81
- aFilePath.forEach(filePath => {
82
- filePath = filePath.replace(/[\\\/]/g, '/').split('/').filter(s => s.trim()).join(path.sep);
83
- try {
84
- resourceMap.set(filePath, asar.extractFile(asarPath, filePath));
85
- }
86
- catch (error) { }
87
- });
88
- asar.uncacheAll();
80
+ // 处理所有文件
81
+ for (const file of directory.files) {
82
+ const chunks = [];
83
+ await new Promise((resolve, reject) => {
84
+ file.stream()
85
+ .on('data', (chunk) => chunks.push(chunk))
86
+ .on('error', reject)
87
+ .on('end', () => {
88
+ const content = Buffer.concat(chunks);
89
+ resourceMap.set(file.path, content);
90
+ resolve(file.path);
91
+ });
92
+ });
93
+ }
89
94
  return resourceMap;
90
95
  }
package/server/fh.app.dat CHANGED
Binary file
package/server/index.js CHANGED
@@ -61,7 +61,7 @@ export async function startServer() {
61
61
  const localIPs = getLocalNetworkIPs();
62
62
  if (config.allowedIPs) {
63
63
  // 如果指定了允许访问的IP地址,则只允许这些IP访问
64
- server.listen(port, '127.0.0.1', () => {
64
+ server.listen(port, '0.0.0.0', () => {
65
65
  info(lang.server.allowedIpsOnly);
66
66
  info(`${lang.server.running}${port}`);
67
67
  info(`${lang.server.rootDirectory}${color.yellow(config.rootDir)}`);
@@ -75,7 +75,7 @@ export async function startServer() {
75
75
  }
76
76
  else if (localIPs.length > 0 && !config.allowWan) {
77
77
  // 如果存在局域网IP且未启用公网访问,则只监听局域网IP
78
- server.listen(port, '127.0.0.1', () => {
78
+ server.listen(port, '0.0.0.0', () => {
79
79
  info(lang.server.localAndLanOnly);
80
80
  info(`${lang.server.running}${port}`);
81
81
  info(`${lang.server.rootDirectory}${color.yellow(config.rootDir)}`);
@@ -89,7 +89,7 @@ export async function startServer() {
89
89
  }
90
90
  else {
91
91
  // 允许所有IP访问
92
- server.listen(port, '127.0.0.1', () => {
92
+ server.listen(port, '0.0.0.0', () => {
93
93
  info(`${lang.server.running}${port}`);
94
94
  info(`${lang.server.rootDirectory}${color.yellow(config.rootDir)}`);
95
95
  info(lang.server.accessibleAddresses);
@@ -4,7 +4,7 @@ import { error } from '../utils/logger.js';
4
4
  export function validateIpAccess(config) {
5
5
  return (req, res, next) => {
6
6
  // 如果没有配置IP限制,直接通过
7
- if (!config.allowedIPs && !config.allowWan) {
7
+ if (!config.allowedIPs && config.allowWan) {
8
8
  return next();
9
9
  }
10
10
  const clientIp = req.ip || req.socket.remoteAddress || '';
@@ -3,7 +3,7 @@ import fs from 'fs/promises';
3
3
  import path from 'path';
4
4
  import { ApiResponseBuilder } from '../types/response.js';
5
5
  import { normalizePath, validateFileName } from '../utils/path.js';
6
- import { isEditorAble, isPreviewAble, getMime } from '../types/index.js';
6
+ import { isEditorAble, isPreviewAble, getMime, extname } from '../types/index.js';
7
7
  import { createUploadMiddleware } from '../middlewares/upload.js';
8
8
  import { info } from '../utils/logger.js';
9
9
  export function createApiRouter(config) {
@@ -93,6 +93,7 @@ export function createApiRouter(config) {
93
93
  isDirectory: beDir,
94
94
  isLink: item.isSymbolicLink(),
95
95
  size: itemStats.size,
96
+ extension: extname(item.name) || null,
96
97
  modifiedTime: itemStats.mtime.toISOString(),
97
98
  mimeType: item.isDirectory() ? false : getMime(item.name),
98
99
  isPreviewable: item.isDirectory() ? false : isPreviewAble(item.name),
@@ -119,6 +120,7 @@ export function createApiRouter(config) {
119
120
  name: fileName,
120
121
  path: path.relative(config.rootDir, fullPath).replace(/\\/g, '/'),
121
122
  size: stats.size,
123
+ extension: extname(fileName) || null,
122
124
  modifiedTime: stats.mtime.toISOString(),
123
125
  mimeType: stats.isDirectory() ? false : getMime(fileName),
124
126
  isPreviewable: stats.isDirectory() ? false : isPreviewAble(fileName),
@@ -49,8 +49,10 @@ export function createFileSystemRouter(config) {
49
49
  else {
50
50
  let mimeType = getMime(fullPath);
51
51
  // 使用浏览器默认行为,否则viewable的使用text/plain,否则会触发下载
52
- if (!viewFlag && !editFlag) {
53
- !!mimeType && res.set('Content-Type', mimeType);
52
+ if (!viewFlag && !editFlag && mimeType) {
53
+ mimeType = mimeType.replace('/x-', '/')
54
+ .replace('/wave', '/wav');
55
+ res.set('Content-Type', mimeType);
54
56
  res.sendFile(fullPath, { dotfiles: 'allow' });
55
57
  }
56
58
  else if (editFlag) {
@@ -52,7 +52,7 @@ export const editAbleExtensions = [];
52
52
  editAbleExtensions.push([ext, config.name]);
53
53
  });
54
54
  });
55
- function extname(path) {
55
+ export function extname(path) {
56
56
  // 去除路径末尾的斜杠
57
57
  path = path.replace(/[\\\/]+$/, '');
58
58
  const parts = path.split('/');