1 import { map } from 'bluebird'
2 import { program } from 'commander'
3 import { pathExists, remove } from 'fs-extra'
4 import { generateImageFilename, processImage } from '@server/helpers/image-utils'
5 import { THUMBNAILS_SIZE } from '@server/initializers/constants'
6 import { initDatabaseModels } from '@server/initializers/database'
7 import { VideoModel } from '@server/models/video/video'
10 .description('Regenerate local thumbnails using preview files')
14 .then(() => process.exit(0))
15 .catch(err => console.error(err))
17 async function run () {
18 await initDatabaseModels(true)
20 const ids = await VideoModel.listLocalIds()
22 await map(ids, id => {
23 return processVideo(id)
24 .catch(err => console.error('Cannot process video %d.', id, err))
25 }, { concurrency: 20 })
28 async function processVideo (id: number) {
29 const video = await VideoModel.loadWithFiles(id)
31 console.log('Processing video %s.', video.name)
33 const thumbnail = video.getMiniature()
34 const preview = video.getPreview()
36 const previewPath = preview.getPath()
38 if (!await pathExists(previewPath)) {
39 throw new Error(`Preview ${previewPath} does not exist on disk`)
43 width: THUMBNAILS_SIZE.width,
44 height: THUMBNAILS_SIZE.height
47 const oldPath = thumbnail.getPath()
50 thumbnail.filename = generateImageFilename()
51 thumbnail.width = size.width
52 thumbnail.height = size.height
54 const thumbnailPath = thumbnail.getPath()
55 await processImage(previewPath, thumbnailPath, size, true)
57 // Save new attributes
58 await thumbnail.save()
60 // Remove old thumbnail
63 // Don't federate, remote instances will refresh the thumbnails after a while