Change api output for videos
[oweals/peertube.git] / server / lib / videos.js
1 'use strict'
2
3 const async = require('async')
4 const config = require('config')
5 const pathUtils = require('path')
6 const webtorrent = require('../lib/webtorrent')
7
8 const logger = require('../helpers/logger')
9 const Videos = require('../models/videos')
10
11 const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads'))
12
13 const videos = {
14   getVideoState: getVideoState,
15   seed: seed,
16   seedAllExisting: seedAllExisting
17 }
18
19 function getVideoState (video) {
20   const exist = (video !== null)
21   let owned = false
22   if (exist === true) {
23     owned = (video.namePath !== null)
24   }
25
26   return { exist: exist, owned: owned }
27 }
28
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