2016-02-07 11:23:23 +01:00
|
|
|
'use strict'
|
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const async = require('async')
|
|
|
|
const config = require('config')
|
|
|
|
const pathUtils = require('path')
|
|
|
|
const webtorrent = require('../lib/webtorrent')
|
2016-02-07 11:23:23 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const logger = require('../helpers/logger')
|
|
|
|
const Videos = require('../models/videos')
|
2016-02-07 11:23:23 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads'))
|
2016-02-07 11:23:23 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const videos = {
|
2016-03-16 21:37:17 +01:00
|
|
|
getVideoState: getVideoState,
|
2016-02-07 11:23:23 +01:00
|
|
|
seed: seed,
|
|
|
|
seedAllExisting: seedAllExisting
|
|
|
|
}
|
|
|
|
|
2016-03-18 16:28:09 +01:00
|
|
|
function getVideoState (video) {
|
2016-03-16 22:29:27 +01:00
|
|
|
const exist = (video !== null)
|
|
|
|
let owned = false
|
2016-03-16 21:37:17 +01:00
|
|
|
if (exist === true) {
|
|
|
|
owned = (video.namePath !== null)
|
|
|
|
}
|
|
|
|
|
2016-03-18 16:28:09 +01:00
|
|
|
return { exist: exist, owned: owned }
|
2016-03-16 21:37:17 +01:00
|
|
|
}
|
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function seed (path, callback) {
|
|
|
|
logger.info('Seeding %s...', path)
|
|
|
|
|
|
|
|
webtorrent.seed(path, function (torrent) {
|
|
|
|
logger.info('%s seeded (%s).', path, torrent.magnetURI)
|
|
|
|
|
|
|
|
return callback(null, torrent)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function seedAllExisting (callback) {
|
|
|
|
Videos.listOwned(function (err, videos_list) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot get list of the videos to seed.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
async.each(videos_list, function (video, each_callback) {
|
|
|
|
seed(uploadDir + video.namePath, function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot seed this video.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
each_callback(null)
|
|
|
|
})
|
|
|
|
}, callback)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
module.exports = videos
|