diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-09-12 12:53:55 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-09-12 13:12:35 +0200 |
commit | 6d33593a0829a7f041127d50d4c455456550a47f (patch) | |
tree | da316570134a33f4f8cef1fda282c8ef2d0e30a6 /server/controllers/api | |
parent | 7ca86c864e102b65e4ff3224a06554a66148fef8 (diff) | |
download | PeerTube-6d33593a0829a7f041127d50d4c455456550a47f.tar.gz PeerTube-6d33593a0829a7f041127d50d4c455456550a47f.tar.zst PeerTube-6d33593a0829a7f041127d50d4c455456550a47f.zip |
Improve real world script
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/remote/videos.ts | 30 | ||||
-rw-r--r-- | server/controllers/api/videos/index.ts | 11 |
2 files changed, 28 insertions, 13 deletions
diff --git a/server/controllers/api/remote/videos.ts b/server/controllers/api/remote/videos.ts index 9dd4afdb5..23023211f 100644 --- a/server/controllers/api/remote/videos.ts +++ b/server/controllers/api/remote/videos.ts | |||
@@ -17,7 +17,7 @@ import { | |||
17 | } from '../../../middlewares' | 17 | } from '../../../middlewares' |
18 | import { logger, retryTransactionWrapper } from '../../../helpers' | 18 | import { logger, retryTransactionWrapper } from '../../../helpers' |
19 | import { quickAndDirtyUpdatesVideoToFriends } from '../../../lib' | 19 | import { quickAndDirtyUpdatesVideoToFriends } from '../../../lib' |
20 | import { PodInstance } from '../../../models' | 20 | import { PodInstance, VideoFileInstance } from '../../../models' |
21 | import { | 21 | import { |
22 | RemoteVideoRequest, | 22 | RemoteVideoRequest, |
23 | RemoteVideoCreateData, | 23 | RemoteVideoCreateData, |
@@ -81,7 +81,7 @@ function remoteVideos (req: express.Request, res: express.Response, next: expres | |||
81 | // Get the function we need to call in order to process the request | 81 | // Get the function we need to call in order to process the request |
82 | const fun = functionsHash[request.type] | 82 | const fun = functionsHash[request.type] |
83 | if (fun === undefined) { | 83 | if (fun === undefined) { |
84 | logger.error('Unkown remote request type %s.', request.type) | 84 | logger.error('Unknown remote request type %s.', request.type) |
85 | return | 85 | return |
86 | } | 86 | } |
87 | 87 | ||
@@ -176,7 +176,7 @@ function processVideosEvents (eventData: RemoteVideoEventData, fromPod: PodInsta | |||
176 | return quickAndDirtyUpdatesVideoToFriends(qadusParams, t) | 176 | return quickAndDirtyUpdatesVideoToFriends(qadusParams, t) |
177 | }) | 177 | }) |
178 | }) | 178 | }) |
179 | .then(() => logger.info('Remote video event processed for video %s.', eventData.uuid)) | 179 | .then(() => logger.info('Remote video event processed for video with uuid %s.', eventData.uuid)) |
180 | .catch(err => { | 180 | .catch(err => { |
181 | logger.debug('Cannot process a video event.', err) | 181 | logger.debug('Cannot process a video event.', err) |
182 | throw err | 182 | throw err |
@@ -193,14 +193,14 @@ function quickAndDirtyUpdateVideoRetryWrapper (videoData: RemoteQaduVideoData, f | |||
193 | } | 193 | } |
194 | 194 | ||
195 | function quickAndDirtyUpdateVideo (videoData: RemoteQaduVideoData, fromPod: PodInstance) { | 195 | function quickAndDirtyUpdateVideo (videoData: RemoteQaduVideoData, fromPod: PodInstance) { |
196 | let videoName | 196 | let videoUUID = '' |
197 | 197 | ||
198 | return db.sequelize.transaction(t => { | 198 | return db.sequelize.transaction(t => { |
199 | return fetchVideoByHostAndUUID(fromPod.host, videoData.uuid) | 199 | return fetchVideoByHostAndUUID(fromPod.host, videoData.uuid) |
200 | .then(videoInstance => { | 200 | .then(videoInstance => { |
201 | const options = { transaction: t } | 201 | const options = { transaction: t } |
202 | 202 | ||
203 | videoName = videoInstance.name | 203 | videoUUID = videoInstance.uuid |
204 | 204 | ||
205 | if (videoData.views) { | 205 | if (videoData.views) { |
206 | videoInstance.set('views', videoData.views) | 206 | videoInstance.set('views', videoData.views) |
@@ -217,7 +217,7 @@ function quickAndDirtyUpdateVideo (videoData: RemoteQaduVideoData, fromPod: PodI | |||
217 | return videoInstance.save(options) | 217 | return videoInstance.save(options) |
218 | }) | 218 | }) |
219 | }) | 219 | }) |
220 | .then(() => logger.info('Remote video %s quick and dirty updated', videoName)) | 220 | .then(() => logger.info('Remote video with uuid %s quick and dirty updated', videoUUID)) |
221 | .catch(err => logger.debug('Cannot quick and dirty update the remote video.', err)) | 221 | .catch(err => logger.debug('Cannot quick and dirty update the remote video.', err)) |
222 | } | 222 | } |
223 | 223 | ||
@@ -315,7 +315,7 @@ function addRemoteVideo (videoToCreateData: RemoteVideoCreateData, fromPod: PodI | |||
315 | return videoCreated.setTags(tagInstances, options) | 315 | return videoCreated.setTags(tagInstances, options) |
316 | }) | 316 | }) |
317 | }) | 317 | }) |
318 | .then(() => logger.info('Remote video %s inserted.', videoToCreateData.name)) | 318 | .then(() => logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)) |
319 | .catch(err => { | 319 | .catch(err => { |
320 | logger.debug('Cannot insert the remote video.', err) | 320 | logger.debug('Cannot insert the remote video.', err) |
321 | throw err | 321 | throw err |
@@ -361,7 +361,17 @@ function updateRemoteVideo (videoAttributesToUpdate: RemoteVideoUpdateData, from | |||
361 | return videoInstance.save(options).then(() => ({ videoInstance, tagInstances })) | 361 | return videoInstance.save(options).then(() => ({ videoInstance, tagInstances })) |
362 | }) | 362 | }) |
363 | .then(({ tagInstances, videoInstance }) => { | 363 | .then(({ tagInstances, videoInstance }) => { |
364 | const tasks = [] | 364 | const tasks: Promise<void>[] = [] |
365 | |||
366 | // Remove old video files | ||
367 | videoInstance.VideoFiles.forEach(videoFile => { | ||
368 | tasks.push(videoFile.destroy()) | ||
369 | }) | ||
370 | |||
371 | return Promise.all(tasks).then(() => ({ tagInstances, videoInstance })) | ||
372 | }) | ||
373 | .then(({ tagInstances, videoInstance }) => { | ||
374 | const tasks: Promise<VideoFileInstance>[] = [] | ||
365 | const options = { | 375 | const options = { |
366 | transaction: t | 376 | transaction: t |
367 | } | 377 | } |
@@ -386,7 +396,7 @@ function updateRemoteVideo (videoAttributesToUpdate: RemoteVideoUpdateData, from | |||
386 | return videoInstance.setTags(tagInstances, options) | 396 | return videoInstance.setTags(tagInstances, options) |
387 | }) | 397 | }) |
388 | }) | 398 | }) |
389 | .then(() => logger.info('Remote video %s updated', videoAttributesToUpdate.name)) | 399 | .then(() => logger.info('Remote video with uuid %s updated', videoAttributesToUpdate.uuid)) |
390 | .catch(err => { | 400 | .catch(err => { |
391 | // This is just a debug because we will retry the insert | 401 | // This is just a debug because we will retry the insert |
392 | logger.debug('Cannot update the remote video.', err) | 402 | logger.debug('Cannot update the remote video.', err) |
@@ -398,7 +408,7 @@ function removeRemoteVideo (videoToRemoveData: RemoteVideoRemoveData, fromPod: P | |||
398 | // We need the instance because we have to remove some other stuffs (thumbnail etc) | 408 | // We need the instance because we have to remove some other stuffs (thumbnail etc) |
399 | return fetchVideoByHostAndUUID(fromPod.host, videoToRemoveData.uuid) | 409 | return fetchVideoByHostAndUUID(fromPod.host, videoToRemoveData.uuid) |
400 | .then(video => { | 410 | .then(video => { |
401 | logger.debug('Removing remote video %s.', video.uuid) | 411 | logger.debug('Removing remote video with uuid %s.', video.uuid) |
402 | return video.destroy() | 412 | return video.destroy() |
403 | }) | 413 | }) |
404 | .catch(err => { | 414 | .catch(err => { |
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 3a19fe989..7a9cd9d37 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts | |||
@@ -157,6 +157,7 @@ function addVideoRetryWrapper (req: express.Request, res: express.Response, next | |||
157 | 157 | ||
158 | function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) { | 158 | function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) { |
159 | const videoInfo: VideoCreate = req.body | 159 | const videoInfo: VideoCreate = req.body |
160 | let videoUUID = '' | ||
160 | 161 | ||
161 | return db.sequelize.transaction(t => { | 162 | return db.sequelize.transaction(t => { |
162 | const user = res.locals.oauth.token.User | 163 | const user = res.locals.oauth.token.User |
@@ -241,6 +242,7 @@ function addVideo (req: express.Request, res: express.Response, videoPhysicalFil | |||
241 | .then(videoCreated => { | 242 | .then(videoCreated => { |
242 | // Do not forget to add Author information to the created video | 243 | // Do not forget to add Author information to the created video |
243 | videoCreated.Author = author | 244 | videoCreated.Author = author |
245 | videoUUID = videoCreated.uuid | ||
244 | 246 | ||
245 | return { tagInstances, video: videoCreated, videoFile } | 247 | return { tagInstances, video: videoCreated, videoFile } |
246 | }) | 248 | }) |
@@ -274,7 +276,7 @@ function addVideo (req: express.Request, res: express.Response, videoPhysicalFil | |||
274 | }) | 276 | }) |
275 | }) | 277 | }) |
276 | }) | 278 | }) |
277 | .then(() => logger.info('Video with name %s created.', videoInfo.name)) | 279 | .then(() => logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoUUID)) |
278 | .catch((err: Error) => { | 280 | .catch((err: Error) => { |
279 | logger.debug('Cannot insert the video.', err) | 281 | logger.debug('Cannot insert the video.', err) |
280 | throw err | 282 | throw err |
@@ -342,7 +344,7 @@ function updateVideo (req: express.Request, res: express.Response) { | |||
342 | }) | 344 | }) |
343 | }) | 345 | }) |
344 | .then(() => { | 346 | .then(() => { |
345 | logger.info('Video with name %s updated.', videoInstance.name) | 347 | logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid) |
346 | }) | 348 | }) |
347 | .catch(err => { | 349 | .catch(err => { |
348 | logger.debug('Cannot update the video.', err) | 350 | logger.debug('Cannot update the video.', err) |
@@ -398,7 +400,10 @@ function removeVideo (req: express.Request, res: express.Response, next: express | |||
398 | const videoInstance = res.locals.video | 400 | const videoInstance = res.locals.video |
399 | 401 | ||
400 | videoInstance.destroy() | 402 | videoInstance.destroy() |
401 | .then(() => res.type('json').status(204).end()) | 403 | .then(() => { |
404 | logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) | ||
405 | res.type('json').status(204).end() | ||
406 | }) | ||
402 | .catch(err => { | 407 | .catch(err => { |
403 | logger.error('Errors when removed the video.', err) | 408 | logger.error('Errors when removed the video.', err) |
404 | return next(err) | 409 | return next(err) |