aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/videos.js
blob: eaea35b7fe40a0b51ef3b48a7e957facafd7448b (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
'use strict'

const config = require('config')
const mongoose = require('mongoose')

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 videosSchema = mongoose.Schema({
  name: String,
  namePath: String,
  description: String,
  magnetUri: String,
  podUrl: String,
  author: String,
  duration: Number,
  thumbnail: String
})
const VideosDB = mongoose.model('videos', videosSchema)

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

const Videos = {
  add: add,
  addRemotes: addRemotes,
  get: get,
  list: list,
  listFromUrl: listFromUrl,
  listFromUrls: listFromUrls,
  listFromUrlAndMagnets: listFromUrlAndMagnets,
  listFromRemotes: listFromRemotes,
  listOwned: listOwned,
  removeOwned: removeOwned,
  removeByIds: removeByIds,
  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)
  })
}

function addRemotes (videos, callback) {
  videos.forEach(function (video) {
    // Ensure they are remote videos
    video.namePath = null
  })

  VideosDB.create(videos, callback)
}

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

    return callback(null, videosList)
  })
}

function listFromUrl (fromUrl, callback) {
  VideosDB.find({ podUrl: fromUrl }, callback)
}

function listFromUrls (fromUrls, callback) {
  VideosDB.find({ podUrl: { $in: fromUrls } }, callback)
}

function listFromUrlAndMagnets (fromUrl, magnets, callback) {
  VideosDB.find({ podUrl: fromUrl, magnetUri: { $in: magnets } }, callback)
}

function listFromRemotes (callback) {
  VideosDB.find({ namePath: null }, callback)
}

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

    return callback(null, videosList)
  })
}

// Return the video in the callback
function removeOwned (id, callback) {
  VideosDB.findByIdAndRemove(id, callback)
}

// Use the magnet Uri because the _id field is not the same on different servers
function removeByIds (ids, callback) {
  VideosDB.remove({ _id: { $in: ids } }, callback)
}

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