aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/videos.js
blob: 154c31ef9e4f3537913b90bee673a30d0001edd8 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict'

const async = require('async')
const config = require('config')
const ffmpeg = require('fluent-ffmpeg')
const fs = require('fs')
const map = require('lodash/map')
const pathUtils = require('path')

const constants = require('../initializers/constants')
const logger = require('../helpers/logger')
const utils = require('../helpers/utils')
const Videos = require('../models/videos')
const webtorrent = require('../lib/webtorrent')

const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads'))
const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails'))

const videos = {
  createRemoteVideos: createRemoteVideos,
  getVideoDuration: getVideoDuration,
  getVideoState: getVideoState,
  createVideoThumbnail: createVideoThumbnail,
  removeVideosDataFromDisk: removeVideosDataFromDisk,
  removeRemoteVideos: removeRemoteVideos,
  seed: seed,
  seedAllExisting: seedAllExisting
}

function createRemoteVideos (videos, callback) {
  // Create the remote videos from the new pod
  createRemoteVideoObjects(videos, function (err, remoteVideos) {
    if (err) return callback(err)

    Videos.addRemotes(remoteVideos, callback)
  })
}

function getVideoDuration (videoPath, callback) {
  ffmpeg.ffprobe(videoPath, function (err, metadata) {
    if (err) return callback(err)

    return callback(null, Math.floor(metadata.format.duration))
  })
}

function getVideoState (video) {
  const exist = (video !== null)
  let owned = false
  if (exist === true) {
    owned = (video.namePath !== null)
  }

  return { exist: exist, owned: owned }
}

function createVideoThumbnail (videoPath, callback) {
  const filename = pathUtils.basename(videoPath) + '.jpg'
  ffmpeg(videoPath)
    .on('error', callback)
    .on('end', function () {
      callback(null, filename)
    })
    .thumbnail({
      count: 1,
      folder: thumbnailsDir,
      size: constants.THUMBNAILS_SIZE,
      filename: filename
    })
}

// Remove video datas from disk (video file, thumbnail...)
function removeVideosDataFromDisk (videos, callback) {
  async.each(videos, function (video, callbackEach) {
    fs.unlink(thumbnailsDir + video.thumbnail, function (err) {
      if (err) logger.error('Cannot remove the video thumbnail')

      if (getVideoState(video).owned === true) {
        fs.unlink(uploadDir + video.namePath, function (err) {
          if (err) {
            logger.error('Cannot remove this video file.')
            return callbackEach(err)
          }

          callbackEach(null)
        })
      } else {
        callbackEach(null)
      }
    })
  }, callback)
}

function removeRemoteVideos (videos, callback) {
  Videos.removeByIds(map(videos, '_id'), function (err) {
    if (err) return callback(err)

    removeVideosDataFromDisk(videos, callback)
  })
}

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, videosList) {
    if (err) {
      logger.error('Cannot get list of the videos to seed.')
      return callback(err)
    }

    async.each(videosList, function (video, callbackEach) {
      seed(uploadDir + video.namePath, function (err) {
        if (err) {
          logger.error('Cannot seed this video.')
          return callback(err)
        }

        callbackEach(null)
      })
    }, callback)
  })
}

// ---------------------------------------------------------------------------

module.exports = videos

// ---------------------------------------------------------------------------

function createRemoteVideoObjects (videos, callback) {
  const remoteVideos = []

  async.each(videos, function (video, callbackEach) {
    // Creating the thumbnail for this remote video
    utils.generateRandomString(16, function (err, randomString) {
      if (err) return callbackEach(err)

      const thumbnailName = randomString + '.jpg'
      createThumbnailFromBase64(thumbnailName, video.thumbnailBase64, function (err) {
        if (err) return callbackEach(err)

        const params = {
          name: video.name,
          description: video.description,
          magnetUri: video.magnetUri,
          podUrl: video.podUrl,
          duration: video.duration,
          thumbnail: thumbnailName
        }
        remoteVideos.push(params)

        callbackEach(null)
      })
    })
  },
  function (err) {
    if (err) return callback(err)

    callback(null, remoteVideos)
  })
}

function createThumbnailFromBase64 (thumbnailName, data, callback) {
  fs.writeFile(thumbnailsDir + thumbnailName, data, { encoding: 'base64' }, callback)
}