1 | var zxy = require('zxy-comm');
|
2 | var path = require('path');
|
3 | var _ = require('underscore');
|
4 | var Promise = require('bluebird');
|
5 | var multiparty = require('multiparty');
|
6 | var fs = require('fs');
|
7 |
|
8 |
|
9 | var upload = function (req, res) {
|
10 | res.setHeader("Access-Control-Allow-Origin", "*");
|
11 |
|
12 | var uploadfilepath = path.join(__dirname, '../../../uploads/');
|
13 | if (!fs.existsSync(uploadfilepath)) {
|
14 | fs.mkdirSync(uploadfilepath);
|
15 | }
|
16 |
|
17 | var form = new multiparty.Form();
|
18 |
|
19 | form.encoding = 'utf-8';
|
20 |
|
21 | form.uploadDir = "uploads/";
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | form.parse(req, function (err, fields, files) {
|
27 | var destination = path.join(__dirname, '../../../uploads/');
|
28 | var encoding = form.encoding;
|
29 | var batch = zxy.app.m_cryptogram.md5(new Date().valueOf().toString() + zxy.app.m_obtain.getRandomStr() + zxy.app.m_obtain.guid());
|
30 | var ids = new Array();
|
31 | var promise_arr = new Array();
|
32 | _.each(files, function (obj) {
|
33 | obj = obj[0];
|
34 | var id = zxy.app.m_obtain.guid();
|
35 | var originalname = obj.originalFilename;
|
36 | var fieldname = obj.fieldName;
|
37 | var filename = obj.path.replace('uploads\\', '').replace('uploads/', '');
|
38 | var mimetype = obj.headers['content-type'];
|
39 | var path = require('path').join(__dirname, '../../../' + obj.path);
|
40 | var size = obj.size;
|
41 | ids.push({ uuid: id, originalname: originalname, mimetype: mimetype, size: size });
|
42 | var sql = "INSERT INTO zxy_upload_log("
|
43 | + "id,destination,encoding,fieldname,filename,mimetype,originalname,path,size,upload_time,batch)VALUES("
|
44 | + "@id,@destination,@encoding,@fieldname,@filename,@mimetype,@originalname,@path,@size,NOW(),@batch);";
|
45 | promise_arr.push(
|
46 | zxy.app.m_mysql.run(sql, {
|
47 | id: id,
|
48 | destination: destination,
|
49 | encoding: encoding,
|
50 | fieldname: fieldname,
|
51 | filename: filename,
|
52 | mimetype: mimetype,
|
53 | originalname: originalname,
|
54 | path: path,
|
55 | size: size,
|
56 | batch: batch
|
57 | })
|
58 | );
|
59 | });
|
60 | Promise.all(promise_arr).then(function () {
|
61 | res.jsonp(ids);
|
62 | });
|
63 | });
|
64 | }
|
65 | exports.upload = upload;
|
66 |
|
67 |
|
68 | var download = function (req, res) {
|
69 | res.setHeader("Access-Control-Allow-Origin", "*");
|
70 | var id = req.params.id;
|
71 | try {
|
72 | var sql = "select * from zxy_upload_log where id = @id";
|
73 | zxy.app.m_mysql.run(sql, {
|
74 | id: id
|
75 | }).then(function (r) {
|
76 | if (r && r.length && r[0]) {
|
77 | if (fs.exists(r[0].path, function (exists) {
|
78 | if (exists) {
|
79 | var attachmentName = r[0].originalname;
|
80 | attachmentName = encodeURIComponent(attachmentName);
|
81 | res.setHeader('Content-disposition', 'attachment; filename=' + attachmentName);
|
82 | res.setHeader('Content-type', r[0].mimetype + ";charset=utf8");
|
83 | var filestream = fs.createReadStream(r[0].path);
|
84 | filestream.on('data', function (chunk) {
|
85 | res.write(chunk);
|
86 | });
|
87 | filestream.on('end', function () {
|
88 | res.end();
|
89 | });
|
90 | }
|
91 | else {
|
92 | res.send({ code: -1, msg: '文件不存在' });
|
93 | }
|
94 | }));
|
95 | }
|
96 | else {
|
97 | res.send({ code: -2, msg: '文件信息不存在' });
|
98 | }
|
99 | }).catch(function () {
|
100 | res.send({ code: -3, msg: '查找文件信息异常' });
|
101 | });
|
102 | }
|
103 | catch (ex) {
|
104 | res.send({ code: -4, msg: '系统错误' });
|
105 | }
|
106 | }
|
107 | exports.download = download;
|
108 |
|
109 |
|
110 | var preview = function (req, res) {
|
111 | res.setHeader("Access-Control-Allow-Origin", "*");
|
112 | var id = req.params.id;
|
113 | try {
|
114 | var sql = "select * from zxy_upload_log where id = @id";
|
115 | zxy.app.m_mysql.run(sql, {
|
116 | id: id
|
117 | }).then(function (r) {
|
118 | if (r && r.length && r[0]) {
|
119 | if (fs.exists(r[0].path, function (exists) {
|
120 | if (exists) {
|
121 | if (r[0].mimetype.StartWith('video/') ||
|
122 | r[0].mimetype.StartWith('image/') ||
|
123 | r[0].mimetype.StartWith('text/')) {
|
124 | var rs = fs.createReadStream(r[0].path);
|
125 | rs.on('data', function (data) {
|
126 | res.write(data);
|
127 | });
|
128 | rs.on('end', function () {
|
129 | res.end();
|
130 | });
|
131 | }
|
132 | else {
|
133 | res.send({ code: -5, msg: '暂不支持该类型文件预览' });
|
134 | }
|
135 | }
|
136 | else {
|
137 | res.send({ code: -1, msg: '文件不存在' });
|
138 | }
|
139 | }));
|
140 | }
|
141 | else {
|
142 | res.send({ code: -2, msg: '文件信息不存在' });
|
143 | }
|
144 | }).catch(function () {
|
145 | res.send({ code: -3, msg: '查找文件信息异常' });
|
146 | });
|
147 | }
|
148 | catch (ex) {
|
149 | res.send({ code: -4, msg: '系统错误' });
|
150 | }
|
151 | }
|
152 | exports.preview = preview;
|
153 |
|
154 |
|
155 | var attribute = function (req, res) {
|
156 | res.setHeader("Access-Control-Allow-Origin", "*");
|
157 | var id = req.params.id;
|
158 | try {
|
159 | var sql = "select * from zxy_upload_log where id = @id";
|
160 | zxy.app.m_mysql.run(sql, {
|
161 | id: id
|
162 | }).then(function (r) {
|
163 | if (r && r.length && r[0]) {
|
164 | var attr = {
|
165 | uuid: r[0].id,
|
166 | mimetype: r[0].mimetype,
|
167 | originalname: r[0].originalname,
|
168 | size: r[0].size,
|
169 | upload_time: r[0].upload_time,
|
170 | batch: r[0].batch
|
171 | };
|
172 | res.jsonp(attr);
|
173 | }
|
174 | else {
|
175 | res.send({ code: -2, msg: '文件信息不存在' });
|
176 | }
|
177 | }).catch(function () {
|
178 | res.send({ code: -3, msg: '查找文件信息异常' });
|
179 | });
|
180 | }
|
181 | catch (ex) {
|
182 | res.send({ code: -4, msg: '系统错误' });
|
183 | }
|
184 | }
|
185 | exports.attribute = attribute; |
\ | No newline at end of file |