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
|
// 图片上传
var path = require('path');
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
function curlPostAssign(res, filepath, filename, filesize) {
request.post(config.url_image + 'dir/assign',{},
function (error, response, body) {
if (!error && response.statusCode == 200) {
assign = eval('(' + body + ')');
var result = {};
result.res = res;
result.json = JSON.stringify({
"originalName": filename,
"name": assign.fid,
"url": '/res?fid=' + assign.fid,
"type": ext,
"size": filesize,
"state": "SUCCESS"
});
curlPostWrite(assign.fid, filepath, assign.publicUrl, result);
var ext = path.extname(filename);
} else {
console.log("Image server assign error...")
}
});
}
function curlPostWrite(fid, tmp_file, publicUrl, result) {
var files = [
{urlKey: "file1", urlValue: tmp_file}
]
var options = {
host: publicUrl.split(":")[0],
port: publicUrl.split(":")[1],
method: "POST",
path: "/" + fid
}
var req = http.request(options, function(res){
res.on("data", function(chunk){
})
})
req.on('error', function(e){
console.log('problem with request:' + e.message);
console.log(e);
})
postfile.postFile(files, req, result);
}
var action = {
// 上传图片
uploadimage: function(req, res) {
curlPostAssign(res, req.files.upfile.path, req.files.upfile.originalFilename, req.files.upfile.size);
},
// 获取配置文件
config: function (req, res) {
return res.redirect('/js/UEditor/config.js');
},
// 在线管理
listimage: function (req, res) {
fs.readdir(uploadsPath, function (err, files) {
var total = 0, list = [];
files.sort().splice(req.query.start, req.query.size).forEach(function (a, b) {
/^.+\..+$/.test(a) &&
list.push({
url: '/uploads/' + a,
mtime: new Date(fs.statSync(uploadsPath + a).mtime).getTime()
});
});
total = list.length;
res.json({state: total === 0 ? 'no match file' : 'SUCCESS', list: list, total: total, start: req.query.start});
});
},
// 抓取图片(粘贴时将图片保存到服务端)
catchimage: function (req, res) {
var list = [];
req.body.source.forEach(function (src, index) {
http.get(src, function (_res) {
var imagedata = '';
_res.setEncoding('binary');
_res.on('data', function (chunk) {
imagedata += chunk
});
_res.on('end', function () {
var pathname = url.parse(src).pathname;
var original = pathname.match(/[^/]+\.\w+$/g)[0];
var suffix = original.match(/[^\.]+$/)[0];
var filename = Date.now() + '.' + suffix;
var filepath = uploadsPath + 'catchimages/' + filename;
fs.writeFile(filepath, imagedata, 'binary', function (err) {
list.push({
original: original,
source: src,
state: err ? "ERROR" : "SUCCESS",
title: filename,
url: '/uploads/catchimages/' + filename
});
})
});
})
});
var f = setInterval(function () {
if (req.body.source.length === list.length) {
clearInterval(f);
res.json({state: "SUCCESS", list: list});
}
}, 50);
}
};
app.get('/ue/uploads', multipartMiddleware, function (req, res) {
action[req.query.action](req, res);
});
app.post('/ue/uploads', multipartMiddleware, function (req, res) {
action[req.query.action](req, res);
});
|