Commit 1e73801d authored by 刘文胜's avatar 刘文胜

文件上传中间件改成调用java接口

parent ce847c38
...@@ -7,6 +7,8 @@ var fs = require('fs'); ...@@ -7,6 +7,8 @@ var fs = require('fs');
var crypto = require('crypto'); var crypto = require('crypto');
var ossClient = require('oss-easy'); var ossClient = require('oss-easy');
var env = require('../../config/config'); var env = require('../../config/config');
var JAVA_UPLOADFILE_API=env.rest_api + '/v1.0/internal/common/imageUpload';
var request = require('request');
var ossOption = { var ossOption = {
accessKeyId: env.ossConfig.accessKeyId, accessKeyId: env.ossConfig.accessKeyId,
accessKeySecret: env.ossConfig.accessKeySecret accessKeySecret: env.ossConfig.accessKeySecret
...@@ -93,38 +95,49 @@ function handleUpload(req, uploadfile, storageType, targetPath, callback) { ...@@ -93,38 +95,49 @@ function handleUpload(req, uploadfile, storageType, targetPath, callback) {
'Cache-Control': 'max-age=86400' 'Cache-Control': 'max-age=86400'
}; };
filePath = 'upload/' + filePath; filePath = 'upload/' + filePath;
var fileSavePath = '/oss/' + filePath;
var url = isDebug === true ? env.ossConfig.devCDNURL : env.ossConfig.prodCDNURL; var url = isDebug === true ? env.ossConfig.devCDNURL : env.ossConfig.prodCDNURL;
// ofs.uploadFile(uploadfile.path, filePath, metadata, function(err) { ofs.uploadFile(uploadfile.path, filePath, metadata, function(err) {
// if (err) { if (err) {
// console.log(err); console.log(err);
// callback(err, null); callback(err, null);
// } else { } else {
// callback(null, { callback(null, {
// originalName: uploadfile.name, originalName: uploadfile.name,
// fileName: filePath, fileName: filePath,
// fileType: uploadfile.type, fileType: uploadfile.type,
// fileSize: uploadfile.size, fileSize: uploadfile.size,
// urlFileName: url + filePath, urlFileName: url + filePath,
// fieldName: uploadfile.fieldName fieldName: uploadfile.fieldName
// }); });
// } }
// }); });
console.log('targetPath:' + fileSavePath); }
fse.move(uploadfile.path, fileSavePath, function(err) { }
console.error('error:' + err);
if (err) throw callback(err, null); function handleUploadWithJavaAPI(uploadfile, callback) {
if (!uploadfile) return callback && callback(null,null);
var req = request.post({
url: JAVA_UPLOADFILE_API,
json: true
}, function (err, resp, body) {
if (err) {
callback(err, null);
}else if(!body || !body.data || !body.data.url){
console.log(body);
callback('返回数据错误', null);
} else {
callback(null, { callback(null, {
originalName: uploadfile.name, originalName: body.data.fileName,
fileName: filePath, fileName: body.data.url,
fileType: uploadfile.type, fileType: uploadfile.type,
fileSize: uploadfile.size, fileSize: uploadfile.size,
urlFileName: url + filePath, urlFileName: body.data.url,
fieldName: uploadfile.fieldName fieldName: uploadfile.fieldName
}); });
}
}); });
} var form = req.form();
form.append('file', fs.createReadStream(uploadfile.path));
} }
module.exports = function(storageType, targetPath) { module.exports = function(storageType, targetPath) {
...@@ -143,7 +156,8 @@ module.exports = function(storageType, targetPath) { ...@@ -143,7 +156,8 @@ module.exports = function(storageType, targetPath) {
var asyncTasks = []; var asyncTasks = [];
items.forEach(function(file) { items.forEach(function(file) {
asyncTasks.push(function(callback) { asyncTasks.push(function(callback) {
handleUpload(req, file, storageType, storePath, callback); //handleUpload(req, file, storageType, storePath, callback);
handleUploadWithJavaAPI(file, callback);
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment