X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=scripts%2Fprune-storage.ts;h=4ab0b4863fd1ccff1aaaf4ff16652272be98787b;hb=58d515e32fe1d0133435b3a5e550c6ff24906fff;hp=bc59da6afb383d124ca6e5913274d2d51d2d64b4;hpb=92d83c6a78e86c66a55958f8e36a7f2e123efdf8;p=github%2FChocobozzz%2FPeerTube.git diff --git a/scripts/prune-storage.ts b/scripts/prune-storage.ts index bc59da6af..4ab0b4863 100755 --- a/scripts/prune-storage.ts +++ b/scripts/prune-storage.ts @@ -1,9 +1,11 @@ import * as prompt from 'prompt' import { join } from 'path' -import { readdirPromise, unlinkPromise } from '../server/helpers/core-utils' import { CONFIG } from '../server/initializers/constants' import { VideoModel } from '../server/models/video/video' import { initDatabaseModels } from '../server/initializers' +import { remove, readdir } from 'fs-extra' +import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy' +import { getUUIDFromFilename } from '../server/helpers/utils' run() .then(() => process.exit(0)) @@ -15,16 +17,23 @@ run() async function run () { await initDatabaseModels(true) - const storageToPrune = [ + const storageOnlyOwnedToPrune = [ CONFIG.STORAGE.VIDEOS_DIR, - CONFIG.STORAGE.PREVIEWS_DIR, - CONFIG.STORAGE.THUMBNAILS_DIR, CONFIG.STORAGE.TORRENTS_DIR ] + const storageForAllToPrune = [ + CONFIG.STORAGE.PREVIEWS_DIR, + CONFIG.STORAGE.THUMBNAILS_DIR + ] + let toDelete: string[] = [] - for (const directory of storageToPrune) { - toDelete = toDelete.concat(await pruneDirectory(directory)) + for (const directory of storageOnlyOwnedToPrune) { + toDelete = toDelete.concat(await pruneDirectory(directory, true)) + } + + for (const directory of storageForAllToPrune) { + toDelete = toDelete.concat(await pruneDirectory(directory, false)) } if (toDelete.length === 0) { @@ -39,7 +48,7 @@ async function run () { console.log('Processing delete...\n') for (const path of toDelete) { - await unlinkPromise(path) + await remove(path) } console.log('Done!') @@ -48,31 +57,32 @@ async function run () { } } -async function pruneDirectory (directory: string) { - const files = await readdirPromise(directory) +async function pruneDirectory (directory: string, onlyOwned = false) { + const files = await readdir(directory) const toDelete: string[] = [] for (const file of files) { const uuid = getUUIDFromFilename(file) let video: VideoModel + let localRedundancy: boolean - if (uuid) video = await VideoModel.loadByUUID(uuid) + if (uuid) { + video = await VideoModel.loadByUUIDWithFile(uuid) + localRedundancy = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid) + } - if (!uuid || !video) toDelete.push(join(directory, file)) + if ( + !uuid || + !video || + (onlyOwned === true && (video.isOwned() === false && localRedundancy === false)) + ) { + toDelete.push(join(directory, file)) + } } return toDelete } -function getUUIDFromFilename (filename: string) { - const regex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/ - const result = filename.match(regex) - - if (!result || Array.isArray(result) === false) return null - - return result[0] -} - async function askConfirmation () { return new Promise((res, rej) => { prompt.start() @@ -80,7 +90,8 @@ async function askConfirmation () { properties: { confirm: { type: 'string', - description: 'Are you sure you want to delete these files? Please check carefully', + description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' + + ' Can we delete these files?', default: 'n', required: true }