aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/videos.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-06-24 17:42:51 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-06-24 17:42:51 +0200
commitaaf61f3810e6d57c5130af959bd2860df32775e7 (patch)
tree3871b20359aa56995a4b8974ae5cb6b911dd73f5 /server/models/videos.js
parent07d932038745e773359fa87bac6be70523f593ee (diff)
downloadPeerTube-aaf61f3810e6d57c5130af959bd2860df32775e7.tar.gz
PeerTube-aaf61f3810e6d57c5130af959bd2860df32775e7.tar.zst
PeerTube-aaf61f3810e6d57c5130af959bd2860df32775e7.zip
Video model refractoring -> use mongoose api
Diffstat (limited to 'server/models/videos.js')
-rw-r--r--server/models/videos.js162
1 files changed, 0 insertions, 162 deletions
diff --git a/server/models/videos.js b/server/models/videos.js
deleted file mode 100644
index c177b414c..000000000
--- a/server/models/videos.js
+++ /dev/null
@@ -1,162 +0,0 @@
1'use strict'
2
3const async = require('async')
4const config = require('config')
5const mongoose = require('mongoose')
6
7const logger = require('../helpers/logger')
8
9const http = config.get('webserver.https') === true ? 'https' : 'http'
10const host = config.get('webserver.host')
11const port = config.get('webserver.port')
12
13// ---------------------------------------------------------------------------
14
15// TODO: add indexes on searchable columns
16const videosSchema = mongoose.Schema({
17 name: String,
18 namePath: String,
19 description: String,
20 magnetUri: String,
21 podUrl: String,
22 author: String,
23 duration: Number,
24 thumbnail: String,
25 tags: [ String ],
26 createdDate: {
27 type: Date,
28 default: Date.now
29 }
30})
31const VideosDB = mongoose.model('videos', videosSchema)
32
33// ---------------------------------------------------------------------------
34
35const Videos = {
36 add: add,
37 addRemotes: addRemotes,
38 get: get,
39 list: list,
40 listFromUrl: listFromUrl,
41 listFromUrls: listFromUrls,
42 listFromUrlAndMagnets: listFromUrlAndMagnets,
43 listFromRemotes: listFromRemotes,
44 listOwned: listOwned,
45 removeOwned: removeOwned,
46 removeByIds: removeByIds,
47 search: search
48}
49
50function add (video, callback) {
51 logger.info('Adding %s video to database.', video.name)
52
53 const params = video
54 params.podUrl = http + '://' + host + ':' + port
55
56 VideosDB.create(params, function (err, insertedVideo) {
57 if (err) {
58 logger.error('Cannot insert this video into database.')
59 return callback(err)
60 }
61
62 callback(null, insertedVideo)
63 })
64}
65
66function addRemotes (videos, callback) {
67 videos.forEach(function (video) {
68 // Ensure they are remote videos
69 video.namePath = null
70 })
71
72 VideosDB.create(videos, callback)
73}
74
75function get (id, callback) {
76 VideosDB.findById(id, function (err, video) {
77 if (err) {
78 logger.error('Cannot get this video.')
79 return callback(err)
80 }
81
82 return callback(null, video)
83 })
84}
85
86function list (start, count, sort, callback) {
87 const query = {}
88 return findWithCount(query, start, count, sort, callback)
89}
90
91function listFromUrl (fromUrl, callback) {
92 VideosDB.find({ podUrl: fromUrl }, callback)
93}
94
95function listFromUrls (fromUrls, callback) {
96 VideosDB.find({ podUrl: { $in: fromUrls } }, callback)
97}
98
99function listFromUrlAndMagnets (fromUrl, magnets, callback) {
100 VideosDB.find({ podUrl: fromUrl, magnetUri: { $in: magnets } }, callback)
101}
102
103function listFromRemotes (callback) {
104 VideosDB.find({ namePath: null }, callback)
105}
106
107function listOwned (callback) {
108 // If namePath is not null this is *our* video
109 VideosDB.find({ namePath: { $ne: null } }, function (err, videosList) {
110 if (err) {
111 logger.error('Cannot get the list of owned videos.')
112 return callback(err)
113 }
114
115 return callback(null, videosList)
116 })
117}
118
119// Return the video in the callback
120function removeOwned (id, callback) {
121 VideosDB.findByIdAndRemove(id, callback)
122}
123
124// Use the magnet Uri because the _id field is not the same on different servers
125function removeByIds (ids, callback) {
126 VideosDB.remove({ _id: { $in: ids } }, callback)
127}
128
129function search (value, field, start, count, sort, callback) {
130 const query = {}
131 // Make an exact search with the magnet
132 if (field === 'magnetUri' || field === 'tags') {
133 query[field] = value
134 } else {
135 query[field] = new RegExp(value)
136 }
137
138 findWithCount(query, start, count, sort, callback)
139}
140
141// ---------------------------------------------------------------------------
142
143module.exports = Videos
144
145// ---------------------------------------------------------------------------
146
147function findWithCount (query, start, count, sort, callback) {
148 async.parallel([
149 function (asyncCallback) {
150 VideosDB.find(query).skip(start).limit(start + count).sort(sort).exec(asyncCallback)
151 },
152 function (asyncCallback) {
153 VideosDB.count(query, asyncCallback)
154 }
155 ], function (err, results) {
156 if (err) return callback(err)
157
158 const videos = results[0]
159 const totalVideos = results[1]
160 return callback(null, videos, totalVideos)
161 })
162}