fix mp4 upload and video conversion progress display; fixes #21

This commit is contained in:
Lukas F. Hartmann 2018-05-07 20:19:07 +02:00
parent dc986dcc7e
commit c075c562d6
2 changed files with 26 additions and 29 deletions

View File

@ -92,14 +92,14 @@ function createWaveform(fileName, localFilePath, callback){
}); });
} }
function convertVideo(fileName, filePath, codec, callback, progress_callback) { function convertVideo(fileName, filePath, codec, callback, progressCallback) {
var ext = path.extname(fileName); var ext = path.extname(fileName);
var presetMime = mime.lookup(fileName); var presetMime = mime.lookup(fileName);
var newExt = codec == "mp4" ? "mp4" : "ogv"; var newExt = codec == "mp4" ? "mp4" : "ogv";
var convertedPath = filePath + "." + newExt; var convertedPath = filePath + "." + newExt;
console.log("converting", filePath, "to", convertedPath, "progress_cb:",progress_callback); console.log("converting", filePath, "to", convertedPath);
var convertArgs = (codec == "mp4") ? [ var convertArgs = (codec == "mp4") ? [
"-i", filePath, "-i", filePath,
@ -134,8 +134,8 @@ function convertVideo(fileName, filePath, codec, callback, progress_callback) {
ff.stderr.on('data', function (data) { ff.stderr.on('data', function (data) {
console.log('[ffmpeg-video] stderr: ' + data); console.log('[ffmpeg-video] stderr: ' + data);
if (progress_callback) { if (progressCallback) {
progress_callback(data); progressCallback(data);
} }
}); });
@ -280,7 +280,7 @@ var resizeAndUploadImage = function(a, mimeType, size, fileName, fileNameOrg, im
}; };
module.exports = { module.exports = {
convert: function(a, fileName, localFilePath, payloadCallback, progress_callback) { convert: function(a, fileName, localFilePath, payloadCallback, progressCallback) {
getMime(fileName, localFilePath, function(err, mimeType){ getMime(fileName, localFilePath, function(err, mimeType){
console.log("[convert] fn: "+fileName+" local: "+localFilePath+" mimeType:", mimeType); console.log("[convert] fn: "+fileName+" local: "+localFilePath+" mimeType:", mimeType);
@ -311,8 +311,8 @@ module.exports = {
var s3Key = "s"+ a.space_id.toString() + "/a" + a._id.toString() + "/" + fileName; var s3Key = "s"+ a.space_id.toString() + "/a" + a._id.toString() + "/" + fileName;
uploader.uploadFile(s3Key, "image/gif", localFilePath, function(err, url) { uploader.uploadFile(s3Key, "image/gif", localFilePath, function(err, url) {
if(err)callback(err); if (err) payloadCallback(err);
else{ else {
console.log(localFilePath); console.log(localFilePath);
var stats = fs.statSync(localFilePath); var stats = fs.statSync(localFilePath);
@ -357,8 +357,8 @@ module.exports = {
thumbnail: function(callback) { thumbnail: function(callback) {
createThumbnailForVideo(fileName, localFilePath, function(err, created){ createThumbnailForVideo(fileName, localFilePath, function(err, created){
console.log("thumbnail created: ", err, created); console.log("thumbnail created: ", err, created);
if(err) callback(err); if (err) callback(err);
else{ else {
var keyName = "s" + a.space_id.toString() + "/a" + a._id.toString() + "/" + fileName + ".jpg" ; var keyName = "s" + a.space_id.toString() + "/a" + a._id.toString() + "/" + fileName + ".jpg" ;
uploader.uploadFile(keyName, "image/jpeg", created, function(err, url){ uploader.uploadFile(keyName, "image/jpeg", created, function(err, url){
if (err) callback(err); if (err) callback(err);
@ -380,7 +380,7 @@ module.exports = {
else callback(null, url); else callback(null, url);
}); });
} }
}, progress_callback); }, progressCallback);
} }
}, },
mp4: function(callback) { mp4: function(callback) {
@ -396,7 +396,7 @@ module.exports = {
else callback(null, url); else callback(null, url);
}); });
} }
}, progress_callback); }, progressCallback);
} }
}, },
original: function(callback){ original: function(callback){
@ -404,7 +404,7 @@ module.exports = {
callback(null, url); callback(null, url);
}); });
} }
}, function(err, results){ }, function(err, results) {
console.log(err, results); console.log(err, results);
if (err) payloadCallback(err, a); if (err) payloadCallback(err, a);
@ -438,11 +438,9 @@ module.exports = {
db.packArtifact(a); db.packArtifact(a);
a.updated_at = new Date(); a.updated_at = new Date();
a.save(function(err) { a.save().then(function() {
if (err) payloadCallback(err, null);
else {
fs.unlink(localFilePath, function (err) { fs.unlink(localFilePath, function (err) {
if (err){ if (err) {
console.error(err); console.error(err);
payloadCallback(err, null); payloadCallback(err, null);
} else { } else {
@ -450,7 +448,6 @@ module.exports = {
payloadCallback(null, a); payloadCallback(null, a);
} }
}); });
}
}); });
} }
}); });

View File

@ -123,11 +123,11 @@ router.post('/:artifact_id/payload', function(req, res, next) {
var writeStream = fs.createWriteStream(localFilePath); var writeStream = fs.createWriteStream(localFilePath);
var stream = req.pipe(writeStream); var stream = req.pipe(writeStream);
var progress_callback = function(progress_msg) { var progressCallback = function(progressMsg) {
a.description = progress_msg.toString(); a.description = progressMsg.toString();
db.packArtifact(a); db.packArtifact(a);
a.save(); a.save();
redis.sendMessage("update", a, JSON.stringify(a), req.channelId); redis.sendMessage("update", "Artifact", a, req.channelId);
}; };
stream.on('finish', function() { stream.on('finish', function() {
@ -137,7 +137,7 @@ router.post('/:artifact_id/payload', function(req, res, next) {
db.Space.update({ updated_at: new Date() }, {where: {_id: req.space._id}}); db.Space.update({ updated_at: new Date() }, {where: {_id: req.space._id}});
res.distributeUpdate("Artifact", artifact); res.distributeUpdate("Artifact", artifact);
} }
}, progress_callback); }, progressCallback);
}); });
} else { } else {
res.status(401).json({ res.status(401).json({