'use strict';

var express = require('express');
var router = express.Router();
var constant = require('../../mixin/constant');
var async = require('async');

//var AccountController = require('../../controllers/account-controller');
//var accountController = new AccountController();

var Vedio = require('../../models/vedio');


router.post('/', (req, res, next)=> {
  var userId = req.session.user.id;
  var file = req.files;
  var fileName = file.fileInfo.originalname;

  async.waterfall([
    (done)=> {
      //查找数据库是否存在某一用户上传过的某一同名视频

      Vedio.findOne({userId: userId, originalname: fileName}, done);
    }, (data, done)=> {
      if (data) {
        done(null, null);
      } else {
        //不存在,需要要记录一条记录
        var creatTime = Date.parse(new Date()) / constant.time.MILLISECOND_PER_SECONDS;
        var vedioInfo = Object.assign({}, file.fileInfo, {createTime: creatTime}, {userId: userId});

        var vedio = new Vedio(vedioInfo);
        vedio.save((err, doc, affectNum)=> {
          done(err, doc);
        })
      }
    }], (err, data)=> {
    if (err) return next(err);

    res.send({status: constant.httpCode.OK});
  });

});

router.post('/form', (req, res, next)=> {
  var userId = req.session.user.id;
  var form = req.body;

  //因为视频已经上传,但是可能详细的信息并没有填写 所以在此处要先查询该用户没填写详细信息的视频
  Vedio.findOne({userId: userId, isDetailsInfoComplete: false}, (err, doc)=> {
    if (err) return next(err);
    if (doc) {
      doc = Object.assign(doc, form, {isDetailsInfoComplete: true});
      doc.save((err, doc)=> {
        if (err) return next(err);
        res.send({status: constant.httpCode.OK});
      })
    }
  });
});

module.exports = router;
@@ -150,16 +150,55 @@
 async');
+%0Avar ffmpeg = require('fluent-ffmpeg');
 %0A%0A//var 
@@ -362,16 +362,691 @@
 dio');%0A%0A
+function takeScreeshots(fileName, fileType, callback) %7B%0A%0A  var originalFileLocation = __dirname + '/../../public/vedio/' + fileName;%0A  var photoName = fileName.replace(fileType, 'png');%0A  var photoLocation = __dirname + '/../../public/screeshots';%0A%0A  ffmpeg(originalFileLocation)%0A      .on('filenames', function (filenames) %7B%0A        console.log('Will generate ' + filenames.join(', '))%0A      %7D)%0A      .on('end', function (err, doc) %7B%0A        callback(err, doc);%0A      %7D)%0A      .screenshots(%7B%0A        // Will take screens at 20%25, 40%25, 60%25 and 80%25 of the video%0A        filename: photoName,%0A        count: 1,%0A        folder: photoLocation,%0A        size: '320x240'%0A      %7D);%0A%0A%7D%0A
 %0Arouter.
@@ -1181,16 +1181,59 @@
 alname;%0A
+  var fileType = file.fileInfo.extension;%0A%0A
 %0A  async
@@ -1292,17 +1292,16 @@
 %E7%9A%84%E6%9F%90%E4%B8%80%E5%90%8C%E5%90%8D%E8%A7%86%E9%A2%91%0A
-%0A
       Ve
@@ -1467,16 +1467,22 @@
 %E8%A6%81%E8%A6%81%E8%AE%B0%E5%BD%95%E4%B8%80%E6%9D%A1%E8%AE%B0%E5%BD%95
+ %E5%B9%B6%E7%94%9F%E6%88%90%E5%9B%BE%E7%89%87
 %0A       
@@ -1486,88 +1486,317 @@
     
-var creatTime = Date.parse(new Date()) / constant.time.MILLISECOND_PER_SECONDS;%0A
+takeScreeshots(fileName, fileType, (err, doc)=%3E %7B%0A          if (err) %7B%0A            done(err, null);%0A          %7D else %7B%0A%0A            var creatTime = Date.parse(new Date()) / constant.time.MILLISECOND_PER_SECONDS;%0A            var screenshotsPath = 'public/screeshots/' + fileName.replace(fileType, 'png');%0A%0A    
     
@@ -1889,16 +1889,52 @@
  userId%7D
+, %7BscreenshotsPath: screenshotsPath%7D
 );%0A%0A    
@@ -1929,32 +1929,36 @@
 ath%7D);%0A%0A        
+  
+  
 var vedio = new 
@@ -1975,16 +1975,20 @@
 oInfo);%0A
+    
         
@@ -2020,24 +2020,28 @@
 ectNum)=%3E %7B%0A
+    
           do
@@ -2058,26 +2058,54 @@
 c);%0A        
-%7D)
+    %7D)%0A          %7D%0A        %7D);
 %0A      %7D%0A   
@@ -2121,24 +2121,25 @@
 , data)=%3E %7B%0A
+%0A
     if (err)

use ffmpeg to take a screenshots