From 90a8bd305de4153ec21137a73ff482dcc2e3e19b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Feb 2021 16:25:53 +0100 Subject: Dissociate video file names and video uuid --- .../abstract-video-static-file-cache.ts | 2 +- server/lib/files-cache/videos-torrent-cache.ts | 54 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 server/lib/files-cache/videos-torrent-cache.ts (limited to 'server/lib/files-cache') diff --git a/server/lib/files-cache/abstract-video-static-file-cache.ts b/server/lib/files-cache/abstract-video-static-file-cache.ts index c06355446..af66689a0 100644 --- a/server/lib/files-cache/abstract-video-static-file-cache.ts +++ b/server/lib/files-cache/abstract-video-static-file-cache.ts @@ -2,7 +2,7 @@ import { remove } from 'fs-extra' import { logger } from '../../helpers/logger' import * as memoizee from 'memoizee' -type GetFilePathResult = { isOwned: boolean, path: string } | undefined +type GetFilePathResult = { isOwned: boolean, path: string, downloadName?: string } | undefined export abstract class AbstractVideoStaticFileCache { diff --git a/server/lib/files-cache/videos-torrent-cache.ts b/server/lib/files-cache/videos-torrent-cache.ts new file mode 100644 index 000000000..ca0e1770d --- /dev/null +++ b/server/lib/files-cache/videos-torrent-cache.ts @@ -0,0 +1,54 @@ +import { join } from 'path' +import { doRequestAndSaveToFile } from '@server/helpers/requests' +import { VideoFileModel } from '@server/models/video/video-file' +import { CONFIG } from '../../initializers/config' +import { FILES_CACHE } from '../../initializers/constants' +import { VideoModel } from '../../models/video/video' +import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' + +class VideosTorrentCache extends AbstractVideoStaticFileCache { + + private static instance: VideosTorrentCache + + private constructor () { + super() + } + + static get Instance () { + return this.instance || (this.instance = new this()) + } + + async getFilePathImpl (filename: string) { + const file = await VideoFileModel.loadWithVideoOrPlaylistByTorrentFilename(filename) + if (!file) return undefined + + if (file.getVideo().isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.TORRENTS_DIR, file.torrentFilename) } + + return this.loadRemoteFile(filename) + } + + // Key is the torrent filename + protected async loadRemoteFile (key: string) { + const file = await VideoFileModel.loadWithVideoOrPlaylistByTorrentFilename(key) + if (!file) return undefined + + if (file.getVideo().isOwned()) throw new Error('Cannot load remote file of owned video.') + + // Used to fetch the path + const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(file.getVideo().id) + if (!video) return undefined + + const remoteUrl = file.getRemoteTorrentUrl(video) + const destPath = join(FILES_CACHE.TORRENTS.DIRECTORY, file.torrentFilename) + + await doRequestAndSaveToFile({ uri: remoteUrl }, destPath) + + const downloadName = `${video.name}-${file.resolution}p.torrent` + + return { isOwned: false, path: destPath, downloadName } + } +} + +export { + VideosTorrentCache +} -- cgit v1.2.3