]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video.js
Server: use _id for image and video files. Add remoteId field
[github/Chocobozzz/PeerTube.git] / server / models / video.js
CommitLineData
aaf61f38
C
1'use strict'
2
052937db 3const createTorrent = require('create-torrent')
aaf61f38
C
4const ffmpeg = require('fluent-ffmpeg')
5const fs = require('fs')
1a42c9e2 6const parallel = require('async/parallel')
052937db 7const parseTorrent = require('parse-torrent')
aaf61f38 8const pathUtils = require('path')
052937db 9const magnet = require('magnet-uri')
aaf61f38
C
10const mongoose = require('mongoose')
11
12const constants = require('../initializers/constants')
e4c55619 13const customVideosValidators = require('../helpers/custom-validators').videos
aaf61f38 14const logger = require('../helpers/logger')
0ff21c1c 15const modelUtils = require('./utils')
aaf61f38 16
aaf61f38
C
17// ---------------------------------------------------------------------------
18
19// TODO: add indexes on searchable columns
20const VideoSchema = mongoose.Schema({
21 name: String,
558d7c23
C
22 extname: {
23 type: String,
24 enum: [ '.mp4', '.webm', '.ogv' ]
25 },
26 remoteId: mongoose.Schema.Types.ObjectId,
aaf61f38
C
27 description: String,
28 magnetUri: String,
29 podUrl: String,
30 author: String,
31 duration: Number,
32 thumbnail: String,
33 tags: [ String ],
34 createdDate: {
35 type: Date,
36 default: Date.now
37 }
38})
39
e4c55619
C
40VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
41VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
42VideoSchema.path('magnetUri').validate(customVideosValidators.isVideoMagnetUriValid)
43VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid)
44VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
45VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid)
aaf61f38
C
46// The tumbnail can be the path or the data in base 64
47// The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename
48VideoSchema.path('thumbnail').validate(function (value) {
e4c55619 49 return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value)
aaf61f38 50})
e4c55619 51VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
aaf61f38
C
52
53VideoSchema.methods = {
558d7c23
C
54 getFilename,
55 getJPEGName,
56 getTorrentName,
c4403b29
C
57 isOwned,
58 toFormatedJSON,
59 toRemoteJSON
aaf61f38
C
60}
61
62VideoSchema.statics = {
c4403b29
C
63 getDurationFromFile,
64 listForApi,
558d7c23 65 listByUrlAndRemoteId,
80a6c9e7 66 listByUrl,
c4403b29
C
67 listOwned,
68 listOwnedByAuthor,
69 listRemotes,
70 load,
a6375e69 71 search
aaf61f38
C
72}
73
74VideoSchema.pre('remove', function (next) {
75 const video = this
76 const tasks = []
77
78 tasks.push(
79 function (callback) {
80 removeThumbnail(video, callback)
81 }
82 )
83
84 if (video.isOwned()) {
85 tasks.push(
86 function (callback) {
87 removeFile(video, callback)
88 },
89 function (callback) {
90 removeTorrent(video, callback)
6a94a109
C
91 },
92 function (callback) {
93 removePreview(video, callback)
aaf61f38
C
94 }
95 )
96 }
97
1a42c9e2 98 parallel(tasks, next)
aaf61f38
C
99})
100
101VideoSchema.pre('save', function (next) {
102 const video = this
103 const tasks = []
104
105 if (video.isOwned()) {
558d7c23 106 const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getFilename())
e861452f 107 this.podUrl = constants.CONFIG.WEBSERVER.URL
aaf61f38
C
108
109 tasks.push(
052937db 110 // TODO: refractoring
aaf61f38 111 function (callback) {
25cad919
C
112 const options = {
113 announceList: [
3737bbaf 114 [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
25cad919
C
115 ],
116 urlList: [
558d7c23 117 constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.getFilename()
25cad919
C
118 ]
119 }
120
121 createTorrent(videoPath, options, function (err, torrent) {
052937db
C
122 if (err) return callback(err)
123
558d7c23 124 fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) {
052937db
C
125 if (err) return callback(err)
126
127 const parsedTorrent = parseTorrent(torrent)
558d7c23 128 parsedTorrent.xs = video.podUrl + constants.STATIC_PATHS.TORRENTS + video.getTorrentName()
052937db
C
129 video.magnetUri = magnet.encode(parsedTorrent)
130
131 callback(null)
132 })
133 })
aaf61f38
C
134 },
135 function (callback) {
558d7c23 136 createThumbnail(video, videoPath, callback)
6a94a109
C
137 },
138 function (callback) {
558d7c23 139 createPreview(video, videoPath, callback)
aaf61f38
C
140 }
141 )
142
558d7c23 143 parallel(tasks, next)
aaf61f38 144 } else {
558d7c23 145 generateThumbnailFromBase64(video, video.thumbnail, next)
aaf61f38
C
146 }
147})
148
149mongoose.model('Video', VideoSchema)
150
151// ------------------------------ METHODS ------------------------------
152
558d7c23
C
153function getFilename () {
154 return this._id + this.extname
155}
156
157function getJPEGName () {
158 return this._id + '.jpg'
159}
160
161function getTorrentName () {
162 return this._id + '.torrent'
163}
164
aaf61f38 165function isOwned () {
558d7c23 166 return this.remoteId === null
aaf61f38
C
167}
168
169function toFormatedJSON () {
170 const json = {
171 id: this._id,
172 name: this.name,
173 description: this.description,
174 podUrl: this.podUrl.replace(/^https?:\/\//, ''),
175 isLocal: this.isOwned(),
176 magnetUri: this.magnetUri,
177 author: this.author,
178 duration: this.duration,
179 tags: this.tags,
558d7c23 180 thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.getJPEGName(),
aaf61f38
C
181 createdDate: this.createdDate
182 }
183
184 return json
185}
186
187function toRemoteJSON (callback) {
188 const self = this
189
190 // Convert thumbnail to base64
558d7c23
C
191 const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getJPEGName())
192 fs.readFile(thumbnailPath, function (err, thumbnailData) {
aaf61f38
C
193 if (err) {
194 logger.error('Cannot read the thumbnail of the video')
195 return callback(err)
196 }
197
198 const remoteVideo = {
199 name: self.name,
200 description: self.description,
201 magnetUri: self.magnetUri,
558d7c23 202 remoteId: self._id,
aaf61f38
C
203 author: self.author,
204 duration: self.duration,
205 thumbnailBase64: new Buffer(thumbnailData).toString('base64'),
206 tags: self.tags,
207 createdDate: self.createdDate,
208 podUrl: self.podUrl
209 }
210
211 return callback(null, remoteVideo)
212 })
213}
214
215// ------------------------------ STATICS ------------------------------
216
217function getDurationFromFile (videoPath, callback) {
218 ffmpeg.ffprobe(videoPath, function (err, metadata) {
219 if (err) return callback(err)
220
221 return callback(null, Math.floor(metadata.format.duration))
222 })
223}
224
0ff21c1c 225function listForApi (start, count, sort, callback) {
aaf61f38 226 const query = {}
5c39adb7 227 return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
aaf61f38
C
228}
229
558d7c23
C
230function listByUrlAndRemoteId (fromUrl, remoteId, callback) {
231 this.find({ podUrl: fromUrl, remoteId: remoteId }, callback)
aaf61f38
C
232}
233
80a6c9e7
C
234function listByUrl (fromUrl, callback) {
235 this.find({ podUrl: fromUrl }, callback)
aaf61f38
C
236}
237
238function listOwned (callback) {
558d7c23
C
239 // If remoteId is null this is *our* video
240 this.find({ remoteId: null }, callback)
aaf61f38
C
241}
242
9bd26629 243function listOwnedByAuthor (author, callback) {
558d7c23 244 this.find({ remoteId: null, author: author }, callback)
9bd26629
C
245}
246
aaf61f38 247function listRemotes (callback) {
558d7c23 248 this.find({ remoteId: { $ne: null } }, callback)
aaf61f38
C
249}
250
251function load (id, callback) {
252 this.findById(id, callback)
253}
254
255function search (value, field, start, count, sort, callback) {
256 const query = {}
257 // Make an exact search with the magnet
258 if (field === 'magnetUri' || field === 'tags') {
259 query[field] = value
260 } else {
cf6412e8 261 query[field] = new RegExp(value, 'i')
aaf61f38
C
262 }
263
5c39adb7 264 modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
aaf61f38
C
265}
266
aaf61f38
C
267// ---------------------------------------------------------------------------
268
aaf61f38 269function removeThumbnail (video, callback) {
558d7c23 270 fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getJPEGName(), callback)
aaf61f38
C
271}
272
273function removeFile (video, callback) {
558d7c23 274 fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getFilename(), callback)
aaf61f38
C
275}
276
aaf61f38 277function removeTorrent (video, callback) {
558d7c23 278 fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback)
aaf61f38
C
279}
280
6a94a109
C
281function removePreview (video, callback) {
282 // Same name than video thumnail
283 // TODO: refractoring
558d7c23 284 fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.getJPEGName(), callback)
6a94a109
C
285}
286
558d7c23
C
287function createPreview (video, videoPath, callback) {
288 generateImage(video, videoPath, constants.CONFIG.STORAGE.PREVIEWS_DIR, callback)
6a94a109
C
289}
290
558d7c23
C
291function createThumbnail (video, videoPath, callback) {
292 generateImage(video, videoPath, constants.CONFIG.STORAGE.THUMBNAILS_DIR, constants.THUMBNAILS_SIZE, callback)
aaf61f38
C
293}
294
558d7c23
C
295function generateThumbnailFromBase64 (video, thumbnailData, callback) {
296 // Creating the thumbnail for this remote video)
297
298 const thumbnailName = video.getJPEGName()
299 const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName
300 fs.writeFile(thumbnailPath, thumbnailData, { encoding: 'base64' }, function (err) {
aaf61f38
C
301 if (err) return callback(err)
302
558d7c23
C
303 return callback(null, thumbnailName)
304 })
305}
306
307function generateImage (video, videoPath, folder, size, callback) {
308 const filename = video.getJPEGName()
309 const options = {
310 filename,
311 count: 1,
312 folder
313 }
aaf61f38 314
558d7c23
C
315 if (!callback) {
316 callback = size
317 } else {
318 options.size = size
319 }
320
321 ffmpeg(videoPath)
322 .on('error', callback)
323 .on('end', function () {
324 callback(null, filename)
aaf61f38 325 })
558d7c23 326 .thumbnail(options)
aaf61f38 327}