@zmx0142857/file-share 1.0.1 → 1.0.3

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.
Files changed (3) hide show
  1. package/README.md +4 -1
  2. package/package.json +1 -1
  3. package/serve.js +58 -55
package/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # file-share
2
2
 
3
- ## Quick start
3
+ Start a simple file share server with one command:
4
4
 
5
5
  $ npx @zmx0142857/file-share
6
+ 🚀 server running at http://192.168.?.?:3001
7
+
8
+ Open browser and visit!
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zmx0142857/file-share",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=16.0.0"
package/serve.js CHANGED
@@ -1,55 +1,58 @@
1
- import express from 'express'
2
- import cors from 'cors'
3
- import fileUpload from 'express-fileupload'
4
- import fs from 'fs'
5
- import path from 'path'
6
- import indexHtml from './index.js'
7
-
8
- const app = express()
9
- app.use(cors())
10
- app.use(fileUpload({ createParentPath: true, useTempFiles: true }))
11
- app.use(express.json())
12
- app.use('/files', express.static('files'))
13
-
14
- // 文件列表接口
15
- app.get('/list', (req, res) => {
16
- const dir = path.resolve('files')
17
- if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true })
18
- fs.readdir(dir, (err, entries) => {
19
- if (err) return res.status(500).json({ code: 500, msg: '读取目录失败' })
20
- const data = entries
21
- .filter(name => fs.statSync(path.join(dir, name)).isFile())
22
- .map(name => ({
23
- name,
24
- size: fs.statSync(path.join(dir, name)).size,
25
- }))
26
- res.json({ code: 200, msg: 'ok', data })
27
- })
28
- })
29
-
30
- // 上传接口(返回 JSON)
31
- app.post('/upload', (req, res) => {
32
- if (!req.files || !req.files.files) {
33
- return res.status(400).json({ code: 400, msg: '请选择一个或多个文件' })
34
- }
35
- let { files } = req.files
36
- if (!Array.isArray(files)) files = [files]
37
- const data = []
38
- files.forEach(file => {
39
- file.name = new TextDecoder().decode(Uint8Array.from(file.name, c => c.charCodeAt(0)))
40
- console.log(file)
41
- file.mv('./files/' + file.name)
42
- data.push({
43
- name: file.name,
44
- mimetype: file.mimetype,
45
- size: file.size,
46
- })
47
- })
48
- res.json({ code: 200, msg: 'ok', data })
49
- })
50
-
51
- app.get('/', (req, res) => res.send(indexHtml))
52
-
53
- const argv = process.argv.slice(2)
54
- const port = argv[0] || 3001
55
- app.listen(port, () => console.log(`server is running at port http://localhost:${port}`))
1
+ #!/usr/bin/env node
2
+ import express from 'express'
3
+ import cors from 'cors'
4
+ import fileUpload from 'express-fileupload'
5
+ import fs from 'fs'
6
+ import path from 'path'
7
+ import indexHtml from './index.js'
8
+ import os from 'os'
9
+
10
+ const app = express()
11
+ app.use(cors())
12
+ app.use(fileUpload({ createParentPath: true, useTempFiles: true }))
13
+ app.use(express.json())
14
+ app.use('/files', express.static('files'))
15
+
16
+ // 文件列表接口
17
+ app.get('/list', (req, res) => {
18
+ const dir = path.resolve('files')
19
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true })
20
+ fs.readdir(dir, (err, entries) => {
21
+ if (err) return res.status(500).json({ code: 500, msg: '读取目录失败' })
22
+ const data = entries
23
+ .filter(name => fs.statSync(path.join(dir, name)).isFile())
24
+ .map(name => ({
25
+ name,
26
+ size: fs.statSync(path.join(dir, name)).size,
27
+ }))
28
+ res.json({ code: 200, msg: 'ok', data })
29
+ })
30
+ })
31
+
32
+ // 上传接口(返回 JSON)
33
+ app.post('/upload', (req, res) => {
34
+ if (!req.files || !req.files.files) {
35
+ return res.status(400).json({ code: 400, msg: '请选择一个或多个文件' })
36
+ }
37
+ let { files } = req.files
38
+ if (!Array.isArray(files)) files = [files]
39
+ const data = []
40
+ files.forEach(file => {
41
+ file.name = new TextDecoder().decode(Uint8Array.from(file.name, c => c.charCodeAt(0)))
42
+ console.log(file)
43
+ file.mv('./files/' + file.name)
44
+ data.push({
45
+ name: file.name,
46
+ mimetype: file.mimetype,
47
+ size: file.size,
48
+ })
49
+ })
50
+ res.json({ code: 200, msg: 'ok', data })
51
+ })
52
+
53
+ app.get('/', (req, res) => res.send(indexHtml))
54
+
55
+ const argv = process.argv.slice(2)
56
+ const port = argv[0] || 3001
57
+ const ip = Object.values(os.networkInterfaces()).flat().find(v => v.family === 'IPv4').address
58
+ app.listen(port, () => console.log(`🚀 server running at http://${ip}:${port}`))