Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try {
- if (fs.existsSync(uploadPath + filename.substring(0, filename.lastIndexOf('.')) + '.mp4')) {
- res.end('<b><i>' + filename + '</i></b> already exists in the directory.');
- }
- else {
- const fstream = fs.createWriteStream(path.join(cfg.tempStoragePath, filename));
- file.pipe(fstream);
- console.log("nfile_type: " + file_type);
- filename = filename.substring(0, filename.lastIndexOf('.'));
- // On finish of the copying file to temp location
- fstream.on('close', () => {
- hbjs.spawn({
- input: cfg.tempStoragePath + filename + '.' + file_type,
- output: uploadPath + filename + '.mp4'
- })
- .on('error', err => {
- // invalid user input, no video found etc
- console.log('error! No input video found at: n: ' + cfg.tempStoragePath + filename + '.' + file_type);
- 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);
- })
- .on('progress', progress => {
- progress_percent = (Number(progress.percentComplete) * 2 <= 100) ? Number(progress.percentComplete) * 2 : 100;
- eta = progress.eta.split(/[a-zA-Z]/);
- minutes = ((+eta[0]) * 60 + (+eta[1])) / 2;
- console.log('Percent complete: %d, ETA: %d ///// %s ==> mp4', progress_percent, minutes, file_type);
- })
- .on('end', end => {
- console.log('Conversion from .' + file_type + ' to .mp4 complete.');
- //delete the temp file
- fs.unlink(cfg.tempStoragePath + filename + '.' + file_type);
- let new_path = uploadPath + filename + '.mp4';
- let stat = fs.statSync(new_path);
- console.log(`Upload of '${filename}' finished`);
- if(Number(progress_percent) === Number(100))
- res.send('The file, <b><i>' + filename + '</i></b>, has been converted from <b>.' + file_type + '</b>' + ' to <b>.mp4</b> complete.');
- })
- });
- }
- }
- catch (err) {
- res.end(err);
- }
- request = new XMLHttpRequest();
- request.onreadystatechange = function () {
- if (request.readyState === XMLHttpRequest.DONE && request.status === 200) {
- showConversionModal('<p>' + request.responseText + '</p>', 'done');
- }
- };
- showSomeModal('something');
- request.open("POST", client.clientHost + ":" + client.clientPort + "/uploadVideoService");
- formData = new FormData();
- formData.append("file", files[0], files[0].name);
- request.send(formData);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement