From 40e87e9ecc54e3513fb586928330a7855eb192c6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 12 Jul 2018 19:02:00 +0200 Subject: Implement captions/subtitles --- server/lib/cache/videos-preview-cache.ts | 60 ++++++++------------------------ 1 file changed, 14 insertions(+), 46 deletions(-) (limited to 'server/lib/cache/videos-preview-cache.ts') diff --git a/server/lib/cache/videos-preview-cache.ts b/server/lib/cache/videos-preview-cache.ts index d09d55e11..1c0e7ed9d 100644 --- a/server/lib/cache/videos-preview-cache.ts +++ b/server/lib/cache/videos-preview-cache.ts @@ -1,71 +1,39 @@ -import * as asyncLRU from 'async-lru' -import { createWriteStream } from 'fs' import { join } from 'path' -import { unlinkPromise } from '../../helpers/core-utils' -import { logger } from '../../helpers/logger' -import { CACHE, CONFIG } from '../../initializers' +import { CACHE, CONFIG, STATIC_PATHS } from '../../initializers' import { VideoModel } from '../../models/video/video' -import { fetchRemoteVideoPreview } from '../activitypub' +import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' -class VideosPreviewCache { +class VideosPreviewCache extends AbstractVideoStaticFileCache { private static instance: VideosPreviewCache - private lru - - private constructor () { } + private constructor () { + super() + } static get Instance () { return this.instance || (this.instance = new this()) } - init (max: number) { - this.lru = new asyncLRU({ - max, - load: (key, cb) => { - this.loadPreviews(key) - .then(res => cb(null, res)) - .catch(err => cb(err)) - } - }) - - this.lru.on('evict', (obj: { key: string, value: string }) => { - unlinkPromise(obj.value).then(() => logger.debug('%s evicted from VideosPreviewCache', obj.value)) - }) - } - - async getPreviewPath (key: string) { - const video = await VideoModel.loadByUUID(key) + async getFilePath (videoUUID: string) { + const video = await VideoModel.loadByUUID(videoUUID) if (!video) return undefined if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()) - return new Promise((res, rej) => { - this.lru.get(key, (err, value) => { - err ? rej(err) : res(value) - }) - }) + return this.loadFromLRU(videoUUID) } - private async loadPreviews (key: string) { + protected async loadRemoteFile (key: string) { const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(key) if (!video) return undefined - if (video.isOwned()) throw new Error('Cannot load preview of owned video.') - - return this.saveRemotePreviewAndReturnPath(video) - } + if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.') - private saveRemotePreviewAndReturnPath (video: VideoModel) { - return new Promise((res, rej) => { - const req = fetchRemoteVideoPreview(video, rej) - const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName()) - const stream = createWriteStream(path) + const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreviewName()) + const destPath = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName()) - req.pipe(stream) - .on('error', (err) => rej(err)) - .on('finish', () => res(path)) - }) + return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) } } -- cgit v1.2.3