X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=scripts%2Fprune-storage.ts;h=d19594a60ed45122fb497e83344422a44cd43b5f;hb=92a6e85fe7679b47bb61f7ff6bc757c7ee75a440;hp=5b029d215a703de61bfad4fe6cee4d9894394692;hpb=764b1a14fc494f2cfd7ea590d2f07b01df65c7ad;p=github%2FChocobozzz%2FPeerTube.git diff --git a/scripts/prune-storage.ts b/scripts/prune-storage.ts index 5b029d215..d19594a60 100755 --- a/scripts/prune-storage.ts +++ b/scripts/prune-storage.ts @@ -1,20 +1,19 @@ -import { registerTSPaths } from '../server/helpers/register-ts-paths' -registerTSPaths() - -import * as prompt from 'prompt' -import { join, basename } from 'path' +import { map } from 'bluebird' +import { readdir, remove, stat } from 'fs-extra' +import { basename, join } from 'path' +import { get, start } from 'prompt' +import { DIRECTORIES } from '@server/initializers/constants' +import { VideoFileModel } from '@server/models/video/video-file' +import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' +import { uniqify } from '@shared/core-utils' +import { ThumbnailType } from '@shared/models' +import { getUUIDFromFilename } from '../server/helpers/utils' import { CONFIG } from '../server/initializers/config' -import { VideoModel } from '../server/models/video/video' import { initDatabaseModels } from '../server/initializers/database' -import { readdir, remove, stat } from 'fs-extra' +import { ActorImageModel } from '../server/models/actor/actor-image' 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 { ActorImageModel } from '../server/models/actor/actor-image' -import { uniq, values } from 'lodash' -import { ThumbnailType } from '@shared/models' -import { VideoFileModel } from '@server/models/video/video-file' +import { VideoModel } from '../server/models/video/video' run() .then(() => process.exit(0)) @@ -24,9 +23,9 @@ run() }) async function run () { - const dirs = values(CONFIG.STORAGE) + const dirs = Object.values(CONFIG.STORAGE) - if (uniq(dirs).length !== dirs.length) { + if (uniqify(dirs).length !== dirs.length) { console.error('Cannot prune storage because you put multiple storage keys in the same directory.') process.exit(0) } @@ -38,7 +37,12 @@ async function run () { console.log('Detecting files to remove, it could take a while...') toDelete = toDelete.concat( - await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesWebTorrentFileExist()), + await pruneDirectory(DIRECTORIES.VIDEOS.PUBLIC, doesWebTorrentFileExist()), + await pruneDirectory(DIRECTORIES.VIDEOS.PRIVATE, doesWebTorrentFileExist()), + + await pruneDirectory(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, doesHLSPlaylistExist()), + await pruneDirectory(DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, doesHLSPlaylistExist()), + await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesTorrentFileExist()), await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist), @@ -73,12 +77,12 @@ async function run () { } } -type ExistFun = (file: string) => Promise +type ExistFun = (file: string) => Promise | boolean async function pruneDirectory (directory: string, existFun: ExistFun) { const files = await readdir(directory) const toDelete: string[] = [] - await Bluebird.map(files, async file => { + await map(files, async file => { const filePath = join(directory, file) if (await existFun(filePath) !== true) { @@ -90,7 +94,21 @@ async function pruneDirectory (directory: string, existFun: ExistFun) { } function doesWebTorrentFileExist () { - return (filePath: string) => VideoFileModel.doesOwnedWebTorrentVideoFileExist(basename(filePath)) + return (filePath: string) => { + // Don't delete private directory + if (filePath === DIRECTORIES.VIDEOS.PRIVATE) return true + + return VideoFileModel.doesOwnedWebTorrentVideoFileExist(basename(filePath)) + } +} + +function doesHLSPlaylistExist () { + return (hlsPath: string) => { + // Don't delete private directory + if (hlsPath === DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE) return true + + return VideoStreamingPlaylistModel.doesOwnedHLSPlaylistExist(basename(hlsPath)) + } } function doesTorrentFileExist () { @@ -121,6 +139,9 @@ async function doesRedundancyExist (filePath: string) { const isPlaylist = (await stat(filePath)).isDirectory() if (isPlaylist) { + // Don't delete HLS redundancy directory + if (filePath === DIRECTORIES.HLS_REDUNDANCY) return true + const uuid = getUUIDFromFilename(filePath) const video = await VideoModel.loadWithFiles(uuid) if (!video) return false @@ -141,7 +162,7 @@ async function doesRedundancyExist (filePath: string) { async function askConfirmation () { return new Promise((res, rej) => { - prompt.start() + start() const schema = { properties: { confirm: { @@ -154,7 +175,7 @@ async function askConfirmation () { } } } - prompt.get(schema, function (err, result) { + get(schema, function (err, result) { if (err) return rej(err) return res(result.confirm?.match(/y/) !== null)