From ca6d36227a9273f616a462d3aad6a721ab5dd627 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 30 Jan 2020 11:53:38 +0100 Subject: Add url field in caption and use it for thumbnails --- server/models/video/thumbnail.ts | 13 +++++++++---- server/models/video/video-caption.ts | 26 +++++++++++++++++++++----- server/models/video/video-format-utils.ts | 15 ++++++++------- server/models/video/video.ts | 2 +- 4 files changed, 39 insertions(+), 17 deletions(-) (limited to 'server/models') diff --git a/server/models/video/thumbnail.ts b/server/models/video/thumbnail.ts index 3b011b1d2..b69bc0872 100644 --- a/server/models/video/thumbnail.ts +++ b/server/models/video/thumbnail.ts @@ -19,6 +19,8 @@ import { CONFIG } from '../../initializers/config' import { VideoModel } from './video' import { VideoPlaylistModel } from './video-playlist' import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' +import { MVideoAccountLight } from '@server/typings/models' +import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' @Table({ tableName: 'thumbnail', @@ -126,11 +128,14 @@ export class ThumbnailModel extends Model { return videoUUID + '.jpg' } - getFileUrl (isLocal: boolean) { - if (isLocal === false) return this.fileUrl + getFileUrl (video: MVideoAccountLight) { + const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename - const staticPath = ThumbnailModel.types[this.type].staticPath - return WEBSERVER.URL + staticPath + this.filename + if (video.isOwned()) return WEBSERVER.URL + staticPath + if (this.fileUrl) return this.fileUrl + + // Fallback if we don't have a file URL + return buildRemoteVideoBaseUrl(video, staticPath) } getPath () { diff --git a/server/models/video/video-caption.ts b/server/models/video/video-caption.ts index 6335d44e4..1307c27f1 100644 --- a/server/models/video/video-caption.ts +++ b/server/models/video/video-caption.ts @@ -4,7 +4,7 @@ import { BeforeDestroy, BelongsTo, Column, - CreatedAt, + CreatedAt, DataType, ForeignKey, Is, Model, @@ -16,13 +16,14 @@ import { buildWhereIdOrUUID, throwIfNotValid } from '../utils' import { VideoModel } from './video' import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model' -import { LAZY_STATIC_PATHS, VIDEO_LANGUAGES } from '../../initializers/constants' +import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, VIDEO_LANGUAGES, WEBSERVER } from '../../initializers/constants' import { join } from 'path' import { logger } from '../../helpers/logger' import { remove } from 'fs-extra' import { CONFIG } from '../../initializers/config' import * as Bluebird from 'bluebird' -import { MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/typings/models' +import { MVideo, MVideoAccountLight, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/typings/models' +import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' export enum ScopeNames { WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE' @@ -64,6 +65,10 @@ export class VideoCaptionModel extends Model { @Column language: string + @AllowNull(true) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max)) + fileUrl: string + @ForeignKey(() => VideoModel) @Column videoId: number @@ -114,10 +119,11 @@ export class VideoCaptionModel extends Model { return VideoCaptionModel.findOne(query) } - static insertOrReplaceLanguage (videoId: number, language: string, transaction: Transaction) { + static insertOrReplaceLanguage (videoId: number, language: string, fileUrl: string, transaction: Transaction) { const values = { videoId, - language + language, + fileUrl } return VideoCaptionModel.upsert(values, { transaction, returning: true }) @@ -175,4 +181,14 @@ export class VideoCaptionModel extends Model { removeCaptionFile (this: MVideoCaptionFormattable) { return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.getCaptionName()) } + + getFileUrl (video: MVideoAccountLight) { + if (!this.Video) this.Video = video as VideoModel + + if (video.isOwned()) return WEBSERVER.URL + this.getCaptionStaticPath() + if (this.fileUrl) return this.fileUrl + + // Fallback if we don't have a file URL + return buildRemoteVideoBaseUrl(video, this.getCaptionStaticPath()) + } } diff --git a/server/models/video/video-format-utils.ts b/server/models/video/video-format-utils.ts index 2aa5b8677..bb50edcaa 100644 --- a/server/models/video/video-format-utils.ts +++ b/server/models/video/video-format-utils.ts @@ -307,11 +307,12 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject { for (const caption of video.VideoCaptions) { subtitleLanguage.push({ identifier: caption.language, - name: VideoCaptionModel.getLanguageLabel(caption.language) + name: VideoCaptionModel.getLanguageLabel(caption.language), + url: caption.getFileUrl(video) }) } - const miniature = video.getMiniature() + const icons = [ video.getMiniature(), video.getPreview() ] return { type: 'Video' as 'Video', @@ -336,13 +337,13 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject { content: video.getTruncatedDescription(), support: video.support, subtitleLanguage, - icon: { + icon: icons.map(i => ({ type: 'Image', - url: miniature.getFileUrl(video.isOwned()), + url: i.getFileUrl(video), mediaType: 'image/jpeg', - width: miniature.width, - height: miniature.height - }, + width: i.width, + height: i.height + })), url, likes: getVideoLikesActivityPubUrl(video), dislikes: getVideoDislikesActivityPubUrl(video), diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 20e1f1c4a..1a924e6c9 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -1121,7 +1121,7 @@ export class VideoModel extends Model { }, include: [ { - attributes: [ 'language' ], + attributes: [ 'language', 'fileUrl' ], model: VideoCaptionModel.unscoped(), required: false }, -- cgit v1.2.3