aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-08-25 11:36:23 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-08-25 11:36:23 +0200
commit93e1258c7cbc0d1235ca6d2a1f7c1875985328b8 (patch)
treeb0a1f77af7ab54dc5f58f569fcd1e9d84b04c533 /server/controllers/api
parent69f224587e99d56008e1fa129d0641840a486620 (diff)
downloadPeerTube-93e1258c7cbc0d1235ca6d2a1f7c1875985328b8.tar.gz
PeerTube-93e1258c7cbc0d1235ca6d2a1f7c1875985328b8.tar.zst
PeerTube-93e1258c7cbc0d1235ca6d2a1f7c1875985328b8.zip
Move video file metadata in their own table
Will be used for user video quotas and multiple video resolutions
Diffstat (limited to 'server/controllers/api')
-rw-r--r--server/controllers/api/remote/videos.ts42
-rw-r--r--server/controllers/api/videos/index.ts65
2 files changed, 93 insertions, 14 deletions
diff --git a/server/controllers/api/remote/videos.ts b/server/controllers/api/remote/videos.ts
index 30771d8c4..e7edff606 100644
--- a/server/controllers/api/remote/videos.ts
+++ b/server/controllers/api/remote/videos.ts
@@ -258,8 +258,6 @@ function addRemoteVideo (videoToCreateData: RemoteVideoCreateData, fromPod: PodI
258 const videoData = { 258 const videoData = {
259 name: videoToCreateData.name, 259 name: videoToCreateData.name,
260 uuid: videoToCreateData.uuid, 260 uuid: videoToCreateData.uuid,
261 extname: videoToCreateData.extname,
262 infoHash: videoToCreateData.infoHash,
263 category: videoToCreateData.category, 261 category: videoToCreateData.category,
264 licence: videoToCreateData.licence, 262 licence: videoToCreateData.licence,
265 language: videoToCreateData.language, 263 language: videoToCreateData.language,
@@ -290,6 +288,26 @@ function addRemoteVideo (videoToCreateData: RemoteVideoCreateData, fromPod: PodI
290 return video.save(options).then(videoCreated => ({ tagInstances, videoCreated })) 288 return video.save(options).then(videoCreated => ({ tagInstances, videoCreated }))
291 }) 289 })
292 .then(({ tagInstances, videoCreated }) => { 290 .then(({ tagInstances, videoCreated }) => {
291 const tasks = []
292 const options = {
293 transaction: t
294 }
295
296 videoToCreateData.files.forEach(fileData => {
297 const videoFileInstance = db.VideoFile.build({
298 extname: fileData.extname,
299 infoHash: fileData.infoHash,
300 resolution: fileData.resolution,
301 size: fileData.size,
302 videoId: videoCreated.id
303 })
304
305 tasks.push(videoFileInstance.save(options))
306 })
307
308 return Promise.all(tasks).then(() => ({ tagInstances, videoCreated }))
309 })
310 .then(({ tagInstances, videoCreated }) => {
293 const options = { 311 const options = {
294 transaction: t 312 transaction: t
295 } 313 }
@@ -344,6 +362,26 @@ function updateRemoteVideo (videoAttributesToUpdate: RemoteVideoUpdateData, from
344 362
345 return videoInstance.save(options).then(() => ({ videoInstance, tagInstances })) 363 return videoInstance.save(options).then(() => ({ videoInstance, tagInstances }))
346 }) 364 })
365 .then(({ tagInstances, videoInstance }) => {
366 const tasks = []
367 const options = {
368 transaction: t
369 }
370
371 videoAttributesToUpdate.files.forEach(fileData => {
372 const videoFileInstance = db.VideoFile.build({
373 extname: fileData.extname,
374 infoHash: fileData.infoHash,
375 resolution: fileData.resolution,
376 size: fileData.size,
377 videoId: videoInstance.id
378 })
379
380 tasks.push(videoFileInstance.save(options))
381 })
382
383 return Promise.all(tasks).then(() => ({ tagInstances, videoInstance }))
384 })
347 .then(({ videoInstance, tagInstances }) => { 385 .then(({ videoInstance, tagInstances }) => {
348 const options = { transaction: t } 386 const options = { transaction: t }
349 387
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index 815881df3..d71a132ed 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -1,7 +1,7 @@
1import * as express from 'express' 1import * as express from 'express'
2import * as Promise from 'bluebird' 2import * as Promise from 'bluebird'
3import * as multer from 'multer' 3import * as multer from 'multer'
4import * as path from 'path' 4import { extname, join } from 'path'
5 5
6import { database as db } from '../../../initializers/database' 6import { database as db } from '../../../initializers/database'
7import { 7import {
@@ -16,7 +16,8 @@ import {
16 addEventToRemoteVideo, 16 addEventToRemoteVideo,
17 quickAndDirtyUpdateVideoToFriends, 17 quickAndDirtyUpdateVideoToFriends,
18 addVideoToFriends, 18 addVideoToFriends,
19 updateVideoToFriends 19 updateVideoToFriends,
20 JobScheduler
20} from '../../../lib' 21} from '../../../lib'
21import { 22import {
22 authenticate, 23 authenticate,
@@ -155,7 +156,7 @@ function addVideoRetryWrapper (req: express.Request, res: express.Response, next
155 .catch(err => next(err)) 156 .catch(err => next(err))
156} 157}
157 158
158function addVideo (req: express.Request, res: express.Response, videoFile: Express.Multer.File) { 159function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) {
159 const videoInfos: VideoCreate = req.body 160 const videoInfos: VideoCreate = req.body
160 161
161 return db.sequelize.transaction(t => { 162 return db.sequelize.transaction(t => {
@@ -177,13 +178,13 @@ function addVideo (req: express.Request, res: express.Response, videoFile: Expre
177 const videoData = { 178 const videoData = {
178 name: videoInfos.name, 179 name: videoInfos.name,
179 remote: false, 180 remote: false,
180 extname: path.extname(videoFile.filename), 181 extname: extname(videoPhysicalFile.filename),
181 category: videoInfos.category, 182 category: videoInfos.category,
182 licence: videoInfos.licence, 183 licence: videoInfos.licence,
183 language: videoInfos.language, 184 language: videoInfos.language,
184 nsfw: videoInfos.nsfw, 185 nsfw: videoInfos.nsfw,
185 description: videoInfos.description, 186 description: videoInfos.description,
186 duration: videoFile['duration'], // duration was added by a previous middleware 187 duration: videoPhysicalFile['duration'], // duration was added by a previous middleware
187 authorId: author.id 188 authorId: author.id
188 } 189 }
189 190
@@ -191,18 +192,50 @@ function addVideo (req: express.Request, res: express.Response, videoFile: Expre
191 return { author, tagInstances, video } 192 return { author, tagInstances, video }
192 }) 193 })
193 .then(({ author, tagInstances, video }) => { 194 .then(({ author, tagInstances, video }) => {
195 const videoFileData = {
196 extname: extname(videoPhysicalFile.filename),
197 resolution: 0, // TODO: improve readability,
198 size: videoPhysicalFile.size
199 }
200
201 const videoFile = db.VideoFile.build(videoFileData)
202 return { author, tagInstances, video, videoFile }
203 })
204 .then(({ author, tagInstances, video, videoFile }) => {
194 const videoDir = CONFIG.STORAGE.VIDEOS_DIR 205 const videoDir = CONFIG.STORAGE.VIDEOS_DIR
195 const source = path.join(videoDir, videoFile.filename) 206 const source = join(videoDir, videoPhysicalFile.filename)
196 const destination = path.join(videoDir, video.getVideoFilename()) 207 const destination = join(videoDir, video.getVideoFilename(videoFile))
197 208
198 return renamePromise(source, destination) 209 return renamePromise(source, destination)
199 .then(() => { 210 .then(() => {
200 // This is important in case if there is another attempt in the retry process 211 // This is important in case if there is another attempt in the retry process
201 videoFile.filename = video.getVideoFilename() 212 videoPhysicalFile.filename = video.getVideoFilename(videoFile)
202 return { author, tagInstances, video } 213 return { author, tagInstances, video, videoFile }
203 }) 214 })
204 }) 215 })
205 .then(({ author, tagInstances, video }) => { 216 .then(({ author, tagInstances, video, videoFile }) => {
217 const tasks = []
218
219 tasks.push(
220 video.createTorrentAndSetInfoHash(videoFile),
221 video.createThumbnail(videoFile),
222 video.createPreview(videoFile)
223 )
224
225 if (CONFIG.TRANSCODING.ENABLED === true) {
226 // Put uuid because we don't have id auto incremented for now
227 const dataInput = {
228 videoUUID: video.uuid
229 }
230
231 tasks.push(
232 JobScheduler.Instance.createJob(t, 'videoTranscoder', dataInput)
233 )
234 }
235
236 return Promise.all(tasks).then(() => ({ author, tagInstances, video, videoFile }))
237 })
238 .then(({ author, tagInstances, video, videoFile }) => {
206 const options = { transaction: t } 239 const options = { transaction: t }
207 240
208 return video.save(options) 241 return video.save(options)
@@ -210,9 +243,17 @@ function addVideo (req: express.Request, res: express.Response, videoFile: Expre
210 // Do not forget to add Author informations to the created video 243 // Do not forget to add Author informations to the created video
211 videoCreated.Author = author 244 videoCreated.Author = author
212 245
213 return { tagInstances, video: videoCreated } 246 return { tagInstances, video: videoCreated, videoFile }
214 }) 247 })
215 }) 248 })
249 .then(({ tagInstances, video, videoFile }) => {
250 const options = { transaction: t }
251 videoFile.videoId = video.id
252
253 return videoFile.save(options)
254 .then(() => video.VideoFiles = [ videoFile ])
255 .then(() => ({ tagInstances, video }))
256 })
216 .then(({ tagInstances, video }) => { 257 .then(({ tagInstances, video }) => {
217 if (!tagInstances) return video 258 if (!tagInstances) return video
218 259
@@ -236,7 +277,7 @@ function addVideo (req: express.Request, res: express.Response, videoFile: Expre
236 }) 277 })
237 .then(() => logger.info('Video with name %s created.', videoInfos.name)) 278 .then(() => logger.info('Video with name %s created.', videoInfos.name))
238 .catch((err: Error) => { 279 .catch((err: Error) => {
239 logger.debug('Cannot insert the video.', { error: err.stack }) 280 logger.debug('Cannot insert the video.', err)
240 throw err 281 throw err
241 }) 282 })
242} 283}