2018-05-29 11:11:52 +02:00
|
|
|
import * as prompt from 'prompt'
|
|
|
|
import { join } from 'path'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { CONFIG } from '../server/initializers/config'
|
2018-05-29 11:11:52 +02:00
|
|
|
import { VideoModel } from '../server/models/video/video'
|
|
|
|
import { initDatabaseModels } from '../server/initializers'
|
2019-08-09 15:04:36 +02:00
|
|
|
import { readdir, remove } from 'fs-extra'
|
2018-09-28 10:56:13 +02:00
|
|
|
import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
|
2019-08-09 15:04:36 +02:00
|
|
|
import * as Bluebird from 'bluebird'
|
2018-10-08 16:26:04 +02:00
|
|
|
import { getUUIDFromFilename } from '../server/helpers/utils'
|
2019-08-09 15:04:36 +02:00
|
|
|
import { ThumbnailModel } from '../server/models/video/thumbnail'
|
|
|
|
import { AvatarModel } from '../server/models/avatar/avatar'
|
2019-08-15 11:56:54 +02:00
|
|
|
import { uniq, values } from 'lodash'
|
2018-05-29 11:11:52 +02:00
|
|
|
|
|
|
|
run()
|
|
|
|
.then(() => process.exit(0))
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(-1)
|
|
|
|
})
|
|
|
|
|
|
|
|
async function run () {
|
2019-08-15 11:56:54 +02:00
|
|
|
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
|
|
|
|
2019-08-09 15:04:36 +02:00
|
|
|
toDelete = toDelete.concat(
|
|
|
|
await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesVideoExist(true)),
|
|
|
|
await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesVideoExist(true)),
|
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
|
|
|
|
2019-08-09 15:04:36 +02:00
|
|
|
await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true)),
|
|
|
|
await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false)),
|
|
|
|
|
|
|
|
await pruneDirectory(CONFIG.STORAGE.AVATARS_DIR, doesAvatarExist)
|
|
|
|
)
|
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-12-06 09:42:45 +01:00
|
|
|
|
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[] = []
|
2019-08-09 15:04:36 +02:00
|
|
|
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) => {
|
2018-05-29 11:11:52 +02:00
|
|
|
const uuid = getUUIDFromFilename(file)
|
2019-08-09 15:04:36 +02:00
|
|
|
const video = await VideoModel.loadByUUID(uuid)
|
2018-05-29 11:11:52 +02:00
|
|
|
|
2019-08-09 15:04:36 +02:00
|
|
|
return video && (keepOnlyOwned === false || video.isOwned())
|
|
|
|
}
|
|
|
|
}
|
2018-05-29 11:11:52 +02:00
|
|
|
|
2019-08-09 15:04:36 +02:00
|
|
|
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
|
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
|
|
|
|
2019-08-09 15:04:36 +02:00
|
|
|
async function doesAvatarExist (file: string) {
|
|
|
|
const avatar = await AvatarModel.loadByName(file)
|
|
|
|
|
|
|
|
return !!avatar
|
|
|
|
}
|
|
|
|
|
|
|
|
async function doesRedundancyExist (file: string) {
|
|
|
|
const uuid = getUUIDFromFilename(file)
|
|
|
|
const video = await VideoModel.loadWithFiles(uuid)
|
|
|
|
|
|
|
|
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.getFile(resolution)
|
|
|
|
if (!videoFile) {
|
|
|
|
console.error('Cannot find file of video %s - %d', video.url, resolution)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
const redundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
|
|
|
|
return !!redundancy
|
2018-05-29 11:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function askConfirmation () {
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
prompt.start()
|
|
|
|
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).' +
|
2018-12-05 17:44:36 +01:00
|
|
|
' 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prompt.get(schema, function (err, result) {
|
|
|
|
if (err) return rej(err)
|
|
|
|
return res(result.confirm && result.confirm.match(/y/) !== null)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|