aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/index.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-12 17:53:50 +0100
committerChocobozzz <me@florianbigard.com>2017-12-13 16:50:33 +0100
commit3fd3ab2d34d512b160a5e6084d7609be7b4f4452 (patch)
treee5ca358287fca6ecacce83defcf23af1e8e9f419 /server/controllers/api/videos/index.ts
parentc893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff)
downloadPeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz
PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst
PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip
Move models to typescript-sequelize
Diffstat (limited to 'server/controllers/api/videos/index.ts')
-rw-r--r--server/controllers/api/videos/index.ts53
1 files changed, 29 insertions, 24 deletions
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index 63de662a7..91ab8c66a 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -12,16 +12,19 @@ import {
12 retryTransactionWrapper 12 retryTransactionWrapper
13} from '../../../helpers' 13} from '../../../helpers'
14import { getServerAccount } from '../../../helpers/utils' 14import { getServerAccount } from '../../../helpers/utils'
15import { CONFIG, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_MIMETYPE_EXT, VIDEO_PRIVACIES } from '../../../initializers' 15import {
16import { database as db } from '../../../initializers/database' 16 CONFIG,
17import { sendAddVideo } from '../../../lib/activitypub/send/send-add' 17 sequelizeTypescript,
18import { sendCreateViewToOrigin } from '../../../lib/activitypub/send/send-create' 18 VIDEO_CATEGORIES,
19import { sendUpdateVideo } from '../../../lib/activitypub/send/send-update' 19 VIDEO_LANGUAGES,
20import { shareVideoByServer } from '../../../lib/activitypub/share' 20 VIDEO_LICENCES,
21import { getVideoActivityPubUrl } from '../../../lib/activitypub/url' 21 VIDEO_MIMETYPE_EXT,
22import { fetchRemoteVideoDescription } from '../../../lib/activitypub/videos' 22 VIDEO_PRIVACIES
23} from '../../../initializers'
24import { fetchRemoteVideoDescription, getVideoActivityPubUrl, shareVideoByServer } from '../../../lib/activitypub'
25import { sendAddVideo, sendCreateViewToOrigin, sendUpdateVideo } from '../../../lib/activitypub/send'
23import { sendCreateViewToVideoFollowers } from '../../../lib/index' 26import { sendCreateViewToVideoFollowers } from '../../../lib/index'
24import { transcodingJobScheduler } from '../../../lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler' 27import { transcodingJobScheduler } from '../../../lib/jobs/transcoding-job-scheduler'
25import { 28import {
26 asyncMiddleware, 29 asyncMiddleware,
27 authenticate, 30 authenticate,
@@ -35,7 +38,9 @@ import {
35 videosSortValidator, 38 videosSortValidator,
36 videosUpdateValidator 39 videosUpdateValidator
37} from '../../../middlewares' 40} from '../../../middlewares'
38import { VideoInstance } from '../../../models' 41import { TagModel } from '../../../models/video/tag'
42import { VideoModel } from '../../../models/video/video'
43import { VideoFileModel } from '../../../models/video/video-file'
39import { abuseVideoRouter } from './abuse' 44import { abuseVideoRouter } from './abuse'
40import { blacklistRouter } from './blacklist' 45import { blacklistRouter } from './blacklist'
41import { videoChannelRouter } from './channel' 46import { videoChannelRouter } from './channel'
@@ -99,7 +104,7 @@ videosRouter.put('/:id',
99videosRouter.post('/upload', 104videosRouter.post('/upload',
100 authenticate, 105 authenticate,
101 reqFiles, 106 reqFiles,
102 videosAddValidator, 107 asyncMiddleware(videosAddValidator),
103 asyncMiddleware(addVideoRetryWrapper) 108 asyncMiddleware(addVideoRetryWrapper)
104) 109)
105 110
@@ -181,7 +186,7 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi
181 duration: videoPhysicalFile['duration'], // duration was added by a previous middleware 186 duration: videoPhysicalFile['duration'], // duration was added by a previous middleware
182 channelId: res.locals.videoChannel.id 187 channelId: res.locals.videoChannel.id
183 } 188 }
184 const video = db.Video.build(videoData) 189 const video = new VideoModel(videoData)
185 video.url = getVideoActivityPubUrl(video) 190 video.url = getVideoActivityPubUrl(video)
186 191
187 const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename) 192 const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename)
@@ -192,7 +197,7 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi
192 resolution: videoFileHeight, 197 resolution: videoFileHeight,
193 size: videoPhysicalFile.size 198 size: videoPhysicalFile.size
194 } 199 }
195 const videoFile = db.VideoFile.build(videoFileData) 200 const videoFile = new VideoFileModel(videoFileData)
196 const videoDir = CONFIG.STORAGE.VIDEOS_DIR 201 const videoDir = CONFIG.STORAGE.VIDEOS_DIR
197 const source = join(videoDir, videoPhysicalFile.filename) 202 const source = join(videoDir, videoPhysicalFile.filename)
198 const destination = join(videoDir, video.getVideoFilename(videoFile)) 203 const destination = join(videoDir, video.getVideoFilename(videoFile))
@@ -210,7 +215,7 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi
210 ) 215 )
211 await Promise.all(tasks) 216 await Promise.all(tasks)
212 217
213 return db.sequelize.transaction(async t => { 218 return sequelizeTypescript.transaction(async t => {
214 const sequelizeOptions = { transaction: t } 219 const sequelizeOptions = { transaction: t }
215 220
216 if (CONFIG.TRANSCODING.ENABLED === true) { 221 if (CONFIG.TRANSCODING.ENABLED === true) {
@@ -232,9 +237,9 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi
232 video.VideoFiles = [ videoFile ] 237 video.VideoFiles = [ videoFile ]
233 238
234 if (videoInfo.tags) { 239 if (videoInfo.tags) {
235 const tagInstances = await db.Tag.findOrCreateTags(videoInfo.tags, t) 240 const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t)
236 241
237 await video.setTags(tagInstances, sequelizeOptions) 242 await video.$set('Tags', tagInstances, sequelizeOptions)
238 video.Tags = tagInstances 243 video.Tags = tagInstances
239 } 244 }
240 245
@@ -264,13 +269,13 @@ async function updateVideoRetryWrapper (req: express.Request, res: express.Respo
264} 269}
265 270
266async function updateVideo (req: express.Request, res: express.Response) { 271async function updateVideo (req: express.Request, res: express.Response) {
267 const videoInstance: VideoInstance = res.locals.video 272 const videoInstance: VideoModel = res.locals.video
268 const videoFieldsSave = videoInstance.toJSON() 273 const videoFieldsSave = videoInstance.toJSON()
269 const videoInfoToUpdate: VideoUpdate = req.body 274 const videoInfoToUpdate: VideoUpdate = req.body
270 const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE 275 const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE
271 276
272 try { 277 try {
273 await db.sequelize.transaction(async t => { 278 await sequelizeTypescript.transaction(async t => {
274 const sequelizeOptions = { 279 const sequelizeOptions = {
275 transaction: t 280 transaction: t
276 } 281 }
@@ -286,9 +291,9 @@ async function updateVideo (req: express.Request, res: express.Response) {
286 const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) 291 const videoInstanceUpdated = await videoInstance.save(sequelizeOptions)
287 292
288 if (videoInfoToUpdate.tags) { 293 if (videoInfoToUpdate.tags) {
289 const tagInstances = await db.Tag.findOrCreateTags(videoInfoToUpdate.tags, t) 294 const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t)
290 295
291 await videoInstance.setTags(tagInstances, sequelizeOptions) 296 await videoInstance.$set('Tags', tagInstances, sequelizeOptions)
292 videoInstance.Tags = tagInstances 297 videoInstance.Tags = tagInstances
293 } 298 }
294 299
@@ -350,7 +355,7 @@ async function getVideoDescription (req: express.Request, res: express.Response)
350} 355}
351 356
352async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) { 357async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
353 const resultList = await db.Video.listForApi(req.query.start, req.query.count, req.query.sort) 358 const resultList = await VideoModel.listForApi(req.query.start, req.query.count, req.query.sort)
354 359
355 return res.json(getFormattedObjects(resultList.data, resultList.total)) 360 return res.json(getFormattedObjects(resultList.data, resultList.total))
356} 361}
@@ -367,9 +372,9 @@ async function removeVideoRetryWrapper (req: express.Request, res: express.Respo
367} 372}
368 373
369async function removeVideo (req: express.Request, res: express.Response) { 374async function removeVideo (req: express.Request, res: express.Response) {
370 const videoInstance: VideoInstance = res.locals.video 375 const videoInstance: VideoModel = res.locals.video
371 376
372 await db.sequelize.transaction(async t => { 377 await sequelizeTypescript.transaction(async t => {
373 await videoInstance.destroy({ transaction: t }) 378 await videoInstance.destroy({ transaction: t })
374 }) 379 })
375 380
@@ -377,7 +382,7 @@ async function removeVideo (req: express.Request, res: express.Response) {
377} 382}
378 383
379async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) { 384async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
380 const resultList = await db.Video.searchAndPopulateAccountAndServerAndTags( 385 const resultList = await VideoModel.searchAndPopulateAccountAndServerAndTags(
381 req.query.search, 386 req.query.search,
382 req.query.start, 387 req.query.start,
383 req.query.count, 388 req.query.count,