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