2018-05-29 04:11:52 -05:00
|
|
|
import * as prompt from 'prompt'
|
|
|
|
import { join } from 'path'
|
|
|
|
import { CONFIG } from '../server/initializers/constants'
|
|
|
|
import { VideoModel } from '../server/models/video/video'
|
|
|
|
import { initDatabaseModels } from '../server/initializers'
|
2018-08-27 09:23:34 -05:00
|
|
|
import { remove, readdir } from 'fs-extra'
|
2018-09-28 03:56:13 -05:00
|
|
|
import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
|
2018-10-08 09:26:04 -05:00
|
|
|
import { getUUIDFromFilename } from '../server/helpers/utils'
|
2018-05-29 04:11:52 -05:00
|
|
|
|
|
|
|
run()
|
|
|
|
.then(() => process.exit(0))
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(-1)
|
|
|
|
})
|
|
|
|
|
|
|
|
async function run () {
|
|
|
|
await initDatabaseModels(true)
|
|
|
|
|
2018-09-28 03:56:13 -05:00
|
|
|
const storageOnlyOwnedToPrune = [
|
2018-05-29 04:11:52 -05:00
|
|
|
CONFIG.STORAGE.VIDEOS_DIR,
|
|
|
|
CONFIG.STORAGE.TORRENTS_DIR
|
|
|
|
]
|
|
|
|
|
2018-09-28 03:56:13 -05:00
|
|
|
const storageForAllToPrune = [
|
|
|
|
CONFIG.STORAGE.PREVIEWS_DIR,
|
|
|
|
CONFIG.STORAGE.THUMBNAILS_DIR
|
|
|
|
]
|
|
|
|
|
2018-05-29 04:11:52 -05:00
|
|
|
let toDelete: string[] = []
|
2018-09-28 03:56:13 -05:00
|
|
|
for (const directory of storageOnlyOwnedToPrune) {
|
|
|
|
toDelete = toDelete.concat(await pruneDirectory(directory, true))
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const directory of storageForAllToPrune) {
|
|
|
|
toDelete = toDelete.concat(await pruneDirectory(directory, false))
|
2018-05-29 04:11:52 -05: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 09:23:34 -05:00
|
|
|
await remove(path)
|
2018-05-29 04:11:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log('Done!')
|
|
|
|
} else {
|
|
|
|
console.log('Exiting without deleting files.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 03:56:13 -05:00
|
|
|
async function pruneDirectory (directory: string, onlyOwned = false) {
|
2018-08-27 09:23:34 -05:00
|
|
|
const files = await readdir(directory)
|
2018-05-29 04:11:52 -05:00
|
|
|
|
|
|
|
const toDelete: string[] = []
|
|
|
|
for (const file of files) {
|
|
|
|
const uuid = getUUIDFromFilename(file)
|
|
|
|
let video: VideoModel
|
2018-09-28 03:56:13 -05:00
|
|
|
let localRedundancy: boolean
|
2018-05-29 04:11:52 -05:00
|
|
|
|
2018-09-28 03:56:13 -05:00
|
|
|
if (uuid) {
|
|
|
|
video = await VideoModel.loadByUUIDWithFile(uuid)
|
|
|
|
localRedundancy = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid)
|
|
|
|
}
|
2018-05-29 04:11:52 -05:00
|
|
|
|
2018-09-28 03:56:13 -05:00
|
|
|
if (
|
|
|
|
!uuid ||
|
|
|
|
!video ||
|
|
|
|
(onlyOwned === true && (video.isOwned() === false && localRedundancy === false))
|
|
|
|
) {
|
|
|
|
toDelete.push(join(directory, file))
|
|
|
|
}
|
2018-05-29 04:11:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return toDelete
|
|
|
|
}
|
|
|
|
|
|
|
|
async function askConfirmation () {
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
prompt.start()
|
|
|
|
const schema = {
|
|
|
|
properties: {
|
|
|
|
confirm: {
|
|
|
|
type: 'string',
|
2018-09-28 03:56:13 -05:00
|
|
|
description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
|
|
|
|
' Can we delete these files?',
|
2018-05-29 04:11:52 -05: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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|