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