- 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 informations 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 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)
- })
- })
- })
- .then(() => logger.info('Video with name %s created.', videoInfos.name))
- .catch((err: Error) => {
- logger.debug('Cannot insert the video.', err)
- throw err
+ // Process preview or create it from the video
+ const previewField = req.files['previewfile']
+ if (previewField) {
+ const previewPhysicalFile = previewField[0]
+ await processImage(previewPhysicalFile, join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()), PREVIEWS_SIZE)
+ } else {
+ await video.createPreview(videoFile)
+ }
+
+ await video.createTorrentAndSetInfoHash(videoFile)
+
+ const videoCreated = await sequelizeTypescript.transaction(async t => {
+ const sequelizeOptions = { transaction: t }
+
+ 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 sendCreateVideo(video, t)
+ await shareVideoByServerAndChannel(video, t)
+
+ logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid)
+
+ return videoCreated