X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=scripts%2Fprune-storage.ts;h=fa3d817446754cb50413dcb94ea8bdfbd12b914f;hb=b6e0e6a31bdef7c0faebca96864eb334dd9e7ad7;hp=b00f2093426ddcfac3f909afe9a8f5bdffc9606d;hpb=627621c1e8d37c33f7b3dd59f4c8907b12c630bc;p=github%2FChocobozzz%2FPeerTube.git diff --git a/scripts/prune-storage.ts b/scripts/prune-storage.ts index b00f20934..fa3d81744 100755 --- a/scripts/prune-storage.ts +++ b/scripts/prune-storage.ts @@ -1,9 +1,18 @@ +import { registerTSPaths } from '../server/helpers/register-ts-paths' +registerTSPaths() + import * as prompt from 'prompt' import { join } from 'path' -import { CONFIG } from '../server/initializers/constants' +import { CONFIG } from '../server/initializers/config' import { VideoModel } from '../server/models/video/video' import { initDatabaseModels } from '../server/initializers' -import { remove, readdir } from 'fs-extra' +import { readdir, remove } from 'fs-extra' +import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy' +import * as Bluebird from 'bluebird' +import { getUUIDFromFilename } from '../server/helpers/utils' +import { ThumbnailModel } from '../server/models/video/thumbnail' +import { AvatarModel } from '../server/models/avatar/avatar' +import { uniq, values } from 'lodash' run() .then(() => process.exit(0)) @@ -13,19 +22,31 @@ run() }) async function run () { - await initDatabaseModels(true) + const dirs = values(CONFIG.STORAGE) + + if (uniq(dirs).length !== dirs.length) { + console.error('Cannot prune storage because you put multiple storage keys in the same directory.') + process.exit(0) + } - const storageToPrune = [ - CONFIG.STORAGE.VIDEOS_DIR, - CONFIG.STORAGE.PREVIEWS_DIR, - CONFIG.STORAGE.THUMBNAILS_DIR, - CONFIG.STORAGE.TORRENTS_DIR - ] + await initDatabaseModels(true) let toDelete: string[] = [] - for (const directory of storageToPrune) { - toDelete = toDelete.concat(await pruneDirectory(directory)) - } + + toDelete = toDelete.concat( + await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesVideoExist(true)), + await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesVideoExist(true)), + + await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist), + + await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true)), + await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false)), + + await pruneDirectory(CONFIG.STORAGE.AVATARS_DIR, doesAvatarExist) + ) + + const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR) + toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t))) if (toDelete.length === 0) { console.log('No files to delete.') @@ -48,29 +69,79 @@ async function run () { } } -async function pruneDirectory (directory: string) { +type ExistFun = (file: string) => Promise +async function pruneDirectory (directory: string, existFun: ExistFun) { const files = await readdir(directory) const toDelete: string[] = [] - for (const file of files) { + await Bluebird.map(files, async file => { + if (await existFun(file) !== true) { + toDelete.push(join(directory, file)) + } + }, { concurrency: 20 }) + + return toDelete +} + +function doesVideoExist (keepOnlyOwned: boolean) { + return async (file: string) => { const uuid = getUUIDFromFilename(file) - let video: VideoModel + const video = await VideoModel.loadByUUID(uuid) - if (uuid) video = await VideoModel.loadByUUIDWithFile(uuid) + return video && (keepOnlyOwned === false || video.isOwned()) + } +} + +function doesThumbnailExist (keepOnlyOwned: boolean) { + return async (file: string) => { + const thumbnail = await ThumbnailModel.loadByName(file) + if (!thumbnail) return false + + if (keepOnlyOwned) { + const video = await VideoModel.load(thumbnail.videoId) + if (video.isOwned() === false) return false + } - if (!uuid || !video) toDelete.push(join(directory, file)) + return true } +} - return toDelete +async function doesAvatarExist (file: string) { + const avatar = await AvatarModel.loadByName(file) + + return !!avatar } -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) +async function doesRedundancyExist (file: string) { + const uuid = getUUIDFromFilename(file) + const video = await VideoModel.loadWithFiles(uuid) - if (!result || Array.isArray(result) === false) return null + if (!video) return false + + const isPlaylist = file.includes('.') === false + + if (isPlaylist) { + const p = video.getHLSPlaylist() + if (!p) return false + + const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id) + return !!redundancy + } + + const resolution = parseInt(file.split('-')[5], 10) + if (isNaN(resolution)) { + console.error('Cannot prune %s because we cannot guess guess the resolution.', file) + return true + } + + const videoFile = video.getWebTorrentFile(resolution) + if (!videoFile) { + console.error('Cannot find webtorrent file of video %s - %d', video.url, resolution) + return true + } - return result[0] + const redundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) + return !!redundancy } async function askConfirmation () { @@ -80,7 +151,9 @@ 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).' + + ' Notice PeerTube must have been stopped when your ran this script.' + + ' Can we delete these files?', default: 'n', required: true }