-
Notifications
You must be signed in to change notification settings - Fork 7
/
uploader.js
151 lines (134 loc) · 5.17 KB
/
uploader.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const path = require('path');
const fs = require('fs');
const express = require('express');
const multer = require('multer');
const LogUtil = require('./log');
const config = require('./config');
const TARGET_DIR = config.ConfigManager.getInstance().getValue(config.keys.KEY_IMAGE_DIR);
const ADD_WATERMARK = config.ConfigManager.getInstance().getValue(config.keys.KEY_ADD_WATERMARK);
const URL_RREFIX = config.ConfigManager.getInstance().getValue(config.keys.KEY_URL_PREFIX);
const MAX_IMAGE_SIZE = config.ConfigManager.getInstance().getValue(config.keys.KEY_MAX_IMAGE_SIZE);
const COMPRESS_FORMATS = config.ConfigManager.getInstance().getValue(config.keys.KEY_FORMATS);
const GEN_COMPRESS = config.ConfigManager.getInstance().getValue(config.keys.KEY_GEN_COMPRESS);
const AVAILABLE_EXTENSIONS = config.ConfigManager.getInstance().getValue(config.keys.KEY_AVAILABLE_EXT);
const WaterMarker = require('./watermarker');
const UniResult = require('./universal-result');
const converter = require('./converter');
const BaseService = require('./base_service');
class ImageUploader extends BaseService {
constructor() {
super();
/**
* Init multer.
*/
this.upload = multer({
dest: config.ConfigManager.getInstance().getImageTempPath(),
fileFilter: (req, file, callback) => {
const pToken = req.query.accessToken;
const configToken = config.ConfigManager.getInstance().getValue(config.keys.KEY_ACCESS_TOKEN);
// Check token
if (pToken !== configToken) {
callback(new Error('token is invalid'), false);
return;
}
callback(null, true);
},
limits: {
fileSize: Math.ceil(MAX_IMAGE_SIZE * 1024 * 1024)
}
}).single('image');
}
handleRequest(req, res) {
const noWaterMark = (req.query.nomark === '1');
this.upload(req, res, (err) => {
if (err) {
LogUtil.error(err);
ImageUploader._doResponse(null, err, res);
return;
}
let file = req.file;
if (!file) {
ImageUploader._doErrResponse(UniResult.Errors.PARAM_ERROR, res);
return;
}
let ext = path.parse(file.originalname).ext;
if (!ext || ext.length === 0) {
ImageUploader._doErrResponse(UniResult.Errors.PARAM_ERROR, res);
return;
}
ext = ext.toLowerCase();
if (!AVAILABLE_EXTENSIONS.includes(ext)) {
LogUtil.error("Upload unsupported file type: " + ext);
ImageUploader._doErrResponse(UniResult.Errors.PARAM_ERROR, res);
return;
}
let ts = (new Date() * 1);
let fileName = `${ts}${ext}`;
let imageFilePath = path.join(TARGET_DIR, fileName);
// If enable watermark, add watermark and save to target path.
if (ADD_WATERMARK && !noWaterMark) {
WaterMarker.markAndSave(file.path, imageFilePath, (err) => {
if (!err) {
ImageUploader._processFormats(imageFilePath, fileName, res);
} else {
LogUtil.error('Add watermark error: ' + err);
ImageUploader._doResponse(null, err, res);
}
});
} else {
// If not enable watermark or get an error when adding watermark, rename directly.
ImageUploader._moveFile(file.path, imageFilePath, fileName, res);
}
});
}
getServiceName() {
return 'Image Uploader';
}
getServerPort() {
return config.ConfigManager.getInstance().getValue(config.keys.KEY_UPLOADER_SERVER_PORT);
}
static _moveFile(currentPath, destPath, fileName, res) {
fs.rename(currentPath, destPath, (err) => {
if (err) {
LogUtil.error('Move file error: ' + err);
ImageUploader._doResponse(null, err, res);
return;
} else {
ImageUploader._doNoErrResponse({
url: `${URL_RREFIX}${fileName}`
}, res);
}
});
};
static _processFormats(imageFilePath, fileName, res) {
if (GEN_COMPRESS) {
converter(COMPRESS_FORMATS, imageFilePath)
}
ImageUploader._doNoErrResponse({
url: `${URL_RREFIX}${fileName}`
}, res);
};
/**
* Send JSON response.
*
* @param {UniResult} data
* @param {Error|null} err
* @param {express.Response} res
*/
static _doResponse(data, err = null, res) {
if (data) {
res.json(UniResult.UniResult.getSuccess(data));
} else {
res.json(UniResult.UniResult.getError(-1, err.message))
}
res.end();
}
static _doNoErrResponse(data, res) {
ImageUploader._doResponse(data, null, res);
}
static _doErrResponse(err, res) {
res.json(err)
res.end();
}
}
module.exports = ImageUploader;