-function addVideo (req: express.Request, res: express.Response, videoFile: Express.Multer.File) {
- const videoInfos = req.body
-
- return db.sequelize.transaction(t => {
- const user = res.locals.oauth.token.User
-
- const name = user.username
- // null because it is OUR pod
- const podId = null
- const userId = user.id
-
- return db.Author.findOrCreateAuthor(name, podId, userId, t)
- .then(author => {
- const tags = videoInfos.tags
- if (!tags) return { author, tagInstances: undefined }
-
- return db.Tag.findOrCreateTags(tags, t).then(tagInstances => ({ author, tagInstances }))
- })
- .then(({ author, tagInstances }) => {
- const videoData = {
- name: videoInfos.name,
- remoteId: null,
- extname: path.extname(videoFile.filename),
- category: videoInfos.category,
- licence: videoInfos.licence,
- language: videoInfos.language,
- nsfw: videoInfos.nsfw,
- description: videoInfos.description,
- duration: videoFile['duration'], // duration was added by a previous middleware
- authorId: author.id
- }
-
- const video = db.Video.build(videoData)
- return { author, tagInstances, video }
- })
- .then(({ author, tagInstances, video }) => {
- const videoDir = CONFIG.STORAGE.VIDEOS_DIR
- const source = path.join(videoDir, videoFile.filename)
- const destination = path.join(videoDir, video.getVideoFilename())
-
- return renamePromise(source, destination)
- .then(() => {
- // This is important in case if there is another attempt in the retry process
- videoFile.filename = video.getVideoFilename()
- return { author, tagInstances, video }
- })
- })
- .then(({ author, tagInstances, video }) => {
- const options = { transaction: t }
-
- return video.save(options)
- .then(videoCreated => {
- // Do not forget to add Author informations to the created video
- videoCreated.Author = author
-
- return { tagInstances, video: videoCreated }
- })
- })
- .then(({ tagInstances, video }) => {
- if (!tagInstances) return video
-
- const options = { transaction: t }
- return video.setTags(tagInstances, options)
- .then(() => {
- video.Tags = tagInstances
- return video
- })
- })
- .then(video => {
- // Let transcoding job send the video to friends because the videofile extension might change
- if (CONFIG.TRANSCODING.ENABLED === true) return undefined
-
- return video.toAddRemoteJSON()
- .then(remoteVideo => {
- // Now we'll add the video's meta data to our friends
- return addVideoToFriends(remoteVideo, t)
- })
- })