Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. try {
  2. if (fs.existsSync(uploadPath + filename.substring(0, filename.lastIndexOf('.')) + '.mp4')) {
  3. res.end('<b><i>' + filename + '</i></b> already exists in the directory.');
  4. }
  5. else {
  6. const fstream = fs.createWriteStream(path.join(cfg.tempStoragePath, filename));
  7. file.pipe(fstream);
  8. console.log("nfile_type: " + file_type);
  9. filename = filename.substring(0, filename.lastIndexOf('.'));
  10.  
  11. // On finish of the copying file to temp location
  12. fstream.on('close', () => {
  13. hbjs.spawn({
  14. input: cfg.tempStoragePath + filename + '.' + file_type,
  15. output: uploadPath + filename + '.mp4'
  16. })
  17. .on('error', err => {
  18. // invalid user input, no video found etc
  19. console.log('error! No input video found at: n: ' + cfg.tempStoragePath + filename + '.' + file_type);
  20. res.send('Conversion of the file, <b><i>' + filename + '</i></b>, from <b>.' + file_type + '</b>' + ' to <b>.mp4</b> failed because no input video was found at: n: ' + cfg.tempStoragePath + filename + '.' + file_type);
  21. })
  22. .on('progress', progress => {
  23. progress_percent = (Number(progress.percentComplete) * 2 <= 100) ? Number(progress.percentComplete) * 2 : 100;
  24. eta = progress.eta.split(/[a-zA-Z]/);
  25. minutes = ((+eta[0]) * 60 + (+eta[1])) / 2;
  26. console.log('Percent complete: %d, ETA: %d ///// %s ==> mp4', progress_percent, minutes, file_type);
  27. })
  28. .on('end', end => {
  29. console.log('Conversion from .' + file_type + ' to .mp4 complete.');
  30. //delete the temp file
  31. fs.unlink(cfg.tempStoragePath + filename + '.' + file_type);
  32. let new_path = uploadPath + filename + '.mp4';
  33. let stat = fs.statSync(new_path);
  34. console.log(`Upload of '${filename}' finished`);
  35. if(Number(progress_percent) === Number(100))
  36. res.send('The file, <b><i>' + filename + '</i></b>, has been converted from <b>.' + file_type + '</b>' + ' to <b>.mp4</b> complete.');
  37. })
  38. });
  39. }
  40. }
  41.  
  42. catch (err) {
  43. res.end(err);
  44. }
  45.  
  46. request = new XMLHttpRequest();
  47. request.onreadystatechange = function () {
  48. if (request.readyState === XMLHttpRequest.DONE && request.status === 200) {
  49. showConversionModal('<p>' + request.responseText + '</p>', 'done');
  50. }
  51. };
  52. showSomeModal('something');
  53. request.open("POST", client.clientHost + ":" + client.clientPort + "/uploadVideoService");
  54. formData = new FormData();
  55. formData.append("file", files[0], files[0].name);
  56. request.send(formData);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement