aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/videos.js
blob: e02158be78a3d3c3b50b1522c2cc06d5e11cde81 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'use strict'

const async = require('async')
const config = require('config')
const dz = require('dezalgo')
const fs = require('fs')
const mongoose = require('mongoose')
const path = require('path')

const logger = require('../helpers/logger')

const http = config.get('webserver.https') === true ? 'https' : 'http'
const host = config.get('webserver.host')
const port = config.get('webserver.port')
const uploadDir = path.join(__dirname, '..', '..', config.get('storage.uploads'))

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

const videosSchema = mongoose.Schema({
  name: String,
  namePath: String,
  description: String,
  magnetUri: String,
  podUrl: String,
  author: String,
  duration: Number
})
const VideosDB = mongoose.model('videos', videosSchema)

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

const Videos = {
  add: add,
  addRemotes: addRemotes,
  get: get,
  list: list,
  listOwned: listOwned,
  removeOwned: removeOwned,
  removeAllRemotes: removeAllRemotes,
  removeAllRemotesOf: removeAllRemotesOf,
  removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
  search: search
}

function add (video, callback) {
  logger.info('Adding %s video to database.', video.name)

  const params = video
  params.podUrl = http + '://' + host + ':' + port

  VideosDB.create(params, function (err, video) {
    if (err) {
      logger.error('Cannot insert this video into database.')
      return callback(err)
    }

    callback(null)
  })
}

// TODO: avoid doublons
function addRemotes (videos, callback) {
  if (!callback) callback = function () {}

  const to_add = []

  async.each(videos, function (video, callback_each) {
    callback_each = dz(callback_each)
    logger.debug('Add remote video from pod: %s', video.podUrl)

    const params = {
      name: video.name,
      namePath: null,
      description: video.description,
      magnetUri: video.magnetUri,
      podUrl: video.podUrl,
      duration: video.duration
    }

    to_add.push(params)

    callback_each()
  }, function () {
    VideosDB.create(to_add, function (err, videos) {
      if (err) {
        logger.error('Cannot insert this remote video.')
        return callback(err)
      }

      return callback(null, videos)
    })
  })
}

function get (id, callback) {
  VideosDB.findById(id, function (err, video) {
    if (err) {
      logger.error('Cannot get this video.')
      return callback(err)
    }

    return callback(null, video)
  })
}

function list (callback) {
  VideosDB.find(function (err, videos_list) {
    if (err) {
      logger.error('Cannot get the list of the videos.')
      return callback(err)
    }

    return callback(null, videos_list)
  })
}

function listOwned (callback) {
  // If namePath is not null this is *our* video
  VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
    if (err) {
      logger.error('Cannot get the list of owned videos.')
      return callback(err)
    }

    return callback(null, videos_list)
  })
}

function removeOwned (id, callback) {
  VideosDB.findByIdAndRemove(id, function (err, video) {
    if (err) {
      logger.error('Cannot remove the torrent.')
      return callback(err)
    }

    fs.unlink(uploadDir + video.namePath, function (err) {
      if (err) {
        logger.error('Cannot remove this video file.')
        return callback(err)
      }

      callback(null)
    })
  })
}

function removeAllRemotes (callback) {
  VideosDB.remove({ namePath: null }, callback)
}

function removeAllRemotesOf (fromUrl, callback) {
  VideosDB.remove({ podUrl: fromUrl }, callback)
}

// Use the magnet Uri because the _id field is not the same on different servers
function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
  if (callback === undefined) callback = function () {}

  VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
    if (err || !videos) {
      logger.error('Cannot find the torrent URI of these remote videos.')
      return callback(err)
    }

    const to_remove = []
    async.each(videos, function (video, callback_async) {
      callback_async = dz(callback_async)

      if (video.podUrl !== fromUrl) {
        logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl)
      } else {
        to_remove.push(video._id)
      }

      callback_async()
    }, function () {
      VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
        if (err) {
          logger.error('Cannot remove the remote videos.')
          return callback(err)
        }

        logger.info('Removed remote videos from %s.', fromUrl)
        callback(null)
      })
    })
  })
}

function search (name, callback) {
  VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
    if (err) {
      logger.error('Cannot search the videos.')
      return callback(err)
    }

    return callback(null, videos)
  })
}

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

module.exports = Videos