- 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 = videoInfo.tags
- if (!tags) return { author, tagInstances: undefined }
-
- return db.Tag.findOrCreateTags(tags, t).then(tagInstances => ({ author, tagInstances }))
- })
- .then(({ author, tagInstances }) => {
- const videoData = {
- name: videoInfo.name,
- remote: false,
- extname: extname(videoPhysicalFile.filename),
- category: videoInfo.category,
- licence: videoInfo.licence,
- language: videoInfo.language,
- nsfw: videoInfo.nsfw,
- description: videoInfo.description,
- duration: videoPhysicalFile['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 videoFileData = {
- extname: extname(videoPhysicalFile.filename),
- resolution: 0, // TODO: improve readability,
- size: videoPhysicalFile.size
- }
-
- const videoFile = db.VideoFile.build(videoFileData)
- return { author, tagInstances, video, videoFile }
- })
- .then(({ author, tagInstances, video, videoFile }) => {
- const videoDir = CONFIG.STORAGE.VIDEOS_DIR
- const source = join(videoDir, videoPhysicalFile.filename)
- const destination = join(videoDir, video.getVideoFilename(videoFile))
-
- return renamePromise(source, destination)
- .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 }
- })
- })
- .then(({ author, tagInstances, video, videoFile }) => {
- const tasks = []
-
- tasks.push(
- video.createTorrentAndSetInfoHash(videoFile),
- video.createThumbnail(videoFile),
- video.createPreview(videoFile)
- )
-
- if (CONFIG.TRANSCODING.ENABLED === true) {
- // Put uuid because we don't have id auto incremented for now
- const dataInput = {
- videoUUID: video.uuid
- }
-
- tasks.push(
- JobScheduler.Instance.createJob(t, 'videoTranscoder', dataInput)
- )
- }
-
- return Promise.all(tasks).then(() => ({ author, tagInstances, video, videoFile }))
- })
- .then(({ author, 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
-
- return { tagInstances, video: videoCreated, videoFile }
- })
- })
- .then(({ tagInstances, video, videoFile }) => {
- const options = { transaction: t }
- videoFile.videoId = video.id
-
- return videoFile.save(options)
- .then(() => video.VideoFiles = [ videoFile ])
- .then(() => ({ tagInstances, video }))
- })
- .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 video file 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)
- })
- })
- })
- .then(() => logger.info('Video with name %s created.', videoInfo.name))
- .catch((err: Error) => {
- logger.debug('Cannot insert the video.', err)
- throw err
+ // Prepare data so we don't block the transaction
+ const videoData = {
+ name: videoInfo.name,
+ remote: false,
+ extname: extname(videoPhysicalFile.filename),
+ category: videoInfo.category,
+ licence: videoInfo.licence,
+ language: videoInfo.language,
+ nsfw: videoInfo.nsfw,
+ description: videoInfo.description,
+ privacy: videoInfo.privacy,
+ duration: videoPhysicalFile['duration'], // duration was added by a previous middleware
+ channelId: res.locals.videoChannel.id
+ }
+ const video = new VideoModel(videoData)
+ video.url = getVideoActivityPubUrl(video)
+
+ const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename)
+ const videoFileHeight = await getVideoFileHeight(videoFilePath)
+
+ const videoFileData = {
+ extname: extname(videoPhysicalFile.filename),
+ resolution: videoFileHeight,
+ size: videoPhysicalFile.size
+ }
+ const videoFile = new VideoFileModel(videoFileData)
+ const videoDir = CONFIG.STORAGE.VIDEOS_DIR
+ const source = join(videoDir, videoPhysicalFile.filename)
+ const destination = join(videoDir, video.getVideoFilename(videoFile))
+
+ await renamePromise(source, destination)
+ // This is important in case if there is another attempt in the retry process
+ videoPhysicalFile.filename = video.getVideoFilename(videoFile)
+
+ const tasks = []
+
+ tasks.push(
+ video.createTorrentAndSetInfoHash(videoFile),
+ video.createThumbnail(videoFile),
+ video.createPreview(videoFile)
+ )
+ await Promise.all(tasks)
+
+ return sequelizeTypescript.transaction(async t => {
+ const sequelizeOptions = { transaction: t }
+
+ if (CONFIG.TRANSCODING.ENABLED === true) {
+ // Put uuid because we don't have id auto incremented for now
+ const dataInput = {
+ videoUUID: video.uuid
+ }
+
+ await transcodingJobScheduler.createJob(t, 'videoFileOptimizer', dataInput)
+ }
+
+ const videoCreated = await video.save(sequelizeOptions)
+ // Do not forget to add video channel information to the created video
+ videoCreated.VideoChannel = res.locals.videoChannel
+
+ videoFile.videoId = video.id
+ await videoFile.save(sequelizeOptions)
+
+ video.VideoFiles = [ videoFile ]
+
+ if (videoInfo.tags) {
+ const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t)
+
+ await video.$set('Tags', tagInstances, sequelizeOptions)
+ video.Tags = tagInstances
+ }
+
+ // Let transcoding job send the video to friends because the video file extension might change
+ if (CONFIG.TRANSCODING.ENABLED === true) return videoCreated
+ // Don't send video to remote servers, it is private
+ if (video.privacy === VideoPrivacy.PRIVATE) return videoCreated
+
+ await sendAddVideo(video, t)
+ await shareVideoByServer(video, t)
+
+ logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid)
+
+ return videoCreated