From a0eeb45f14bab539f505861cad8f5d42d9ba30cb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 8 Apr 2021 10:35:49 +0200 Subject: Update data in DB when regenerate thumbnails --- server/lib/activitypub/videos.ts | 3 ++- server/lib/thumbnail.ts | 5 +++-- server/models/account/actor-image.ts | 5 +++++ server/models/video/video.ts | 9 --------- server/tests/cli/regenerate-thumbnails.ts | 32 ++++++++++++++++++++++--------- 5 files changed, 33 insertions(+), 21 deletions(-) (limited to 'server') diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts index d484edd36..492b97b9e 100644 --- a/server/lib/activitypub/videos.ts +++ b/server/lib/activitypub/videos.ts @@ -3,6 +3,7 @@ import { maxBy, minBy } from 'lodash' import * as magnetUtil from 'magnet-uri' import { basename, join } from 'path' import { Transaction } from 'sequelize/types' +import { ActorImageModel } from '@server/models/account/actor-image' import { TrackerModel } from '@server/models/server/tracker' import { VideoLiveModel } from '@server/models/video/video-live' import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' @@ -899,7 +900,7 @@ function getPreviewFromIcons (videoObject: VideoObject) { function getPreviewUrl (previewIcon: ActivityIconObject, video: MVideoWithHost) { return previewIcon ? previewIcon.url - : buildRemoteVideoBaseUrl(video, join(LAZY_STATIC_PATHS.PREVIEWS, video.generatePreviewName())) + : buildRemoteVideoBaseUrl(video, join(LAZY_STATIC_PATHS.PREVIEWS, ActorImageModel.generateFilename())) } function getTrackerUrls (object: VideoObject, video: MVideoWithHost) { diff --git a/server/lib/thumbnail.ts b/server/lib/thumbnail.ts index 106f5fdaa..e1176ac08 100644 --- a/server/lib/thumbnail.ts +++ b/server/lib/thumbnail.ts @@ -1,4 +1,5 @@ import { join } from 'path' +import { ActorImageModel } from '@server/models/account/actor-image' import { ThumbnailType } from '../../shared/models/videos/thumbnail.type' import { generateImageFromVideoFile } from '../helpers/ffmpeg-utils' import { processImage } from '../helpers/image-utils' @@ -200,7 +201,7 @@ function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, si : undefined if (type === ThumbnailType.MINIATURE) { - const filename = video.generateThumbnailName() + const filename = ActorImageModel.generateFilename() const basePath = CONFIG.STORAGE.THUMBNAILS_DIR return { @@ -214,7 +215,7 @@ function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, si } if (type === ThumbnailType.PREVIEW) { - const filename = video.generatePreviewName() + const filename = ActorImageModel.generateFilename() const basePath = CONFIG.STORAGE.PREVIEWS_DIR return { diff --git a/server/models/account/actor-image.ts b/server/models/account/actor-image.ts index b779e3cf6..f7438991a 100644 --- a/server/models/account/actor-image.ts +++ b/server/models/account/actor-image.ts @@ -1,6 +1,7 @@ import { remove } from 'fs-extra' import { join } from 'path' import { AfterDestroy, AllowNull, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { v4 as uuidv4 } from 'uuid' import { MActorImageFormattable } from '@server/types/models' import { ActorImageType } from '@shared/models' import { ActorImage } from '../../../shared/models/actors/actor-image.model' @@ -53,6 +54,10 @@ export class ActorImageModel extends Model { .catch(err => logger.error('Cannot remove actor image file %s.', instance.filename, err)) } + static generateFilename () { + return uuidv4() + '.jpg' + } + static loadByName (filename: string) { const query = { where: { diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 086269921..b10c6e38e 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -24,7 +24,6 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { v4 as uuidv4 } from 'uuid' import { buildNSFWFilter } from '@server/helpers/express-utils' import { getPrivaciesForFederation, isPrivacyForFederation, isStateForFederation } from '@server/helpers/video' import { LiveManager } from '@server/lib/live-manager' @@ -1871,20 +1870,12 @@ export class VideoModel extends Model { this.Thumbnails.push(savedThumbnail) } - generateThumbnailName () { - return uuidv4() + '.jpg' - } - getMiniature () { if (Array.isArray(this.Thumbnails) === false) return undefined return this.Thumbnails.find(t => t.type === ThumbnailType.MINIATURE) } - generatePreviewName () { - return uuidv4() + '.jpg' - } - hasPreview () { return !!this.getPreview() } diff --git a/server/tests/cli/regenerate-thumbnails.ts b/server/tests/cli/regenerate-thumbnails.ts index a9c8642a5..8acb9f263 100644 --- a/server/tests/cli/regenerate-thumbnails.ts +++ b/server/tests/cli/regenerate-thumbnails.ts @@ -2,7 +2,7 @@ import 'mocha' import { expect } from 'chai' import { writeFile } from 'fs-extra' import { basename, join } from 'path' -import { Video } from '@shared/models' +import { Video, VideoDetails } from '@shared/models' import { buildServerDirectory, cleanupTests, @@ -19,6 +19,17 @@ import { } from '../../../shared/extra-utils' import { HttpStatusCode } from '@shared/core-utils' +async function testThumbnail (server: ServerInfo, videoId: number | string) { + const res = await getVideo(server.url, videoId) + const video: VideoDetails = res.body + + const res1 = await makeRawRequest(join(server.url, video.thumbnailPath), HttpStatusCode.OK_200) + expect(res1.body).to.not.have.lengthOf(0) + + const res2 = await makeRawRequest(join(server.url, video.thumbnailPath), HttpStatusCode.OK_200) + expect(res2.body).to.not.have.lengthOf(0) +} + describe('Test regenerate thumbnails script', function () { let servers: ServerInfo[] @@ -84,18 +95,21 @@ describe('Test regenerate thumbnails script', function () { await execCLI(`${env} npm run regenerate-thumbnails`) }) - it('Should have regenerated local thumbnails', async function () { - { - const res1 = await makeRawRequest(join(servers[0].url, video1.thumbnailPath), HttpStatusCode.OK_200) - expect(res1.body).to.not.have.lengthOf(0) + it('Should have generated new thumbnail files', async function () { + await testThumbnail(servers[0], video1.uuid) + await testThumbnail(servers[0], video2.uuid) - const res2 = await makeRawRequest(join(servers[0].url, video1.previewPath), HttpStatusCode.OK_200) - expect(res2.body).to.not.have.lengthOf(0) + const res = await makeRawRequest(join(servers[0].url, remoteVideo.thumbnailPath), HttpStatusCode.OK_200) + expect(res.body).to.have.lengthOf(0) + }) + + it('Should have deleted old thumbnail files', async function () { + { + await makeRawRequest(join(servers[0].url, video1.thumbnailPath), HttpStatusCode.NOT_FOUND_404) } { - const res = await makeRawRequest(join(servers[0].url, video2.thumbnailPath), HttpStatusCode.OK_200) - expect(res.body).to.not.have.lengthOf(0) + await makeRawRequest(join(servers[0].url, video2.thumbnailPath), HttpStatusCode.NOT_FOUND_404) } { -- cgit v1.2.3