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