]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/prune-storage.ts
Add hls support on server
[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,
3ba862da
C
22 CONFIG.STORAGE.TORRENTS_DIR,
23 CONFIG.STORAGE.REDUNDANCY_DIR
a9729e21
C
24 ]
25
5ce1208a
C
26 const storageForAllToPrune = [
27 CONFIG.STORAGE.PREVIEWS_DIR,
28 CONFIG.STORAGE.THUMBNAILS_DIR
29 ]
30
a9729e21 31 let toDelete: string[] = []
5ce1208a
C
32 for (const directory of storageOnlyOwnedToPrune) {
33 toDelete = toDelete.concat(await pruneDirectory(directory, true))
34 }
35
36 for (const directory of storageForAllToPrune) {
37 toDelete = toDelete.concat(await pruneDirectory(directory, false))
a9729e21
C
38 }
39
be9727bd
C
40 const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
41 toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
3ba862da 42
a9729e21
C
43 if (toDelete.length === 0) {
44 console.log('No files to delete.')
45 return
46 }
47
48 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
49
50 const res = await askConfirmation()
51 if (res === true) {
52 console.log('Processing delete...\n')
53
54 for (const path of toDelete) {
62689b94 55 await remove(path)
a9729e21
C
56 }
57
58 console.log('Done!')
59 } else {
60 console.log('Exiting without deleting files.')
61 }
62}
63
5ce1208a 64async function pruneDirectory (directory: string, onlyOwned = false) {
62689b94 65 const files = await readdir(directory)
a9729e21
C
66
67 const toDelete: string[] = []
68 for (const file of files) {
69 const uuid = getUUIDFromFilename(file)
70 let video: VideoModel
5ce1208a 71 let localRedundancy: boolean
a9729e21 72
5ce1208a
C
73 if (uuid) {
74 video = await VideoModel.loadByUUIDWithFile(uuid)
75 localRedundancy = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid)
76 }
a9729e21 77
5ce1208a
C
78 if (
79 !uuid ||
80 !video ||
81 (onlyOwned === true && (video.isOwned() === false && localRedundancy === false))
82 ) {
83 toDelete.push(join(directory, file))
84 }
a9729e21
C
85 }
86
87 return toDelete
88}
89
a9729e21
C
90async function askConfirmation () {
91 return new Promise((res, rej) => {
92 prompt.start()
93 const schema = {
94 properties: {
95 confirm: {
96 type: 'string',
5ce1208a 97 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
7089e7b4 98 ' Notice PeerTube must have been stopped when your ran this script.' +
5ce1208a 99 ' Can we delete these files?',
a9729e21
C
100 default: 'n',
101 required: true
102 }
103 }
104 }
105 prompt.get(schema, function (err, result) {
106 if (err) return rej(err)
107 return res(result.confirm && result.confirm.match(/y/) !== null)
108 })
109 })
110}