PeerTube/scripts/prune-storage.ts

164 lines
4.9 KiB
TypeScript
Raw Normal View History

import { registerTSPaths } from '../server/helpers/register-ts-paths'
registerTSPaths()
2021-08-27 14:32:44 +02:00
import { start, get } from 'prompt'
2021-07-23 11:20:00 +02:00
import { join, basename } from 'path'
import { CONFIG } from '../server/initializers/config'
2018-05-29 11:11:52 +02:00
import { VideoModel } from '../server/models/video/video'
2020-05-07 14:58:24 +02:00
import { initDatabaseModels } from '../server/initializers/database'
2021-07-23 11:20:00 +02:00
import { readdir, remove, stat } from 'fs-extra'
2018-09-28 10:56:13 +02:00
import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
2021-08-27 14:32:44 +02:00
import { map } from 'bluebird'
import { getUUIDFromFilename } from '../server/helpers/utils'
2019-08-09 15:04:36 +02:00
import { ThumbnailModel } from '../server/models/video/thumbnail'
2021-05-11 11:15:29 +02:00
import { ActorImageModel } from '../server/models/actor/actor-image'
import { uniq, values } from 'lodash'
import { ThumbnailType } from '@shared/models'
2021-07-23 11:20:00 +02:00
import { VideoFileModel } from '@server/models/video/video-file'
2018-05-29 11:11:52 +02:00
run()
.then(() => process.exit(0))
.catch(err => {
console.error(err)
process.exit(-1)
})
async function run () {
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)
}
2018-05-29 11:11:52 +02:00
await initDatabaseModels(true)
2019-08-09 15:04:36 +02:00
let toDelete: string[] = []
2018-05-29 11:11:52 +02:00
2021-04-28 08:56:08 +02:00
console.log('Detecting files to remove, it could take a while...')
2019-08-09 15:04:36 +02:00
toDelete = toDelete.concat(
2021-07-23 11:20:00 +02:00
await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesWebTorrentFileExist()),
await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesTorrentFileExist()),
2018-09-28 10:56:13 +02:00
2019-08-09 15:04:36 +02:00
await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist),
2018-09-28 10:56:13 +02:00
await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true, ThumbnailType.PREVIEW)),
await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false, ThumbnailType.MINIATURE)),
2019-08-09 15:04:36 +02:00
2021-04-06 11:35:56 +02:00
await pruneDirectory(CONFIG.STORAGE.ACTOR_IMAGES, doesActorImageExist)
2019-08-09 15:04:36 +02:00
)
2018-05-29 11:11:52 +02:00
2018-12-06 09:46:22 +01:00
const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
2018-05-29 11:11:52 +02:00
if (toDelete.length === 0) {
console.log('No files to delete.')
return
}
console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
const res = await askConfirmation()
if (res === true) {
console.log('Processing delete...\n')
for (const path of toDelete) {
2018-08-27 16:23:34 +02:00
await remove(path)
2018-05-29 11:11:52 +02:00
}
console.log('Done!')
} else {
console.log('Exiting without deleting files.')
}
}
2019-08-09 15:04:36 +02:00
type ExistFun = (file: string) => Promise<boolean>
async function pruneDirectory (directory: string, existFun: ExistFun) {
2018-08-27 16:23:34 +02:00
const files = await readdir(directory)
2018-05-29 11:11:52 +02:00
const toDelete: string[] = []
2021-08-27 14:32:44 +02:00
await map(files, async file => {
2021-07-23 11:20:00 +02:00
const filePath = join(directory, file)
if (await existFun(filePath) !== true) {
toDelete.push(filePath)
2019-08-09 15:04:36 +02:00
}
}, { concurrency: 20 })
return toDelete
}
2021-07-23 11:20:00 +02:00
function doesWebTorrentFileExist () {
return (filePath: string) => VideoFileModel.doesOwnedWebTorrentVideoFileExist(basename(filePath))
}
2018-05-29 11:11:52 +02:00
2021-07-23 11:20:00 +02:00
function doesTorrentFileExist () {
return (filePath: string) => VideoFileModel.doesOwnedTorrentFileExist(basename(filePath))
2019-08-09 15:04:36 +02:00
}
2018-05-29 11:11:52 +02:00
function doesThumbnailExist (keepOnlyOwned: boolean, type: ThumbnailType) {
2021-07-23 11:20:00 +02:00
return async (filePath: string) => {
const thumbnail = await ThumbnailModel.loadByFilename(basename(filePath), type)
2019-08-09 15:04:36 +02:00
if (!thumbnail) return false
if (keepOnlyOwned) {
const video = await VideoModel.load(thumbnail.videoId)
if (video.isOwned() === false) return false
2018-09-28 10:56:13 +02:00
}
2019-08-09 15:04:36 +02:00
return true
2018-05-29 11:11:52 +02:00
}
2019-08-09 15:04:36 +02:00
}
2018-05-29 11:11:52 +02:00
2021-07-23 11:20:00 +02:00
async function doesActorImageExist (filePath: string) {
const image = await ActorImageModel.loadByName(basename(filePath))
2019-08-09 15:04:36 +02:00
2021-04-06 11:35:56 +02:00
return !!image
2019-08-09 15:04:36 +02:00
}
2021-07-23 11:20:00 +02:00
async function doesRedundancyExist (filePath: string) {
const isPlaylist = (await stat(filePath)).isDirectory()
2019-08-09 15:04:36 +02:00
if (isPlaylist) {
2021-07-23 11:20:00 +02:00
const uuid = getUUIDFromFilename(filePath)
const video = await VideoModel.loadWithFiles(uuid)
if (!video) return false
2019-08-09 15:04:36 +02:00
const p = video.getHLSPlaylist()
if (!p) return false
const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id)
return !!redundancy
}
2021-07-23 11:20:00 +02:00
const file = await VideoFileModel.loadByFilename(basename(filePath))
if (!file) return false
2019-08-09 15:04:36 +02:00
2021-07-23 11:20:00 +02:00
const redundancy = await VideoRedundancyModel.loadLocalByFileId(file.id)
2019-08-09 15:04:36 +02:00
return !!redundancy
2018-05-29 11:11:52 +02:00
}
async function askConfirmation () {
return new Promise((res, rej) => {
2021-08-27 14:32:44 +02:00
start()
2018-05-29 11:11:52 +02:00
const schema = {
properties: {
confirm: {
type: 'string',
2018-09-28 10:56:13 +02:00
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.' +
2018-09-28 10:56:13 +02:00
' Can we delete these files?',
2018-05-29 11:11:52 +02:00
default: 'n',
required: true
}
}
}
2021-08-27 14:32:44 +02:00
get(schema, function (err, result) {
2018-05-29 11:11:52 +02:00
if (err) return rej(err)
2020-07-30 14:54:31 +02:00
return res(result.confirm?.match(/y/) !== null)
2018-05-29 11:11:52 +02:00
})
})
}