123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- var fs = require('fs');
- var formidable = require('formidable');
- var linkMysql = require('./linkMysql');
- const uuidv1 = require('uuid/v1');
- module.exports = function (req, res) {
- var form = new formidable.IncomingForm(); //创建上传表单
- // 临时路径
- form.uploadDir = __dirname + '/../temp';
- // 读取formdata
- form.parse(req, function(err, fields, files) {
- // console.log(err);
- // console.log(fields);
- // console.log(files);
- // return
- if(err){
- console.log('fields',fields)
- uploadError(res, err, 501);
- return;
- }
- // theFile
- // fileName
- // totalSize
- // isFirstChunk 是否第一次上传
- // isLastChunk 是否最后一次上传
- var file = files.theFile;
- var fileName = fields.fileName;
- console.log('fileName',fileName)
- var fileHash = fields.fileHash;
- var fileChunk = fields.fileChunk;
- var fileSize = fields.totalSize;
- var isFirstChunk = isTrue(fields.isFirstChunk);
- var isLastChunk = isTrue(fields.isLastChunk);
- // 文件类型
- var fileType = fileName.toLowerCase().split('.').splice(-1);
- // 服务器名
- var fileServerName = uuidv1()+'.'+fileType;
- // if(isFirstChunk){
- // fileName
- // }
- // console.log(file.path)
- // 查询文件是否存在
- linkMysql.call('selectFileMsgWhereFileHash', [fileHash],function(qerr,vals,fields){
- if(qerr){
- uploadError(res, err, 401);
- }else{
- vals = vals[0][0];
- if(vals){//如果存在 获取文件名
- fileServerName = vals.FileServerName
- }
- var fileDir = __dirname + '/../upload/' + fileServerName;
- // 开启读取模式
- fs.readFile(file.path, {flag: 'r+'}, function (err, data) {
- if(err) {
- uploadError(res, err, 502);
- }else{
- // 是否第一次读取
- if(isFirstChunk){
- // 写入文件
- fs.writeFile(fileDir, data, {}, function (err) {
- if(err) {
- uploadError(res, err, 503);
- } else {
- // 上传成功创建文件数据
- linkMysql.call('addFileMsg', [fileServerName, fileName, fileType, fileHash, fileSize, (isLastChunk?'1':'0')],function(qerr,vals,fields){
- if(qerr){
- uploadError(res, err, 5031);
- }else{
- // console.log(qerr,vals,fields)
- uploadSuccess(res, isLastChunk, fileServerName);
- }
- })
- }
- });
- }else{
- // 追加写入文件
- fs.appendFile(fileDir, data, function (err) {
- if(err) {
- uploadError(res, err, 505);
- } else {
- // 上传成功修改文件数据
- linkMysql.call('updateFileChunk', [fileHash, fileChunk, (isLastChunk?'1':'0')], function(qerr,vals,fields){
- if(qerr){
- console.log(qerr)
- uploadError(res, err, 5051);
- }else{
- // console.log(qerr,vals,fields)
- uploadSuccess(res, isLastChunk, fileServerName);
- }
- })
- }
- });
- }
- fs.unlinkSync(file.path) // 删除临时文件
- }
- })
- }
- })
- })
- }
- // 判断字符串true
- function isTrue(t){
- return t === 'true'|| t === true;
- }
- /**
- * 打印上传成功的log并且返回状态信息
- * @param {[type]} res [response]
- * @param {Boolean} isLastChunk 是否最后一片 [string | boolean]
- * @param {Boolean} fileServerName 文件服务器名 [string]
- */
- function uploadSuccess(res, isLastChunk, fileServerName) {
- console.log(isTrue(isLastChunk)? '写入成功': '写入完毕');
- res.send({
- status:200,
- isLastChunk: isLastChunk,
- link: isLastChunk?'/uploadFile/' + fileServerName:''
- })
- }
- /**
- * 打印错误并返回
- * @param {[type]} res [response]
- * @param {[type]} err [错误信息]
- * @param {[type]} status [错误状态]
- */
- function uploadError(res, err, status) {
- console.error(err);
- console.log(status);
- res.send({status:status})
- }
|