]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/videos.js
Change api output for videos
[github/Chocobozzz/PeerTube.git] / server / lib / videos.js
... / ...
CommitLineData
1'use strict'
2
3const async = require('async')
4const config = require('config')
5const pathUtils = require('path')
6const webtorrent = require('../lib/webtorrent')
7
8const logger = require('../helpers/logger')
9const Videos = require('../models/videos')
10
11const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads'))
12
13const videos = {
14 getVideoState: getVideoState,
15 seed: seed,
16 seedAllExisting: seedAllExisting
17}
18
19function 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
29function 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
39function 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
61module.exports = videos