file-handle 0.0.2 → 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
@@ -1,7 +1,7 @@
1
1
 
2
2
  # File-Handle
3
3
 
4
- ![image](docs/logo-s.png)
4
+ ![image](server/client/res/logo-s.png)
5
5
 
6
6
  A simple local web file server with a user-friendly interface, allowing easy file upload, download, preview, and management through a browser.
7
7
 
@@ -32,7 +32,11 @@ npm install -g file-handle
32
32
  Run the following command in the directory you wish to share to start the file server:
33
33
 
34
34
  ```bash
35
- file-handle
35
+ > file-handle
36
+ ```
37
+
38
+ ```bash
39
+ > fh
36
40
  ```
37
41
 
38
42
  This will start the server on the default port (6688) and use the current working directory as the root directory.
@@ -75,6 +79,7 @@ file-handle
75
79
  ## License
76
80
 
77
81
  Copyright [gaoyuan]
82
+
78
83
  Link: www.npmjs.com/package/file-handle
79
84
 
80
85
  Licensed under the Apache License, Version 2.0 (the "License");
package/package.json CHANGED
@@ -1,10 +1,22 @@
1
1
  {
2
2
  "name": "file-handle",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "author": "jsonhandle@gmail.com",
5
- "description": "一个简易的文件服务器,方便通过浏览器来浏览、上传或管理目录和文件. \nA simple local web file server with a user-friendly interface, allowing easy file upload, download, preview, and management through a browser.",
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",
7
7
  "type": "module",
8
+ "keywords": [
9
+ "file-handle",
10
+ "file",
11
+ "server",
12
+ "upload",
13
+ "download",
14
+ "preview",
15
+ "file",
16
+ "serve",
17
+ "web-server",
18
+ "http-server"
19
+ ],
8
20
  "bin": {
9
21
  "file-handle": "server/bin/cli.js",
10
22
  "fh": "server/bin/cli.js"
@@ -21,7 +33,7 @@
21
33
  "test": "echo \"Error: no test specified\" && exit 1"
22
34
  },
23
35
  "dependencies": {
24
- "@electron/asar": "^3.3.1",
36
+ "unzipper": "^0.12.3",
25
37
  "archiver": "^7.0.1",
26
38
  "cli-color": "^2.0.4",
27
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) {
@@ -69,7 +69,7 @@ export function createApiRouter(config) {
69
69
  router.get('/get', async (req, res, next) => {
70
70
  try {
71
71
  const { path: targetPath = '/' } = req.query;
72
- const fullPath = normalizePath(targetPath, config.rootDir);
72
+ const fullPath = normalizePath(decodeURIComponent(targetPath), config.rootDir);
73
73
  const stats = await fs.stat(fullPath);
74
74
  const self = await fs.lstat(fullPath);
75
75
  const isDirectory = stats.isDirectory();
@@ -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),
@@ -151,7 +153,7 @@ export function createApiRouter(config) {
151
153
  throw new FileOperationError('Cannot download a directory', 400);
152
154
  }
153
155
  const filename = path.basename(fullPath);
154
- res.setHeader('Content-Disposition', `attachment; filename="${filename}"`);
156
+ res.setHeader('Content-Disposition', `attachment; filename="${encodeURIComponent(filename)}"`);
155
157
  res.setHeader('Content-Type', 'application/octet-stream');
156
158
  res.setHeader('Content-Length', stats.size);
157
159
  const fileStream = await fs.readFile(fullPath);
@@ -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('/');
package/docs/logo-s.png DELETED
Binary file