aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/prune-storage.ts
blob: 4953a74399eb47a7239646bc5fee59b798149cca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import * as prompt from 'prompt'
import { join } from 'path'
import { CONFIG } from '../server/initializers/config'
import { VideoModel } from '../server/models/video/video'
import { initDatabaseModels } from '../server/initializers'
import { remove, readdir } from 'fs-extra'
import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
import { getUUIDFromFilename } from '../server/helpers/utils'

run()
  .then(() => process.exit(0))
  .catch(err => {
    console.error(err)
    process.exit(-1)
  })

async function run () {
  await initDatabaseModels(true)

  const storageOnlyOwnedToPrune = [
    CONFIG.STORAGE.VIDEOS_DIR,
    CONFIG.STORAGE.TORRENTS_DIR,
    CONFIG.STORAGE.REDUNDANCY_DIR
  ]

  const storageForAllToPrune = [
    CONFIG.STORAGE.PREVIEWS_DIR,
    CONFIG.STORAGE.THUMBNAILS_DIR
  ]

  let toDelete: string[] = []
  for (const directory of storageOnlyOwnedToPrune) {
    toDelete = toDelete.concat(await pruneDirectory(directory, true))
  }

  for (const directory of storageForAllToPrune) {
    toDelete = toDelete.concat(await pruneDirectory(directory, false))
  }

  const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
  toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))

  if (toDelete.length === 0) {
    console.log('No files to delete.')
    return
  }

  console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))

  const res = await askConfirmation()
  if (res === true) {
    console.log('Processing delete...\n')

    for (const path of toDelete) {
      await remove(path)
    }

    console.log('Done!')
  } else {
    console.log('Exiting without deleting files.')
  }
}

async function pruneDirectory (directory: string, onlyOwned = false) {
  const files = await readdir(directory)

  const toDelete: string[] = []
  for (const file of files) {
    const uuid = getUUIDFromFilename(file)
    let video: VideoModel
    let localRedundancy: boolean

    if (uuid) {
      video = await VideoModel.loadByUUIDWithFile(uuid)
      localRedundancy = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid)
    }

    if (
      !uuid ||
      !video ||
      (onlyOwned === true && (video.isOwned() === false && localRedundancy === false))
    ) {
      toDelete.push(join(directory, file))
    }
  }

  return toDelete
}

async function askConfirmation () {
  return new Promise((res, rej) => {
    prompt.start()
    const schema = {
      properties: {
        confirm: {
          type: 'string',
          description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
            ' Notice PeerTube must have been stopped when your ran this script.' +
            ' Can we delete these files?',
          default: 'n',
          required: true
        }
      }
    }
    prompt.get(schema, function (err, result) {
      if (err) return rej(err)
      return res(result.confirm && result.confirm.match(/y/) !== null)
    })
  })
}