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()
videosRouter.use('/', abuseVideoRouter)
videosRouter.use('/', blacklistRouter)
videosRouter.use('/', rateVideoRouter)
+videosRouter.use('/', videoChannelRouter)
videosRouter.get('/categories', listVideoCategories)
videosRouter.get('/licences', listVideoLicences)
videosRouter.delete('/:id',
authenticate,
videosRemoveValidator,
- removeVideo
+ removeVideoRetryWrapper
)
videosRouter.get('/search/:value',
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,
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))
.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(
}
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 }
retryTransactionWrapper(updateVideo, options)
.then(() => {
- // TODO : include Location of the new video -> 201
return res.type('json').status(204).end()
})
.catch(err => next(err))
}
// 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) {
.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) {