]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/index.ts
Add video channels
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / index.ts
index 7a9cd9d37486b03a01512b2a19376181c1f22a49..ec855ee8e12597a27707f508f9531d600d0cb24d 100644 (file)
@@ -37,14 +37,16 @@ import {
   retryTransactionWrapper,
   generateRandomString,
   getFormattedObjects,
-  renamePromise
+  renamePromise,
+  getVideoFileHeight
 } from '../../../helpers'
-import { TagInstance } from '../../../models'
+import { TagInstance, VideoInstance } from '../../../models'
 import { VideoCreate, VideoUpdate } from '../../../../shared'
 
 import { abuseVideoRouter } from './abuse'
 import { blacklistRouter } from './blacklist'
 import { rateVideoRouter } from './rate'
+import { videoChannelRouter } from './channel'
 
 const videosRouter = express.Router()
 
@@ -75,6 +77,7 @@ const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCo
 videosRouter.use('/', abuseVideoRouter)
 videosRouter.use('/', blacklistRouter)
 videosRouter.use('/', rateVideoRouter)
+videosRouter.use('/', videoChannelRouter)
 
 videosRouter.get('/categories', listVideoCategories)
 videosRouter.get('/licences', listVideoLicences)
@@ -106,7 +109,7 @@ videosRouter.get('/:id',
 videosRouter.delete('/:id',
   authenticate,
   videosRemoveValidator,
-  removeVideo
+  removeVideoRetryWrapper
 )
 
 videosRouter.get('/search/:value',
@@ -160,21 +163,13 @@ function addVideo (req: express.Request, res: express.Response, videoPhysicalFil
   let videoUUID = ''
 
   return db.sequelize.transaction(t => {
-    const user = res.locals.oauth.token.User
+    let p: Promise<TagInstance[]>
 
-    const name = user.username
-    // null because it is OUR pod
-    const podId = null
-    const userId = user.id
+    if (!videoInfo.tags) p = Promise.resolve(undefined)
+    else p = db.Tag.findOrCreateTags(videoInfo.tags, t)
 
-    return db.Author.findOrCreateAuthor(name, podId, userId, t)
-      .then(author => {
-        const tags = videoInfo.tags
-        if (!tags) return { author, tagInstances: undefined }
-
-        return db.Tag.findOrCreateTags(tags, t).then(tagInstances => ({ author, tagInstances }))
-      })
-      .then(({ author, tagInstances }) => {
+    return p
+      .then(tagInstances => {
         const videoData = {
           name: videoInfo.name,
           remote: false,
@@ -185,23 +180,28 @@ function addVideo (req: express.Request, res: express.Response, videoPhysicalFil
           nsfw: videoInfo.nsfw,
           description: videoInfo.description,
           duration: videoPhysicalFile['duration'], // duration was added by a previous middleware
-          authorId: author.id
+          channelId: res.locals.videoChannel.id
         }
 
         const video = db.Video.build(videoData)
-        return { author, tagInstances, video }
+        return { tagInstances, video }
       })
-      .then(({ author, tagInstances, video }) => {
+      .then(({ tagInstances, video }) => {
+        const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename)
+        return getVideoFileHeight(videoFilePath)
+          .then(height => ({ tagInstances, video, videoFileHeight: height }))
+      })
+      .then(({ tagInstances, video, videoFileHeight }) => {
         const videoFileData = {
           extname: extname(videoPhysicalFile.filename),
-          resolution: 0, // TODO: improve readability,
+          resolution: videoFileHeight,
           size: videoPhysicalFile.size
         }
 
         const videoFile = db.VideoFile.build(videoFileData)
-        return { author, tagInstances, video, videoFile }
+        return { tagInstances, video, videoFile }
       })
-      .then(({ author, tagInstances, video, videoFile }) => {
+      .then(({ tagInstances, video, videoFile }) => {
         const videoDir = CONFIG.STORAGE.VIDEOS_DIR
         const source = join(videoDir, videoPhysicalFile.filename)
         const destination = join(videoDir, video.getVideoFilename(videoFile))
@@ -210,10 +210,10 @@ function addVideo (req: express.Request, res: express.Response, videoPhysicalFil
           .then(() => {
             // This is important in case if there is another attempt in the retry process
             videoPhysicalFile.filename = video.getVideoFilename(videoFile)
-            return { author, tagInstances, video, videoFile }
+            return { tagInstances, video, videoFile }
           })
       })
-      .then(({ author, tagInstances, video, videoFile }) => {
+      .then(({ tagInstances, video, videoFile }) => {
         const tasks = []
 
         tasks.push(
@@ -229,19 +229,19 @@ function addVideo (req: express.Request, res: express.Response, videoPhysicalFil
           }
 
           tasks.push(
-            JobScheduler.Instance.createJob(t, 'videoTranscoder', dataInput)
+            JobScheduler.Instance.createJob(t, 'videoFileOptimizer', dataInput)
           )
         }
 
-        return Promise.all(tasks).then(() => ({ author, tagInstances, video, videoFile }))
+        return Promise.all(tasks).then(() => ({ tagInstances, video, videoFile }))
       })
-      .then(({ author, tagInstances, video, videoFile }) => {
+      .then(({ tagInstances, video, videoFile }) => {
         const options = { transaction: t }
 
         return video.save(options)
           .then(videoCreated => {
-            // Do not forget to add Author information to the created video
-            videoCreated.Author = author
+            // Do not forget to add video channel information to the created video
+            videoCreated.VideoChannel = res.locals.videoChannel
             videoUUID = videoCreated.uuid
 
             return { tagInstances, video: videoCreated, videoFile }
@@ -291,7 +291,6 @@ function updateVideoRetryWrapper (req: express.Request, res: express.Response, n
 
   retryTransactionWrapper(updateVideo, options)
     .then(() => {
-      // TODO : include Location of the new video -> 201
       return res.type('json').status(204).end()
     })
     .catch(err => next(err))
@@ -387,7 +386,7 @@ function getVideo (req: express.Request, res: express.Response) {
   }
 
   // Do not wait the view system
-  res.json(videoInstance.toFormattedJSON())
+  res.json(videoInstance.toFormattedDetailsJSON())
 }
 
 function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
@@ -396,18 +395,32 @@ function listVideos (req: express.Request, res: express.Response, next: express.
     .catch(err => next(err))
 }
 
-function removeVideo (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const videoInstance = res.locals.video
+function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
+  const options = {
+    arguments: [ req, res ],
+    errorMessage: 'Cannot remove the video with many retries.'
+  }
 
-  videoInstance.destroy()
+  retryTransactionWrapper(removeVideo, options)
     .then(() => {
-      logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
-      res.type('json').status(204).end()
-    })
-    .catch(err => {
-      logger.error('Errors when removed the video.', err)
-      return next(err)
+      return res.type('json').status(204).end()
     })
+    .catch(err => next(err))
+}
+
+function removeVideo (req: express.Request, res: express.Response) {
+  const videoInstance: VideoInstance = res.locals.video
+
+  return db.sequelize.transaction(t => {
+    return videoInstance.destroy({ transaction: t })
+  })
+  .then(() => {
+    logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
+  })
+  .catch(err => {
+    logger.error('Errors when removed the video.', err)
+    throw err
+  })
 }
 
 function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {