@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.
- package/README.md +4 -1
- package/package.json +1 -1
- package/serve.js +58 -55
package/README.md
CHANGED
package/package.json
CHANGED
package/serve.js
CHANGED
|
@@ -1,55 +1,58 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
app
|
|
11
|
-
app.use(
|
|
12
|
-
app.use(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
file.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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}`))
|