aboutsummaryrefslogtreecommitdiffhomepage
path: root/models/videos.js
blob: 4da389c24e61e95e6b152b3ebde27a3f5f49938f (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
'use strict'

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

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

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

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

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

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

var Videos = {
  add: add,
  addRemotes: addRemotes,
  get: get,
  getVideoState: getVideoState,
  isOwned: isOwned,
  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)

  var 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 () {}

  var to_add = []

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

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

    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 getVideoState (id, callback) {
  get(id, function (err, video) {
    if (err) return callback(err)

    var exist = (video !== null)
    var owned = false
    if (exist === true) {
      owned = (video.namePath !== null)
    }

    return callback(null, { exist: exist, owned: owned })
  })
}

function isOwned (id, callback) {
  VideosDB.findById(id, function (err, video) {
    if (err || !video) {
      if (!err) err = new Error('Cannot find this video.')
      logger.error('Cannot find this video.')
      return callback(err)
    }

    if (video.namePath === null) {
      var error_string = 'Cannot remove the video of another pod.'
      logger.error(error_string)
      return callback(new Error(error_string), false, video)
    }

    callback(null, true, 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) {
  // TODO { podUrl: { $in: urls } }
  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)
    }

    var 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