aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/videos')
-rw-r--r--server/controllers/api/videos/index.ts65
1 files changed, 53 insertions, 12 deletions
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index 815881df3..d71a132ed 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -1,7 +1,7 @@
1import * as express from 'express' 1import * as express from 'express'
2import * as Promise from 'bluebird' 2import * as Promise from 'bluebird'
3import * as multer from 'multer' 3import * as multer from 'multer'
4import * as path from 'path' 4import { extname, join } from 'path'
5 5
6import { database as db } from '../../../initializers/database' 6import { database as db } from '../../../initializers/database'
7import { 7import {
@@ -16,7 +16,8 @@ import {
16 addEventToRemoteVideo, 16 addEventToRemoteVideo,
17 quickAndDirtyUpdateVideoToFriends, 17 quickAndDirtyUpdateVideoToFriends,
18 addVideoToFriends, 18 addVideoToFriends,
19 updateVideoToFriends 19 updateVideoToFriends,
20 JobScheduler
20} from '../../../lib' 21} from '../../../lib'
21import { 22import {
22 authenticate, 23 authenticate,
@@ -155,7 +156,7 @@ function addVideoRetryWrapper (req: express.Request, res: express.Response, next
155 .catch(err => next(err)) 156 .catch(err => next(err))
156} 157}
157 158
158function addVideo (req: express.Request, res: express.Response, videoFile: Express.Multer.File) { 159function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) {
159 const videoInfos: VideoCreate = req.body 160 const videoInfos: VideoCreate = req.body
160 161
161 return db.sequelize.transaction(t => { 162 return db.sequelize.transaction(t => {
@@ -177,13 +178,13 @@ function addVideo (req: express.Request, res: express.Response, videoFile: Expre
177 const videoData = { 178 const videoData = {
178 name: videoInfos.name, 179 name: videoInfos.name,
179 remote: false, 180 remote: false,
180 extname: path.extname(videoFile.filename), 181 extname: extname(videoPhysicalFile.filename),
181 category: videoInfos.category, 182 category: videoInfos.category,
182 licence: videoInfos.licence, 183 licence: videoInfos.licence,
183 language: videoInfos.language, 184 language: videoInfos.language,
184 nsfw: videoInfos.nsfw, 185 nsfw: videoInfos.nsfw,
185 description: videoInfos.description, 186 description: videoInfos.description,
186 duration: videoFile['duration'], // duration was added by a previous middleware 187 duration: videoPhysicalFile['duration'], // duration was added by a previous middleware
187 authorId: author.id 188 authorId: author.id
188 } 189 }
189 190
@@ -191,18 +192,50 @@ function addVideo (req: express.Request, res: express.Response, videoFile: Expre
191 return { author, tagInstances, video } 192 return { author, tagInstances, video }
192 }) 193 })
193 .then(({ author, tagInstances, video }) => { 194 .then(({ author, tagInstances, video }) => {
195 const videoFileData = {
196 extname: extname(videoPhysicalFile.filename),
197 resolution: 0, // TODO: improve readability,
198 size: videoPhysicalFile.size
199 }
200
201 const videoFile = db.VideoFile.build(videoFileData)
202 return { author, tagInstances, video, videoFile }
203 })
204 .then(({ author, tagInstances, video, videoFile }) => {
194 const videoDir = CONFIG.STORAGE.VIDEOS_DIR 205 const videoDir = CONFIG.STORAGE.VIDEOS_DIR
195 const source = path.join(videoDir, videoFile.filename) 206 const source = join(videoDir, videoPhysicalFile.filename)
196 const destination = path.join(videoDir, video.getVideoFilename()) 207 const destination = join(videoDir, video.getVideoFilename(videoFile))
197 208
198 return renamePromise(source, destination) 209 return renamePromise(source, destination)
199 .then(() => { 210 .then(() => {
200 // This is important in case if there is another attempt in the retry process 211 // This is important in case if there is another attempt in the retry process
201 videoFile.filename = video.getVideoFilename() 212 videoPhysicalFile.filename = video.getVideoFilename(videoFile)
202 return { author, tagInstances, video } 213 return { author, tagInstances, video, videoFile }
203 }) 214 })
204 }) 215 })
205 .then(({ author, tagInstances, video }) => { 216 .then(({ author, tagInstances, video, videoFile }) => {
217 const tasks = []
218
219 tasks.push(
220 video.createTorrentAndSetInfoHash(videoFile),
221 video.createThumbnail(videoFile),
222 video.createPreview(videoFile)
223 )
224
225 if (CONFIG.TRANSCODING.ENABLED === true) {
226 // Put uuid because we don't have id auto incremented for now
227 const dataInput = {
228 videoUUID: video.uuid
229 }
230
231 tasks.push(
232 JobScheduler.Instance.createJob(t, 'videoTranscoder', dataInput)
233 )
234 }
235
236 return Promise.all(tasks).then(() => ({ author, tagInstances, video, videoFile }))
237 })
238 .then(({ author, tagInstances, video, videoFile }) => {
206 const options = { transaction: t } 239 const options = { transaction: t }
207 240
208 return video.save(options) 241 return video.save(options)
@@ -210,9 +243,17 @@ function addVideo (req: express.Request, res: express.Response, videoFile: Expre
210 // Do not forget to add Author informations to the created video 243 // Do not forget to add Author informations to the created video
211 videoCreated.Author = author 244 videoCreated.Author = author
212 245
213 return { tagInstances, video: videoCreated } 246 return { tagInstances, video: videoCreated, videoFile }
214 }) 247 })
215 }) 248 })
249 .then(({ tagInstances, video, videoFile }) => {
250 const options = { transaction: t }
251 videoFile.videoId = video.id
252
253 return videoFile.save(options)
254 .then(() => video.VideoFiles = [ videoFile ])
255 .then(() => ({ tagInstances, video }))
256 })
216 .then(({ tagInstances, video }) => { 257 .then(({ tagInstances, video }) => {
217 if (!tagInstances) return video 258 if (!tagInstances) return video
218 259
@@ -236,7 +277,7 @@ function addVideo (req: express.Request, res: express.Response, videoFile: Expre
236 }) 277 })
237 .then(() => logger.info('Video with name %s created.', videoInfos.name)) 278 .then(() => logger.info('Video with name %s created.', videoInfos.name))
238 .catch((err: Error) => { 279 .catch((err: Error) => {
239 logger.debug('Cannot insert the video.', { error: err.stack }) 280 logger.debug('Cannot insert the video.', err)
240 throw err 281 throw err
241 }) 282 })
242} 283}