]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
f0f5567b C |
3 | const async = require('async') |
4 | const config = require('config') | |
5 | const pathUtils = require('path') | |
6 | const webtorrent = require('../lib/webtorrent') | |
9f10b292 | 7 | |
f0f5567b C |
8 | const logger = require('../helpers/logger') |
9 | const Videos = require('../models/videos') | |
9f10b292 | 10 | |
f0f5567b | 11 | const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads')) |
9f10b292 | 12 | |
f0f5567b | 13 | const videos = { |
5101105e | 14 | getVideoState: getVideoState, |
9f10b292 C |
15 | seed: seed, |
16 | seedAllExisting: seedAllExisting | |
17 | } | |
18 | ||
2df82d42 | 19 | function getVideoState (video) { |
f0f5567b C |
20 | const exist = (video !== null) |
21 | let owned = false | |
5101105e C |
22 | if (exist === true) { |
23 | owned = (video.namePath !== null) | |
24 | } | |
25 | ||
2df82d42 | 26 | return { exist: exist, owned: owned } |
5101105e C |
27 | } |
28 | ||
9f10b292 C |
29 | function seed (path, callback) { |
30 | logger.info('Seeding %s...', path) | |
31 | ||
32 | webtorrent.seed(path, function (torrent) { | |
33 | logger.info('%s seeded (%s).', path, torrent.magnetURI) | |
34 | ||
35 | return callback(null, torrent) | |
36 | }) | |
37 | } | |
38 | ||
39 | function seedAllExisting (callback) { | |
40 | Videos.listOwned(function (err, videos_list) { | |
41 | if (err) { | |
42 | logger.error('Cannot get list of the videos to seed.') | |
43 | return callback(err) | |
44 | } | |
45 | ||
46 | async.each(videos_list, function (video, each_callback) { | |
47 | seed(uploadDir + video.namePath, function (err) { | |
48 | if (err) { | |
49 | logger.error('Cannot seed this video.') | |
50 | return callback(err) | |
51 | } | |
52 | ||
53 | each_callback(null) | |
54 | }) | |
55 | }, callback) | |
56 | }) | |
57 | } | |
58 | ||
59 | // --------------------------------------------------------------------------- | |
60 | ||
61 | module.exports = videos |