]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/videos.js
9cac8eddabd9e40129811ba359eebe1a449b1992
[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, sort, callback) {
84 VideosDB.find({}).skip(start).limit(start + count).sort(sort)
85 .exec(function (err, videosList) {
86 if (err) {
87 logger.error('Cannot get the list of the videos.')
88 return callback(err)
89 }
90
91 return callback(null, videosList)
92 })
93 }
94
95 function listFromUrl (fromUrl, callback) {
96 VideosDB.find({ podUrl: fromUrl }, callback)
97 }
98
99 function listFromUrls (fromUrls, callback) {
100 VideosDB.find({ podUrl: { $in: fromUrls } }, callback)
101 }
102
103 function listFromUrlAndMagnets (fromUrl, magnets, callback) {
104 VideosDB.find({ podUrl: fromUrl, magnetUri: { $in: magnets } }, callback)
105 }
106
107 function listFromRemotes (callback) {
108 VideosDB.find({ namePath: null }, callback)
109 }
110
111 function listOwned (callback) {
112 // If namePath is not null this is *our* video
113 VideosDB.find({ namePath: { $ne: null } }, function (err, videosList) {
114 if (err) {
115 logger.error('Cannot get the list of owned videos.')
116 return callback(err)
117 }
118
119 return callback(null, videosList)
120 })
121 }
122
123 // Return the video in the callback
124 function removeOwned (id, callback) {
125 VideosDB.findByIdAndRemove(id, callback)
126 }
127
128 // Use the magnet Uri because the _id field is not the same on different servers
129 function removeByIds (ids, callback) {
130 VideosDB.remove({ _id: { $in: ids } }, callback)
131 }
132
133 function search (name, start, count, sort, callback) {
134 VideosDB.find({ name: new RegExp(name) }).skip(start).limit(start + count).sort(sort)
135 .exec(function (err, videos) {
136 if (err) {
137 logger.error('Cannot search the videos.')
138 return callback(err)
139 }
140
141 return callback(null, videos)
142 })
143 }
144
145 // ---------------------------------------------------------------------------
146
147 module.exports = Videos