]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/prune-storage.ts
add parameters to GET /videos route spec
[github/Chocobozzz/PeerTube.git] / scripts / prune-storage.ts
1 import * as prompt from 'prompt'
2 import { join } from 'path'
3 import { CONFIG } from '../server/initializers/constants'
4 import { VideoModel } from '../server/models/video/video'
5 import { initDatabaseModels } from '../server/initializers'
6 import { remove, readdir } from 'fs-extra'
7 import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
8 import { getUUIDFromFilename } from '../server/helpers/utils'
9
10 run()
11 .then(() => process.exit(0))
12 .catch(err => {
13 console.error(err)
14 process.exit(-1)
15 })
16
17 async function run () {
18 await initDatabaseModels(true)
19
20 const storageOnlyOwnedToPrune = [
21 CONFIG.STORAGE.VIDEOS_DIR,
22 CONFIG.STORAGE.TORRENTS_DIR
23 ]
24
25 const storageForAllToPrune = [
26 CONFIG.STORAGE.PREVIEWS_DIR,
27 CONFIG.STORAGE.THUMBNAILS_DIR
28 ]
29
30 let toDelete: string[] = []
31 for (const directory of storageOnlyOwnedToPrune) {
32 toDelete = toDelete.concat(await pruneDirectory(directory, true))
33 }
34
35 for (const directory of storageForAllToPrune) {
36 toDelete = toDelete.concat(await pruneDirectory(directory, false))
37 }
38
39 if (toDelete.length === 0) {
40 console.log('No files to delete.')
41 return
42 }
43
44 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
45
46 const res = await askConfirmation()
47 if (res === true) {
48 console.log('Processing delete...\n')
49
50 for (const path of toDelete) {
51 await remove(path)
52 }
53
54 console.log('Done!')
55 } else {
56 console.log('Exiting without deleting files.')
57 }
58 }
59
60 async function pruneDirectory (directory: string, onlyOwned = false) {
61 const files = await readdir(directory)
62
63 const toDelete: string[] = []
64 for (const file of files) {
65 const uuid = getUUIDFromFilename(file)
66 let video: VideoModel
67 let localRedundancy: boolean
68
69 if (uuid) {
70 video = await VideoModel.loadByUUIDWithFile(uuid)
71 localRedundancy = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid)
72 }
73
74 if (
75 !uuid ||
76 !video ||
77 (onlyOwned === true && (video.isOwned() === false && localRedundancy === false))
78 ) {
79 toDelete.push(join(directory, file))
80 }
81 }
82
83 return toDelete
84 }
85
86 async function askConfirmation () {
87 return new Promise((res, rej) => {
88 prompt.start()
89 const schema = {
90 properties: {
91 confirm: {
92 type: 'string',
93 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
94 ' Can we delete these files?',
95 default: 'n',
96 required: true
97 }
98 }
99 }
100 prompt.get(schema, function (err, result) {
101 if (err) return rej(err)
102 return res(result.confirm && result.confirm.match(/y/) !== null)
103 })
104 })
105 }