Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
123 lines (108 loc) · 3.38 KB
/
Copy pathindex.js
File metadata and controls
123 lines (108 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const http = require('http')
const formidable = require('formidable')
const fs = require('fs')
const log = require('./log.js')
const config = require('./config.json')
const uploadImage = require('./picgo.js')
// 默认8080端口
var port = config.port || 8080
// 默认路径 /upload
const url = config.url || '/upload'
// POST File Name
const fileName = config.fileName || 'file'
// Response URL Path
const urlPath = config.urlPath || 'url'
// 两张图片上传的等待间隔
const interval = config.interval || 200
let timeoutId = null
/**
* 为文件加上扩展名
*
* @param {String} filePath 文件路径
* @param {String} ext 文件扩展名
*/
function addExtForFile(filePath, ext) {
let newFilePath = filePath + '.' + ext
// 同步修改文件名字
fs.renameSync(filePath, newFilePath)
return newFilePath
}
var filePathArray = []
let responseArray = []
function addTask(file, response) {
let filePath = file.path
let fileName = file.name
let extension = fileName.substring(fileName.lastIndexOf('.') + 1)
let filePathHasExt = filePath.lastIndexOf('.' + extension) != -1
if (!filePathHasExt) {
// 如果文件路径没有扩展名,上传到图床会乱码,这里改名,加上扩展名
filePath = addExtForFile(filePath, extension)
}
filePathArray.push(filePath)
responseArray.push(response)
if (filePathArray.length == 1) {
// 第一次手动触发
handleTask()
}
}
function handleTask() {
if (filePathArray.length == 0) return
let filePath = filePathArray[0]
uploadImage([filePath]).then((result) => {
let response = responseArray.shift()
filePathArray.shift()
// 处理结果
if (result) {
response.writeHead(200, { 'Content-Type': 'text/json' })
let res = { status: 'success' }
res[urlPath] = result
response.write(JSON.stringify(res))
} else {
response.writeHead(500, { 'Content-Type': 'text/json' })
response.write(
JSON.stringify({
status: 'false',
})
)
}
response.end()
log.info("已处理文件:" + filePath)
log.info("还剩" + filePathArray.length + "个")
// 延时触发下一张图片上传,防止文件重名
setTimeout(() => {
handleTask()
}, interval)
})
}
// create server, listen to port
http.createServer(function (req, res) {
if (req.url == url && req.method == 'POST') {
var form = new formidable.IncomingForm()
form.parse(req, function (err, fields, files) {
if (err) {
res.send(err)
return
}
let file = files[fileName]
if (!file) {
res.writeHead(500, { 'Content-Type': 'text/json' })
log.error('no file found')
res.write(
JSON.stringify({
status: 'false',
info: 'plz upload a pic',
})
)
res.end()
return
}
addTask(file, res)
})
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.write('The URL Path is ' + url)
res.end()
}
}).listen(port)
// start up server
log.info(`app run at ${port}`)