X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fthumbnail.ts;h=3cad6c668d0f3260d395b70c5d68a40359de2328;hb=a8b1b40485145ac1eae513a661d7dd6e0986ce96;hp=3b011b1d285de4fb1caf5e44f4f5a2a020b377ed;hpb=9c9a16678354f737144ce2156ce0b7e4faa97eac;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/thumbnail.ts b/server/models/video/thumbnail.ts index 3b011b1d2..3cad6c668 100644 --- a/server/models/video/thumbnail.ts +++ b/server/models/video/thumbnail.ts @@ -1,3 +1,4 @@ +import { remove } from 'fs-extra' import { join } from 'path' import { AfterDestroy, @@ -12,13 +13,14 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants' +import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' +import { MThumbnailVideo, MVideoAccountLight } from '@server/types/models' +import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' import { logger } from '../../helpers/logger' -import { remove } from 'fs-extra' import { CONFIG } from '../../initializers/config' +import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants' import { VideoModel } from './video' import { VideoPlaylistModel } from './video-playlist' -import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' @Table({ tableName: 'thumbnail', @@ -29,10 +31,14 @@ import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' { fields: [ 'videoPlaylistId' ], unique: true + }, + { + fields: [ 'filename', 'type' ], + unique: true } ] }) -export class ThumbnailModel extends Model { +export class ThumbnailModel extends Model { @AllowNull(false) @Column @@ -90,7 +96,7 @@ export class ThumbnailModel extends Model { @UpdatedAt updatedAt: Date - private static types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = { + private static readonly types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = { [ThumbnailType.MINIATURE]: { label: 'miniature', directory: CONFIG.STORAGE.THUMBNAILS_DIR, @@ -112,25 +118,31 @@ export class ThumbnailModel extends Model { .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err)) } - static loadByName (filename: string) { + static loadWithVideoByName (filename: string, thumbnailType: ThumbnailType): Promise { const query = { where: { - filename - } + filename, + type: thumbnailType + }, + include: [ + { + model: VideoModel.unscoped(), + required: true + } + ] } return ThumbnailModel.findOne(query) } - static generateDefaultPreviewName (videoUUID: string) { - return videoUUID + '.jpg' - } + getFileUrl (video: MVideoAccountLight) { + const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename - getFileUrl (isLocal: boolean) { - if (isLocal === false) return this.fileUrl + if (video.isOwned()) return WEBSERVER.URL + staticPath + if (this.fileUrl) return this.fileUrl - const staticPath = ThumbnailModel.types[this.type].staticPath - return WEBSERVER.URL + staticPath + this.filename + // Fallback if we don't have a file URL + return buildRemoteVideoBaseUrl(video, staticPath) } getPath () {