import FS from 'fs' ; import Path from 'path' ; import Util from 'util' ; import { execute } from 'proper-job' ; import { createHash } from '../cache/hash' ; import Config from '../config' ; import type { Database , RouterTask , TaskRunnerCallback } from '../types' ; export function getTask ( database ) { return { id : 'REHASH' , description : 'Rehash existing media (updates database)' , runner : ( updateStatus ) => { const cachePath = Path . resolve ( Config . get ( ) . cachePath ) ; return execute ( async ( ) => { const files = await database . subset ( { } ) ; updateStatus ( 0 , files . length ) ; return { iterable : files , init : { current : 0 , max : files . length , } , } ; } , async ( existingHash , init ) => { if ( ! init ) { throw new Error ( 'init not defined' ) ; } const media = await database . getMedia ( existingHash ) ; if ( ! media ) { throw new Error ( `Couldn't find media to rehash: ${ existingHash }` ) ; } const hash = await createHash ( media . absolutePath ) ; if ( hash !== media . hash ) { console . warn ( `Hash changed for ${ media . absolutePath }` ) ; if ( media . type === 'video' ) { try { await Util . promisify ( FS . rename ) ( Path . resolve ( cachePath , media . hash ) , Path . resolve ( cachePath , hash ) ) ; } catch ( err ) { console . warn ( 'Failed to rename video' , err ) ; await database . saveMedia ( media . hash , { metadata : { qualityCache : [ ] } } ) ; } } try { await Util . promisify ( FS . rename ) ( Path . resolve ( cachePath , 'thumbnails' , `${ media . hash }.png` ) , Path . resolve ( cachePath , 'thumbnails' , `${ hash }.png` ) ) ; await Util . promisify ( FS . rename ) ( Path . resolve ( cachePath , 'previews' , `${ media . hash }.png` ) , Path . resolve ( cachePath , 'previews' , `${ hash }.png` ) ) ; } catch ( err ) { console . warn ( 'Failed to rename thumbnail during rehash' , err ) ; await database . saveMedia ( media . hash , { thumbnail : false } ) ; } } await database . saveMedia ( media . hash , { hash } ) ; updateStatus ( init . current ++ , init . max ) ; } , { parallel : 8 } ) ; } , } ; }