Improve prune script
This commit is contained in:
parent
07524e229f
commit
5ce1208a0a
|
@ -4,6 +4,7 @@ import { CONFIG } from '../server/initializers/constants'
|
||||||
import { VideoModel } from '../server/models/video/video'
|
import { VideoModel } from '../server/models/video/video'
|
||||||
import { initDatabaseModels } from '../server/initializers'
|
import { initDatabaseModels } from '../server/initializers'
|
||||||
import { remove, readdir } from 'fs-extra'
|
import { remove, readdir } from 'fs-extra'
|
||||||
|
import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
|
||||||
|
|
||||||
run()
|
run()
|
||||||
.then(() => process.exit(0))
|
.then(() => process.exit(0))
|
||||||
|
@ -15,16 +16,23 @@ run()
|
||||||
async function run () {
|
async function run () {
|
||||||
await initDatabaseModels(true)
|
await initDatabaseModels(true)
|
||||||
|
|
||||||
const storageToPrune = [
|
const storageOnlyOwnedToPrune = [
|
||||||
CONFIG.STORAGE.VIDEOS_DIR,
|
CONFIG.STORAGE.VIDEOS_DIR,
|
||||||
CONFIG.STORAGE.PREVIEWS_DIR,
|
|
||||||
CONFIG.STORAGE.THUMBNAILS_DIR,
|
|
||||||
CONFIG.STORAGE.TORRENTS_DIR
|
CONFIG.STORAGE.TORRENTS_DIR
|
||||||
]
|
]
|
||||||
|
|
||||||
|
const storageForAllToPrune = [
|
||||||
|
CONFIG.STORAGE.PREVIEWS_DIR,
|
||||||
|
CONFIG.STORAGE.THUMBNAILS_DIR
|
||||||
|
]
|
||||||
|
|
||||||
let toDelete: string[] = []
|
let toDelete: string[] = []
|
||||||
for (const directory of storageToPrune) {
|
for (const directory of storageOnlyOwnedToPrune) {
|
||||||
toDelete = toDelete.concat(await pruneDirectory(directory))
|
toDelete = toDelete.concat(await pruneDirectory(directory, true))
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const directory of storageForAllToPrune) {
|
||||||
|
toDelete = toDelete.concat(await pruneDirectory(directory, false))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (toDelete.length === 0) {
|
if (toDelete.length === 0) {
|
||||||
|
@ -48,17 +56,27 @@ async function run () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function pruneDirectory (directory: string) {
|
async function pruneDirectory (directory: string, onlyOwned = false) {
|
||||||
const files = await readdir(directory)
|
const files = await readdir(directory)
|
||||||
|
|
||||||
const toDelete: string[] = []
|
const toDelete: string[] = []
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const uuid = getUUIDFromFilename(file)
|
const uuid = getUUIDFromFilename(file)
|
||||||
let video: VideoModel
|
let video: VideoModel
|
||||||
|
let localRedundancy: boolean
|
||||||
|
|
||||||
if (uuid) video = await VideoModel.loadByUUIDWithFile(uuid)
|
if (uuid) {
|
||||||
|
video = await VideoModel.loadByUUIDWithFile(uuid)
|
||||||
|
localRedundancy = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid)
|
||||||
|
}
|
||||||
|
|
||||||
if (!uuid || !video) toDelete.push(join(directory, file))
|
if (
|
||||||
|
!uuid ||
|
||||||
|
!video ||
|
||||||
|
(onlyOwned === true && (video.isOwned() === false && localRedundancy === false))
|
||||||
|
) {
|
||||||
|
toDelete.push(join(directory, file))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return toDelete
|
return toDelete
|
||||||
|
@ -80,7 +98,8 @@ async function askConfirmation () {
|
||||||
properties: {
|
properties: {
|
||||||
confirm: {
|
confirm: {
|
||||||
type: 'string',
|
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).' +
|
||||||
|
' Can we delete these files?',
|
||||||
default: 'n',
|
default: 'n',
|
||||||
required: true
|
required: true
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,6 +146,38 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> {
|
||||||
return VideoRedundancyModel.findOne(query)
|
return VideoRedundancyModel.findOne(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async isLocalByVideoUUIDExists (uuid: string) {
|
||||||
|
const actor = await getServerActor()
|
||||||
|
|
||||||
|
const query = {
|
||||||
|
raw: true,
|
||||||
|
attributes: [ 'id' ],
|
||||||
|
where: {
|
||||||
|
actorId: actor.id
|
||||||
|
},
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
attributes: [ ],
|
||||||
|
model: VideoFileModel,
|
||||||
|
required: true,
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
attributes: [ ],
|
||||||
|
model: VideoModel,
|
||||||
|
required: true,
|
||||||
|
where: {
|
||||||
|
uuid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
return VideoRedundancyModel.findOne(query)
|
||||||
|
.then(r => !!r)
|
||||||
|
}
|
||||||
|
|
||||||
static async getVideoSample (p: Bluebird<VideoModel[]>) {
|
static async getVideoSample (p: Bluebird<VideoModel[]>) {
|
||||||
const rows = await p
|
const rows = await p
|
||||||
const ids = rows.map(r => r.id)
|
const ids = rows.map(r => r.id)
|
||||||
|
|
Loading…
Reference in New Issue