]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/videos.js
Move video duration logic in lib/
[github/Chocobozzz/PeerTube.git] / server / lib / videos.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b
C
3const async = require('async')
4const config = require('config')
0ae6a09d 5const ffmpeg = require('fluent-ffmpeg')
f0f5567b
C
6const pathUtils = require('path')
7const webtorrent = require('../lib/webtorrent')
9f10b292 8
f0f5567b
C
9const logger = require('../helpers/logger')
10const Videos = require('../models/videos')
9f10b292 11
f0f5567b 12const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads'))
9f10b292 13
f0f5567b 14const videos = {
0ae6a09d 15 getVideoDuration: getVideoDuration,
5101105e 16 getVideoState: getVideoState,
9f10b292
C
17 seed: seed,
18 seedAllExisting: seedAllExisting
19}
20
0ae6a09d
C
21function getVideoDuration (video_path, callback) {
22 ffmpeg.ffprobe(video_path, function (err, metadata) {
23 if (err) return callback(err)
24
25 return callback(null, Math.floor(metadata.format.duration))
26 })
27}
28
2df82d42 29function getVideoState (video) {
f0f5567b
C
30 const exist = (video !== null)
31 let owned = false
5101105e
C
32 if (exist === true) {
33 owned = (video.namePath !== null)
34 }
35
2df82d42 36 return { exist: exist, owned: owned }
5101105e
C
37}
38
9f10b292
C
39function seed (path, callback) {
40 logger.info('Seeding %s...', path)
41
42 webtorrent.seed(path, function (torrent) {
43 logger.info('%s seeded (%s).', path, torrent.magnetURI)
44
45 return callback(null, torrent)
46 })
47}
48
49function seedAllExisting (callback) {
50 Videos.listOwned(function (err, videos_list) {
51 if (err) {
52 logger.error('Cannot get list of the videos to seed.')
53 return callback(err)
54 }
55
56 async.each(videos_list, function (video, each_callback) {
57 seed(uploadDir + video.namePath, function (err) {
58 if (err) {
59 logger.error('Cannot seed this video.')
60 return callback(err)
61 }
62
63 each_callback(null)
64 })
65 }, callback)
66 })
67}
68
69// ---------------------------------------------------------------------------
70
71module.exports = videos