aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/thumbnail.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-02-12 16:23:19 +0100
committerChocobozzz <chocobozzz@cpy.re>2021-02-16 10:36:44 +0100
commita8b1b40485145ac1eae513a661d7dd6e0986ce96 (patch)
tree79436a042f1ed350278dd69e365f37ee623aa539 /server/models/video/thumbnail.ts
parent0472d474fdadd05211fb4f90ce275801db515d08 (diff)
downloadPeerTube-a8b1b40485145ac1eae513a661d7dd6e0986ce96.tar.gz
PeerTube-a8b1b40485145ac1eae513a661d7dd6e0986ce96.tar.zst
PeerTube-a8b1b40485145ac1eae513a661d7dd6e0986ce96.zip
Generate a name for thumbnails
Allows aggressive caching
Diffstat (limited to 'server/models/video/thumbnail.ts')
-rw-r--r--server/models/video/thumbnail.ts31
1 files changed, 19 insertions, 12 deletions
diff --git a/server/models/video/thumbnail.ts b/server/models/video/thumbnail.ts
index 6878a3155..3cad6c668 100644
--- a/server/models/video/thumbnail.ts
+++ b/server/models/video/thumbnail.ts
@@ -1,3 +1,4 @@
1import { remove } from 'fs-extra'
1import { join } from 'path' 2import { join } from 'path'
2import { 3import {
3 AfterDestroy, 4 AfterDestroy,
@@ -12,15 +13,14 @@ import {
12 Table, 13 Table,
13 UpdatedAt 14 UpdatedAt
14} from 'sequelize-typescript' 15} from 'sequelize-typescript'
15import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants' 16import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub'
17import { MThumbnailVideo, MVideoAccountLight } from '@server/types/models'
18import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
16import { logger } from '../../helpers/logger' 19import { logger } from '../../helpers/logger'
17import { remove } from 'fs-extra'
18import { CONFIG } from '../../initializers/config' 20import { CONFIG } from '../../initializers/config'
21import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants'
19import { VideoModel } from './video' 22import { VideoModel } from './video'
20import { VideoPlaylistModel } from './video-playlist' 23import { VideoPlaylistModel } from './video-playlist'
21import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
22import { MVideoAccountLight } from '@server/types/models'
23import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub'
24 24
25@Table({ 25@Table({
26 tableName: 'thumbnail', 26 tableName: 'thumbnail',
@@ -31,6 +31,10 @@ import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub'
31 { 31 {
32 fields: [ 'videoPlaylistId' ], 32 fields: [ 'videoPlaylistId' ],
33 unique: true 33 unique: true
34 },
35 {
36 fields: [ 'filename', 'type' ],
37 unique: true
34 } 38 }
35 ] 39 ]
36}) 40})
@@ -114,20 +118,23 @@ export class ThumbnailModel extends Model {
114 .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err)) 118 .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err))
115 } 119 }
116 120
117 static loadByName (filename: string) { 121 static loadWithVideoByName (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnailVideo> {
118 const query = { 122 const query = {
119 where: { 123 where: {
120 filename 124 filename,
121 } 125 type: thumbnailType
126 },
127 include: [
128 {
129 model: VideoModel.unscoped(),
130 required: true
131 }
132 ]
122 } 133 }
123 134
124 return ThumbnailModel.findOne(query) 135 return ThumbnailModel.findOne(query)
125 } 136 }
126 137
127 static generateDefaultPreviewName (videoUUID: string) {
128 return videoUUID + '.jpg'
129 }
130
131 getFileUrl (video: MVideoAccountLight) { 138 getFileUrl (video: MVideoAccountLight) {
132 const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename 139 const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename
133 140