aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/videos.js
blob: 1bb6f249317dd58db99a55b96901a968e54b42d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
;(function () {
  'use strict'

  var async = require('async')
  var config = require('config')
  var webtorrent = require('../lib/webtorrent')

  var logger = require('../helpers/logger')
  var Videos = require('../models/videos')

  var uploadDir = __dirname + '/../' + config.get('storage.uploads')

  var videos = {
    seed: seed,
    seedAllExisting: seedAllExisting
  }

  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
})()