Fix adding twice a torrent in webtorrent
[oweals/peertube.git] / server / models / videos.js
1 'use strict'
2
3 const config = require('config')
4 const mongoose = require('mongoose')
5
6 const logger = require('../helpers/logger')
7
8 const http = config.get('webserver.https') === true ? 'https' : 'http'
9 const host = config.get('webserver.host')
10 const port = config.get('webserver.port')
11
12 // ---------------------------------------------------------------------------
13
14 const videosSchema = mongoose.Schema({
15   name: String,
16   namePath: String,
17   description: String,
18   magnetUri: String,
19   podUrl: String,
20   author: String,
21   duration: Number,
22   thumbnail: String
23 })
24 const VideosDB = mongoose.model('videos', videosSchema)
25
26 // ---------------------------------------------------------------------------
27
28 const Videos = {
29   add: add,
30   addRemotes: addRemotes,
31   get: get,
32   list: list,
33   listFromUrl: listFromUrl,
34   listFromUrls: listFromUrls,
35   listFromUrlAndMagnets: listFromUrlAndMagnets,
36   listFromRemotes: listFromRemotes,
37   listOwned: listOwned,
38   removeOwned: removeOwned,
39   removeByIds: removeByIds,
40   search: search
41 }
42
43 function add (video, callback) {
44   logger.info('Adding %s video to database.', video.name)
45
46   const params = video
47   params.podUrl = http + '://' + host + ':' + port
48
49   VideosDB.create(params, function (err, video) {
50     if (err) {
51       logger.error('Cannot insert this video into database.')
52       return callback(err)
53     }
54
55     callback(null)
56   })
57 }
58
59 function addRemotes (videos, callback) {
60   videos.forEach(function (video) {
61     // Ensure they are remote videos
62     video.namePath = null
63   })
64
65   VideosDB.create(videos, callback)
66 }
67
68 function get (id, callback) {
69   VideosDB.findById(id, function (err, video) {
70     if (err) {
71       logger.error('Cannot get this video.')
72       return callback(err)
73     }
74
75     return callback(null, video)
76   })
77 }
78
79 function list (callback) {
80   VideosDB.find(function (err, videosList) {
81     if (err) {
82       logger.error('Cannot get the list of the videos.')
83       return callback(err)
84     }
85
86     return callback(null, videosList)
87   })
88 }
89
90 function listFromUrl (fromUrl, callback) {
91   VideosDB.find({ podUrl: fromUrl }, callback)
92 }
93
94 function listFromUrls (fromUrls, callback) {
95   VideosDB.find({ podUrl: { $in: fromUrls } }, callback)
96 }
97
98 function listFromUrlAndMagnets (fromUrl, magnets, callback) {
99   VideosDB.find({ podUrl: fromUrl, magnetUri: { $in: magnets } }, callback)
100 }
101
102 function listFromRemotes (callback) {
103   VideosDB.find({ namePath: null }, callback)
104 }
105
106 function listOwned (callback) {
107   // If namePath is not null this is *our* video
108   VideosDB.find({ namePath: { $ne: null } }, function (err, videosList) {
109     if (err) {
110       logger.error('Cannot get the list of owned videos.')
111       return callback(err)
112     }
113
114     return callback(null, videosList)
115   })
116 }
117
118 // Return the video in the callback
119 function removeOwned (id, callback) {
120   VideosDB.findByIdAndRemove(id, callback)
121 }
122
123 // Use the magnet Uri because the _id field is not the same on different servers
124 function removeByIds (ids, callback) {
125   VideosDB.remove({ _id: { $in: ids } }, callback)
126 }
127
128 function search (name, callback) {
129   VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
130     if (err) {
131       logger.error('Cannot search the videos.')
132       return callback(err)
133     }
134
135     return callback(null, videos)
136   })
137 }
138
139 // ---------------------------------------------------------------------------
140
141 module.exports = Videos