- 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)
- })
- })
- })
- .then(() => logger.info('Video with name %s created.', videoInfos.name))
- .catch((err: Error) => {
- logger.debug('Cannot insert the video.', { error: err.stack })
- throw err
+ const { videoFileResolution } = await getVideoFileResolution(videoPhysicalFile.path)
+
+ const videoFileData = {
+ extname: extname(videoPhysicalFile.filename),
+ resolution: videoFileResolution,
+ size: videoPhysicalFile.size
+ }
+ const videoFile = new VideoFileModel(videoFileData)
+ const videoDir = CONFIG.STORAGE.VIDEOS_DIR
+ const destination = join(videoDir, video.getVideoFilename(videoFile))
+
+ await renamePromise(videoPhysicalFile.path, destination)
+ // This is important in case if there is another attempt in the retry process
+ videoPhysicalFile.filename = video.getVideoFilename(videoFile)
+ videoPhysicalFile.path = destination
+
+ // Process thumbnail or create it from the video
+ const thumbnailField = req.files['thumbnailfile']
+ if (thumbnailField) {
+ const thumbnailPhysicalFile = thumbnailField[0]
+ await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()), THUMBNAILS_SIZE)
+ } else {
+ await video.createThumbnail(videoFile)
+ }
+
+ // 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 !== undefined) {
+ 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