3 const async = require('async')
4 const config = require('config')
5 const ffmpeg = require('fluent-ffmpeg')
6 const pathUtils = require('path')
7 const webtorrent = require('../lib/webtorrent')
9 const logger = require('../helpers/logger')
10 const Videos = require('../models/videos')
12 const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads'))
15 getVideoDuration: getVideoDuration,
16 getVideoState: getVideoState,
18 seedAllExisting: seedAllExisting
21 function getVideoDuration (video_path, callback) {
22 ffmpeg.ffprobe(video_path, function (err, metadata) {
23 if (err) return callback(err)
25 return callback(null, Math.floor(metadata.format.duration))
29 function getVideoState (video) {
30 const exist = (video !== null)
33 owned = (video.namePath !== null)
36 return { exist: exist, owned: owned }
39 function seed (path, callback) {
40 logger.info('Seeding %s...', path)
42 webtorrent.seed(path, function (torrent) {
43 logger.info('%s seeded (%s).', path, torrent.magnetURI)
45 return callback(null, torrent)
49 function seedAllExisting (callback) {
50 Videos.listOwned(function (err, videos_list) {
52 logger.error('Cannot get list of the videos to seed.')
56 async.each(videos_list, function (video, each_callback) {
57 seed(uploadDir + video.namePath, function (err) {
59 logger.error('Cannot seed this video.')
69 // ---------------------------------------------------------------------------
71 module.exports = videos