]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/regenerate-thumbnails.ts
Update data in DB when regenerate thumbnails
[github/Chocobozzz/PeerTube.git] / scripts / regenerate-thumbnails.ts
CommitLineData
c2bd7a6f
C
1import { registerTSPaths } from '../server/helpers/register-ts-paths'
2registerTSPaths()
3
4import * as Bluebird from 'bluebird'
5import * as program from 'commander'
a0eeb45f 6import { pathExists, remove } from 'fs-extra'
c2bd7a6f
C
7import { processImage } from '@server/helpers/image-utils'
8import { THUMBNAILS_SIZE } from '@server/initializers/constants'
9import { VideoModel } from '@server/models/video/video'
10import { MVideo } from '@server/types/models'
11import { initDatabaseModels } from '@server/initializers/database'
a0eeb45f 12import { ActorImageModel } from '@server/models/account/actor-image'
c2bd7a6f
C
13
14program
15 .description('Regenerate local thumbnails using preview files')
16 .parse(process.argv)
17
18run()
19 .then(() => process.exit(0))
20 .catch(err => console.error(err))
21
22async function run () {
23 await initDatabaseModels(true)
24
25 const videos = await VideoModel.listLocal()
26
27 await Bluebird.map(videos, v => {
28 return processVideo(v)
29 .catch(err => console.error('Cannot process video %s.', v.url, err))
30 }, { concurrency: 20 })
31}
32
33async function processVideo (videoArg: MVideo) {
34 const video = await VideoModel.loadWithFiles(videoArg.id)
35
6794d100
C
36 console.log('Processing video %s.', video.name)
37
c2bd7a6f
C
38 const thumbnail = video.getMiniature()
39 const preview = video.getPreview()
40
c2bd7a6f
C
41 const previewPath = preview.getPath()
42
c2bd7a6f
C
43 if (!await pathExists(previewPath)) {
44 throw new Error(`Preview ${previewPath} does not exist on disk`)
45 }
46
47 const size = {
48 width: THUMBNAILS_SIZE.width,
49 height: THUMBNAILS_SIZE.height
50 }
a0eeb45f
C
51
52 const oldPath = thumbnail.getPath()
53
54 // Update thumbnail
55 thumbnail.filename = ActorImageModel.generateFilename()
56 thumbnail.width = size.width
57 thumbnail.height = size.height
58
59 const thumbnailPath = thumbnail.getPath()
c2bd7a6f 60 await processImage(previewPath, thumbnailPath, size, true)
a0eeb45f
C
61
62 // Save new attributes
63 await thumbnail.save()
64
65 // Remove old thumbnail
66 await remove(oldPath)
67
68 // Don't federate, remote instances will refresh the thumbnails after a while
c2bd7a6f 69}