| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import { Meteor } from 'meteor/meteor';
- import { Converted } from './links';
- _ = lodash;
-
- const bindHandlers = (ressource, type, path, filename, id, url, ) => {
- let capType = _.capitalize(type);
- ressource.on('unavailable', Meteor.bindEnvironment(err => {
- console.error('KO', url, err);
- Converted.update({ _id: id }, { $set: { [`error_${type}`]: `${capType} is unreachable` } });
- }))
- .on('available', info => {
- console.info('OK');
- ressource.download(path);
- })
- .on('progress', Meteor.bindEnvironment(percent => {
- Converted.update({ _id: id }, { $set: { [`${type}_progress`]: percent } })
- }))
- .on('error', Meteor.bindEnvironment(err => {
- console.error(`${capType} download error: ${err.message}`);
- Converted.update({ _id: id }, { $set: { [`error_${type}`]: `${capType} cannot be saved` } });
- }))
- .on('end', Meteor.bindEnvironment(() => {
- console.info(`${capType} download complete for ${url}`);
- Converted.update({ _id: id }, { $set: { [type]: filename, [`${type}_progress`]: 100 } });
- }));
- };
-
- const convertAudio = (url, filename, id) => {
- if (Meteor.isServer) {
- import { AudioConverter } from '../../server/Converter';
- import path from 'path';
-
- const filePath = path.resolve(process.env.UPLOAD_DIR, filename);
- console.log(`Converting ${url} to MP3...`);
-
- let audio_dl = new AudioConverter(url);
- bindHandlers(audio_dl, 'audio', filePath + '.mp3', filename + '.mp3', id, url);
- }
- };
-
- const convertVideo = (url, filename, id) => {
- if (Meteor.isServer) {
- import { VideoConverter } from '../../server/Converter';
- import path from 'path';
-
- const filePath = path.resolve(process.env.UPLOAD_DIR, filename);
- console.log(`Converting ${url} to MP4 ...`);
-
- let video_dl = new VideoConverter(url);
- bindHandlers(video_dl, 'video', filePath + '.mp4', filename + '.mp4', id, url);
- }
- };
-
- export { convertAudio, convertVideo };
|