1 import { map } from 'bluebird'
2 import { readdir, remove, stat } from 'fs-extra'
3 import { basename, join } from 'path'
4 import { get, start } from 'prompt'
5 import { DIRECTORIES } from '@server/initializers/constants'
6 import { VideoFileModel } from '@server/models/video/video-file'
7 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
8 import { uniqify } from '@shared/core-utils'
9 import { ThumbnailType } from '@shared/models'
10 import { getUUIDFromFilename } from '../server/helpers/utils'
11 import { CONFIG } from '../server/initializers/config'
12 import { initDatabaseModels } from '../server/initializers/database'
13 import { ActorImageModel } from '../server/models/actor/actor-image'
14 import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
15 import { ThumbnailModel } from '../server/models/video/thumbnail'
16 import { VideoModel } from '../server/models/video/video'
19 .then(() => process.exit(0))
25 async function run () {
26 const dirs = Object.values(CONFIG.STORAGE)
28 if (uniqify(dirs).length !== dirs.length) {
29 console.error('Cannot prune storage because you put multiple storage keys in the same directory.')
33 await initDatabaseModels(true)
35 let toDelete: string[] = []
37 console.log('Detecting files to remove, it could take a while...')
39 toDelete = toDelete.concat(
40 await pruneDirectory(DIRECTORIES.VIDEOS.PUBLIC, doesWebTorrentFileExist()),
41 await pruneDirectory(DIRECTORIES.VIDEOS.PRIVATE, doesWebTorrentFileExist()),
43 await pruneDirectory(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, doesHLSPlaylistExist()),
44 await pruneDirectory(DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, doesHLSPlaylistExist()),
46 await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesTorrentFileExist()),
48 await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist),
50 await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true, ThumbnailType.PREVIEW)),
51 await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false, ThumbnailType.MINIATURE)),
53 await pruneDirectory(CONFIG.STORAGE.ACTOR_IMAGES, doesActorImageExist)
56 const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
57 toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
59 if (toDelete.length === 0) {
60 console.log('No files to delete.')
64 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
66 const res = await askConfirmation()
68 console.log('Processing delete...\n')
70 for (const path of toDelete) {
76 console.log('Exiting without deleting files.')
80 type ExistFun = (file: string) => Promise<boolean> | boolean
81 async function pruneDirectory (directory: string, existFun: ExistFun) {
82 const files = await readdir(directory)
84 const toDelete: string[] = []
85 await map(files, async file => {
86 const filePath = join(directory, file)
88 if (await existFun(filePath) !== true) {
89 toDelete.push(filePath)
91 }, { concurrency: 20 })
96 function doesWebTorrentFileExist () {
97 return (filePath: string) => {
98 // Don't delete private directory
99 if (filePath === DIRECTORIES.VIDEOS.PRIVATE) return true
101 return VideoFileModel.doesOwnedWebTorrentVideoFileExist(basename(filePath))
105 function doesHLSPlaylistExist () {
106 return (hlsPath: string) => {
107 // Don't delete private directory
108 if (hlsPath === DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE) return true
110 return VideoStreamingPlaylistModel.doesOwnedHLSPlaylistExist(basename(hlsPath))
114 function doesTorrentFileExist () {
115 return (filePath: string) => VideoFileModel.doesOwnedTorrentFileExist(basename(filePath))
118 function doesThumbnailExist (keepOnlyOwned: boolean, type: ThumbnailType) {
119 return async (filePath: string) => {
120 const thumbnail = await ThumbnailModel.loadByFilename(basename(filePath), type)
121 if (!thumbnail) return false
124 const video = await VideoModel.load(thumbnail.videoId)
125 if (video.isOwned() === false) return false
132 async function doesActorImageExist (filePath: string) {
133 const image = await ActorImageModel.loadByName(basename(filePath))
138 async function doesRedundancyExist (filePath: string) {
139 const isPlaylist = (await stat(filePath)).isDirectory()
142 // Don't delete HLS redundancy directory
143 if (filePath === DIRECTORIES.HLS_REDUNDANCY) return true
145 const uuid = getUUIDFromFilename(filePath)
146 const video = await VideoModel.loadWithFiles(uuid)
147 if (!video) return false
149 const p = video.getHLSPlaylist()
152 const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id)
156 const file = await VideoFileModel.loadByFilename(basename(filePath))
157 if (!file) return false
159 const redundancy = await VideoRedundancyModel.loadLocalByFileId(file.id)
163 async function askConfirmation () {
164 return new Promise((res, rej) => {
170 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
171 ' Notice PeerTube must have been stopped when your ran this script.' +
172 ' Can we delete these files?',
178 get(schema, function (err, result) {
179 if (err) return rej(err)
181 return res(result.confirm?.match(/y/) !== null)