]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/videos.js
250ad3952b27e297712c75c0cf9da3bedc96edc2
[github/Chocobozzz/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 createdDate: {
24 type: Date,
25 default: Date.now
26 }
27 })
28 const VideosDB = mongoose.model('videos', videosSchema)
29
30 // ---------------------------------------------------------------------------
31
32 const Videos = {
33 add: add,
34 addRemotes: addRemotes,
35 get: get,
36 list: list,
37 listFromUrl: listFromUrl,
38 listFromUrls: listFromUrls,
39 listFromUrlAndMagnets: listFromUrlAndMagnets,
40 listFromRemotes: listFromRemotes,
41 listOwned: listOwned,
42 removeOwned: removeOwned,
43 removeByIds: removeByIds,
44 search: search
45 }
46
47 function add (video, callback) {
48 logger.info('Adding %s video to database.', video.name)
49
50 const params = video
51 params.podUrl = http + '://' + host + ':' + port
52
53 VideosDB.create(params, function (err, insertedVideo) {
54 if (err) {
55 logger.error('Cannot insert this video into database.')
56 return callback(err)
57 }
58
59 callback(null, insertedVideo)
60 })
61 }
62
63 function addRemotes (videos, callback) {
64 videos.forEach(function (video) {
65 // Ensure they are remote videos
66 video.namePath = null
67 })
68
69 VideosDB.create(videos, callback)
70 }
71
72 function get (id, callback) {
73 VideosDB.findById(id, function (err, video) {
74 if (err) {
75 logger.error('Cannot get this video.')
76 return callback(err)
77 }
78
79 return callback(null, video)
80 })
81 }
82
83 function list (start, count, callback) {
84 VideosDB.find({}).skip(start).limit(start + count).exec(function (err, videosList) {
85 if (err) {
86 logger.error('Cannot get the list of the videos.')
87 return callback(err)
88 }
89
90 return callback(null, videosList)
91 })
92 }
93
94 function listFromUrl (fromUrl, callback) {
95 VideosDB.find({ podUrl: fromUrl }, callback)
96 }
97
98 function listFromUrls (fromUrls, callback) {
99 VideosDB.find({ podUrl: { $in: fromUrls } }, callback)
100 }
101
102 function listFromUrlAndMagnets (fromUrl, magnets, callback) {
103 VideosDB.find({ podUrl: fromUrl, magnetUri: { $in: magnets } }, callback)
104 }
105
106 function listFromRemotes (callback) {
107 VideosDB.find({ namePath: null }, callback)
108 }
109
110 function listOwned (callback) {
111 // If namePath is not null this is *our* video
112 VideosDB.find({ namePath: { $ne: null } }, function (err, videosList) {
113 if (err) {
114 logger.error('Cannot get the list of owned videos.')
115 return callback(err)
116 }
117
118 return callback(null, videosList)
119 })
120 }
121
122 // Return the video in the callback
123 function removeOwned (id, callback) {
124 VideosDB.findByIdAndRemove(id, callback)
125 }
126
127 // Use the magnet Uri because the _id field is not the same on different servers
128 function removeByIds (ids, callback) {
129 VideosDB.remove({ _id: { $in: ids } }, callback)
130 }
131
132 function search (name, start, count, callback) {
133 VideosDB.find({ name: new RegExp(name) }).skip(start).limit(start + count)
134 .exec(function (err, videos) {
135 if (err) {
136 logger.error('Cannot search the videos.')
137 return callback(err)
138 }
139
140 return callback(null, videos)
141 })
142 }
143
144 // ---------------------------------------------------------------------------
145
146 module.exports = Videos