aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-02-15 14:08:16 +0100
committerChocobozzz <chocobozzz@cpy.re>2021-02-16 10:36:44 +0100
commit6302d599cdf98b5a5363a2a1dcdc266447950191 (patch)
treeb7dc6dc0f08f0fb8a20720242c9c0a71afeeaa3f /server/lib
parenta8b1b40485145ac1eae513a661d7dd6e0986ce96 (diff)
downloadPeerTube-6302d599cdf98b5a5363a2a1dcdc266447950191.tar.gz
PeerTube-6302d599cdf98b5a5363a2a1dcdc266447950191.tar.zst
PeerTube-6302d599cdf98b5a5363a2a1dcdc266447950191.zip
Generate a name for caption files
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/activitypub/playlist.ts2
-rw-r--r--server/lib/activitypub/videos.ts21
-rw-r--r--server/lib/files-cache/videos-caption-cache.ts30
-rw-r--r--server/lib/files-cache/videos-preview-cache.ts1
-rw-r--r--server/lib/thumbnail.ts3
5 files changed, 33 insertions, 24 deletions
diff --git a/server/lib/activitypub/playlist.ts b/server/lib/activitypub/playlist.ts
index 8b54a001a..53298e968 100644
--- a/server/lib/activitypub/playlist.ts
+++ b/server/lib/activitypub/playlist.ts
@@ -99,8 +99,6 @@ async function createOrUpdateVideoPlaylist (playlistObject: PlaylistObject, byAc
99 return Promise.resolve() 99 return Promise.resolve()
100 }) 100 })
101 101
102 logger.info('toto', { playlist, id: playlist.id })
103
104 const refreshedPlaylist = await VideoPlaylistModel.loadWithAccountAndChannel(playlist.id, null) 102 const refreshedPlaylist = await VideoPlaylistModel.loadWithAccountAndChannel(playlist.id, null)
105 103
106 if (playlistObject.icon) { 104 if (playlistObject.icon) {
diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts
index b5a199e67..201ef0302 100644
--- a/server/lib/activitypub/videos.ts
+++ b/server/lib/activitypub/videos.ts
@@ -56,6 +56,7 @@ import {
56 MVideoAccountLightBlacklistAllFiles, 56 MVideoAccountLightBlacklistAllFiles,
57 MVideoAP, 57 MVideoAP,
58 MVideoAPWithoutCaption, 58 MVideoAPWithoutCaption,
59 MVideoCaption,
59 MVideoFile, 60 MVideoFile,
60 MVideoFullLight, 61 MVideoFullLight,
61 MVideoId, 62 MVideoId,
@@ -90,7 +91,7 @@ async function federateVideoIfNeeded (videoArg: MVideoAPWithoutCaption, isNewVid
90 // Fetch more attributes that we will need to serialize in AP object 91 // Fetch more attributes that we will need to serialize in AP object
91 if (isArray(video.VideoCaptions) === false) { 92 if (isArray(video.VideoCaptions) === false) {
92 video.VideoCaptions = await video.$get('VideoCaptions', { 93 video.VideoCaptions = await video.$get('VideoCaptions', {
93 attributes: [ 'language' ], 94 attributes: [ 'filename', 'language' ],
94 transaction 95 transaction
95 }) 96 })
96 } 97 }
@@ -423,7 +424,14 @@ async function updateVideoFromAP (options: {
423 await VideoCaptionModel.deleteAllCaptionsOfRemoteVideo(videoUpdated.id, t) 424 await VideoCaptionModel.deleteAllCaptionsOfRemoteVideo(videoUpdated.id, t)
424 425
425 const videoCaptionsPromises = videoObject.subtitleLanguage.map(c => { 426 const videoCaptionsPromises = videoObject.subtitleLanguage.map(c => {
426 return VideoCaptionModel.insertOrReplaceLanguage(videoUpdated.id, c.identifier, c.url, t) 427 const caption = new VideoCaptionModel({
428 videoId: videoUpdated.id,
429 filename: VideoCaptionModel.generateCaptionName(c.identifier),
430 language: c.identifier,
431 fileUrl: c.url
432 }) as MVideoCaption
433
434 return VideoCaptionModel.insertOrReplaceLanguage(caption, t)
427 }) 435 })
428 await Promise.all(videoCaptionsPromises) 436 await Promise.all(videoCaptionsPromises)
429 } 437 }
@@ -629,7 +637,14 @@ async function createVideo (videoObject: VideoObject, channel: MChannelAccountLi
629 637
630 // Process captions 638 // Process captions
631 const videoCaptionsPromises = videoObject.subtitleLanguage.map(c => { 639 const videoCaptionsPromises = videoObject.subtitleLanguage.map(c => {
632 return VideoCaptionModel.insertOrReplaceLanguage(videoCreated.id, c.identifier, c.url, t) 640 const caption = new VideoCaptionModel({
641 videoId: videoCreated.id,
642 filename: VideoCaptionModel.generateCaptionName(c.identifier),
643 language: c.identifier,
644 fileUrl: c.url
645 }) as MVideoCaption
646
647 return VideoCaptionModel.insertOrReplaceLanguage(caption, t)
633 }) 648 })
634 await Promise.all(videoCaptionsPromises) 649 await Promise.all(videoCaptionsPromises)
635 650
diff --git a/server/lib/files-cache/videos-caption-cache.ts b/server/lib/files-cache/videos-caption-cache.ts
index 26ab3bd0d..ee0447010 100644
--- a/server/lib/files-cache/videos-caption-cache.ts
+++ b/server/lib/files-cache/videos-caption-cache.ts
@@ -1,17 +1,13 @@
1import { join } from 'path' 1import { join } from 'path'
2import { doRequestAndSaveToFile } from '@server/helpers/requests'
3import { CONFIG } from '../../initializers/config'
2import { FILES_CACHE } from '../../initializers/constants' 4import { FILES_CACHE } from '../../initializers/constants'
3import { VideoModel } from '../../models/video/video' 5import { VideoModel } from '../../models/video/video'
4import { VideoCaptionModel } from '../../models/video/video-caption' 6import { VideoCaptionModel } from '../../models/video/video-caption'
5import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' 7import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
6import { CONFIG } from '../../initializers/config'
7import { logger } from '../../helpers/logger'
8import { doRequestAndSaveToFile } from '@server/helpers/requests'
9 8
10type GetPathParam = { videoId: string, language: string } 9class VideosCaptionCache extends AbstractVideoStaticFileCache <string> {
11 10
12class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> {
13
14 private static readonly KEY_DELIMITER = '%'
15 private static instance: VideosCaptionCache 11 private static instance: VideosCaptionCache
16 12
17 private constructor () { 13 private constructor () {
@@ -22,32 +18,28 @@ class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> {
22 return this.instance || (this.instance = new this()) 18 return this.instance || (this.instance = new this())
23 } 19 }
24 20
25 async getFilePathImpl (params: GetPathParam) { 21 async getFilePathImpl (filename: string) {
26 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language) 22 const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(filename)
27 if (!videoCaption) return undefined 23 if (!videoCaption) return undefined
28 24
29 if (videoCaption.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName()) } 25 if (videoCaption.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.filename) }
30 26
31 const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language 27 return this.loadRemoteFile(filename)
32 return this.loadRemoteFile(key)
33 } 28 }
34 29
30 // Key is the caption filename
35 protected async loadRemoteFile (key: string) { 31 protected async loadRemoteFile (key: string) {
36 logger.debug('Loading remote caption file %s.', key) 32 const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(key)
37
38 const [ videoId, language ] = key.split(VideosCaptionCache.KEY_DELIMITER)
39
40 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(videoId, language)
41 if (!videoCaption) return undefined 33 if (!videoCaption) return undefined
42 34
43 if (videoCaption.isOwned()) throw new Error('Cannot load remote caption of owned video.') 35 if (videoCaption.isOwned()) throw new Error('Cannot load remote caption of owned video.')
44 36
45 // Used to fetch the path 37 // Used to fetch the path
46 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) 38 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoCaption.videoId)
47 if (!video) return undefined 39 if (!video) return undefined
48 40
49 const remoteUrl = videoCaption.getFileUrl(video) 41 const remoteUrl = videoCaption.getFileUrl(video)
50 const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName()) 42 const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.filename)
51 43
52 await doRequestAndSaveToFile({ uri: remoteUrl }, destPath) 44 await doRequestAndSaveToFile({ uri: remoteUrl }, destPath)
53 45
diff --git a/server/lib/files-cache/videos-preview-cache.ts b/server/lib/files-cache/videos-preview-cache.ts
index 51146d718..47488da74 100644
--- a/server/lib/files-cache/videos-preview-cache.ts
+++ b/server/lib/files-cache/videos-preview-cache.ts
@@ -28,6 +28,7 @@ class VideosPreviewCache extends AbstractVideoStaticFileCache <string> {
28 return this.loadRemoteFile(thumbnail.Video.uuid) 28 return this.loadRemoteFile(thumbnail.Video.uuid)
29 } 29 }
30 30
31 // Key is the video UUID
31 protected async loadRemoteFile (key: string) { 32 protected async loadRemoteFile (key: string) {
32 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(key) 33 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(key)
33 if (!video) return undefined 34 if (!video) return undefined
diff --git a/server/lib/thumbnail.ts b/server/lib/thumbnail.ts
index 740b83acb..33aa7159c 100644
--- a/server/lib/thumbnail.ts
+++ b/server/lib/thumbnail.ts
@@ -166,6 +166,9 @@ async function createThumbnailFromFunction (parameters: {
166}) { 166}) {
167 const { thumbnailCreator, filename, width, height, type, existingThumbnail, automaticallyGenerated = null, fileUrl = null } = parameters 167 const { thumbnailCreator, filename, width, height, type, existingThumbnail, automaticallyGenerated = null, fileUrl = null } = parameters
168 168
169 // Remove old file
170 if (existingThumbnail) await existingThumbnail.removeThumbnail()
171
169 const thumbnail = existingThumbnail || new ThumbnailModel() 172 const thumbnail = existingThumbnail || new ThumbnailModel()
170 173
171 thumbnail.filename = filename 174 thumbnail.filename = filename