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

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

parent ce847c38
......@@ -7,6 +7,8 @@ var fs = require('fs');
var crypto = require('crypto');
var ossClient = require('oss-easy');
var env = require('../../config/config');
var JAVA_UPLOADFILE_API=env.rest_api + '/v1.0/internal/common/imageUpload';
var request = require('request');
var ossOption = {
accessKeyId: env.ossConfig.accessKeyId,
accessKeySecret: env.ossConfig.accessKeySecret
......@@ -93,27 +95,12 @@ function handleUpload(req, uploadfile, storageType, targetPath, callback) {
'Cache-Control': 'max-age=86400'
};
filePath = 'upload/' + filePath;
var fileSavePath = '/oss/' + filePath;
var url = isDebug === true ? env.ossConfig.devCDNURL : env.ossConfig.prodCDNURL;
// ofs.uploadFile(uploadfile.path, filePath, metadata, function(err) {
// if (err) {
// console.log(err);
// callback(err, null);
// } else {
// callback(null, {
// originalName: uploadfile.name,
// fileName: filePath,
// fileType: uploadfile.type,
// fileSize: uploadfile.size,
// urlFileName: url + filePath,
// fieldName: uploadfile.fieldName
// });
// }
// });
console.log('targetPath:' + fileSavePath);
fse.move(uploadfile.path, fileSavePath, function(err) {
console.error('error:' + err);
if (err) throw callback(err, null);
ofs.uploadFile(uploadfile.path, filePath, metadata, function(err) {
if (err) {
console.log(err);
callback(err, null);
} else {
callback(null, {
originalName: uploadfile.name,
fileName: filePath,
......@@ -122,9 +109,35 @@ function handleUpload(req, uploadfile, storageType, targetPath, callback) {
urlFileName: url + filePath,
fieldName: uploadfile.fieldName
});
}
});
}
}
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, {
originalName: body.data.fileName,
fileName: body.data.url,
fileType: uploadfile.type,
fileSize: uploadfile.size,
urlFileName: body.data.url,
fieldName: uploadfile.fieldName
});
}
});
var form = req.form();
form.append('file', fs.createReadStream(uploadfile.path));
}
module.exports = function(storageType, targetPath) {
......@@ -143,7 +156,8 @@ module.exports = function(storageType, targetPath) {
var asyncTasks = [];
items.forEach(function(file) {
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