]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/prune-storage.ts
Remove wrong redundancy test
[github/Chocobozzz/PeerTube.git] / scripts / prune-storage.ts
CommitLineData
a9729e21 1import * as prompt from 'prompt'
a9729e21 2import { join } from 'path'
a9729e21
C
3import { CONFIG } from '../server/initializers/constants'
4import { VideoModel } from '../server/models/video/video'
5import { initDatabaseModels } from '../server/initializers'
62689b94 6import { remove, readdir } from 'fs-extra'
5ce1208a 7import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
edb4ffc7 8import { getUUIDFromFilename } from '../server/helpers/utils'
a9729e21
C
9
10run()
11 .then(() => process.exit(0))
12 .catch(err => {
13 console.error(err)
14 process.exit(-1)
15 })
16
17async function run () {
18 await initDatabaseModels(true)
19
5ce1208a 20 const storageOnlyOwnedToPrune = [
a9729e21 21 CONFIG.STORAGE.VIDEOS_DIR,
a9729e21
C
22 CONFIG.STORAGE.TORRENTS_DIR
23 ]
24
5ce1208a
C
25 const storageForAllToPrune = [
26 CONFIG.STORAGE.PREVIEWS_DIR,
27 CONFIG.STORAGE.THUMBNAILS_DIR
28 ]
29
a9729e21 30 let toDelete: string[] = []
5ce1208a
C
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))
a9729e21
C
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) {
62689b94 51 await remove(path)
a9729e21
C
52 }
53
54 console.log('Done!')
55 } else {
56 console.log('Exiting without deleting files.')
57 }
58}
59
5ce1208a 60async function pruneDirectory (directory: string, onlyOwned = false) {
62689b94 61 const files = await readdir(directory)
a9729e21
C
62
63 const toDelete: string[] = []
64 for (const file of files) {
65 const uuid = getUUIDFromFilename(file)
66 let video: VideoModel
5ce1208a 67 let localRedundancy: boolean
a9729e21 68
5ce1208a
C
69 if (uuid) {
70 video = await VideoModel.loadByUUIDWithFile(uuid)
71 localRedundancy = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid)
72 }
a9729e21 73
5ce1208a
C
74 if (
75 !uuid ||
76 !video ||
77 (onlyOwned === true && (video.isOwned() === false && localRedundancy === false))
78 ) {
79 toDelete.push(join(directory, file))
80 }
a9729e21
C
81 }
82
83 return toDelete
84}
85
a9729e21
C
86async function askConfirmation () {
87 return new Promise((res, rej) => {
88 prompt.start()
89 const schema = {
90 properties: {
91 confirm: {
92 type: 'string',
5ce1208a
C
93 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
94 ' Can we delete these files?',
a9729e21
C
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}