From ed31c059851a30bd5ba9999f8ecb3822d576b9f4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 2 Aug 2018 17:48:50 +0200 Subject: Add ability to list video imports --- server/models/video/video-import.ts | 82 +++++++++++++++++++++++++++++++++---- server/models/video/video.ts | 8 +++- 2 files changed, 81 insertions(+), 9 deletions(-) (limited to 'server/models') diff --git a/server/models/video/video-import.ts b/server/models/video/video-import.ts index 89eeafd6a..6b8a16b65 100644 --- a/server/models/video/video-import.ts +++ b/server/models/video/video-import.ts @@ -1,4 +1,5 @@ import { + AfterUpdate, AllowNull, BelongsTo, Column, @@ -12,13 +13,14 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { CONSTRAINTS_FIELDS } from '../../initializers' -import { throwIfNotValid } from '../utils' +import { CONSTRAINTS_FIELDS, VIDEO_IMPORT_STATES } from '../../initializers' +import { getSort, throwIfNotValid } from '../utils' import { VideoModel } from './video' import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports' import { VideoImport, VideoImportState } from '../../../shared' import { VideoChannelModel } from './video-channel' import { AccountModel } from '../account/account' +import { TagModel } from './tag' @DefaultScope({ include: [ @@ -35,6 +37,10 @@ import { AccountModel } from '../account/account' required: true } ] + }, + { + model: () => TagModel, + required: false } ] } @@ -79,27 +85,89 @@ export class VideoImportModel extends Model { @BelongsTo(() => VideoModel, { foreignKey: { - allowNull: false + allowNull: true }, - onDelete: 'CASCADE' + onDelete: 'set null' }) Video: VideoModel + @AfterUpdate + static deleteVideoIfFailed (instance: VideoImportModel, options) { + if (instance.state === VideoImportState.FAILED) { + return instance.Video.destroy({ transaction: options.transaction }) + } + + return undefined + } + static loadAndPopulateVideo (id: number) { return VideoImportModel.findById(id) } + static listUserVideoImportsForApi (accountId: number, start: number, count: number, sort: string) { + const query = { + offset: start, + limit: count, + order: getSort(sort), + include: [ + { + model: VideoModel, + required: true, + include: [ + { + model: VideoChannelModel, + required: true, + include: [ + { + model: AccountModel, + required: true, + where: { + id: accountId + } + } + ] + }, + { + model: TagModel, + required: false + } + ] + } + ] + } + + return VideoImportModel.unscoped() + .findAndCountAll(query) + .then(({ rows, count }) => { + return { + data: rows, + total: count + } + }) + } + toFormattedJSON (): VideoImport { const videoFormatOptions = { additionalAttributes: { state: true, waitTranscoding: true, scheduledUpdate: true } } - const video = Object.assign(this.Video.toFormattedJSON(videoFormatOptions), { - tags: this.Video.Tags.map(t => t.name) - }) + const video = this.Video + ? Object.assign(this.Video.toFormattedJSON(videoFormatOptions), { + tags: this.Video.Tags.map(t => t.name) + }) + : undefined return { targetUrl: this.targetUrl, + state: { + id: this.state, + label: VideoImportModel.getStateLabel(this.state) + }, + updatedAt: this.updatedAt.toISOString(), + createdAt: this.createdAt.toISOString(), video } } + private static getStateLabel (id: number) { + return VIDEO_IMPORT_STATES[id] || 'Unknown' + } } diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 459fcb31e..f32010014 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -1569,21 +1569,25 @@ export class VideoModel extends Model { removeThumbnail () { const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) return unlinkPromise(thumbnailPath) + .catch(err => logger.warn('Cannot delete thumbnail %s.', thumbnailPath, { err })) } removePreview () { - // Same name than video thumbnail - return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName()) + const previewPath = join(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName()) + return unlinkPromise(previewPath) + .catch(err => logger.warn('Cannot delete preview %s.', previewPath, { err })) } removeFile (videoFile: VideoFileModel) { const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile)) return unlinkPromise(filePath) + .catch(err => logger.warn('Cannot delete file %s.', filePath, { err })) } removeTorrent (videoFile: VideoFileModel) { const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) return unlinkPromise(torrentPath) + .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err })) } getActivityStreamDuration () { -- cgit v1.2.3