import { abuseVideoRouter } from './abuse'
import { blacklistRouter } from './blacklist'
import { rateVideoRouter } from './rate'
+import { VideoInstance } from '../../../models/video/video-interface'
const videosRouter = express.Router()
videosRouter.delete('/:id',
authenticate,
videosRemoveValidator,
- removeVideo
+ removeVideoRetryWrapper
)
videosRouter.get('/search/:value',
retryTransactionWrapper(updateVideo, options)
.then(() => {
- // TODO : include Location of the new video -> 201
return res.type('json').status(204).end()
})
.catch(err => next(err))
.catch(err => next(err))
}
-function removeVideo (req: express.Request, res: express.Response, next: express.NextFunction) {
- const videoInstance = res.locals.video
+function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
+ const options = {
+ arguments: [ req, res ],
+ errorMessage: 'Cannot remove the video with many retries.'
+ }
- videoInstance.destroy()
+ retryTransactionWrapper(removeVideo, options)
.then(() => {
- logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
- res.type('json').status(204).end()
- })
- .catch(err => {
- logger.error('Errors when removed the video.', err)
- return next(err)
+ return res.type('json').status(204).end()
})
+ .catch(err => next(err))
+}
+
+function removeVideo (req: express.Request, res: express.Response) {
+ const videoInstance: VideoInstance = res.locals.video
+
+ return db.sequelize.transaction(t => {
+ return videoInstance.destroy({ transaction: t })
+ })
+ .then(() => {
+ logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
+ })
+ .catch(err => {
+ logger.error('Errors when removed the video.', err)
+ throw err
+ })
}
function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
return createRequest(options)
}
-function removeVideoToFriends (videoParams: RemoteVideoRemoveData) {
+function removeVideoToFriends (videoParams: RemoteVideoRemoveData, transaction: Sequelize.Transaction) {
const options = {
type: ENDPOINT_ACTIONS.REMOVE,
endpoint: REQUEST_ENDPOINTS.VIDEOS,
data: videoParams,
- transaction: null
+ transaction
}
return createRequest(options)
}
})
}
-function afterDestroy (video: VideoInstance) {
+function afterDestroy (video: VideoInstance, options: { transaction: Sequelize.Transaction }) {
const tasks = []
tasks.push(
tasks.push(
video.removePreview(),
- removeVideoToFriends(removeVideoToFriendsParams)
+ removeVideoToFriends(removeVideoToFriendsParams, options.transaction)
)
- // TODO: check files is populated
+ // Remove physical files and torrents
video.VideoFiles.forEach(file => {
video.removeFile(file),
video.removeTorrent(file)