-
Notifications
You must be signed in to change notification settings - Fork 0
/
recover_assets.go
74 lines (57 loc) · 1.5 KB
/
recover_assets.go
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
// Recover assets:
// - Recovers non-indexed media assets
package main
import (
"encoding/hex"
"fmt"
"os"
"path"
)
// Recovers non-indexed media assets
// vault - Reference to the vault to handle
func RecoverVaultAssets(vault *Vault) {
// Get the full list of media assets in the vault, and check if they exists in the main index
index, err := vault.index.StartWrite()
if err != nil {
vault.index.CancelWrite(index)
LogError(err)
os.Exit(1)
}
dirs, err := os.ReadDir(path.Join(vault.path, "media"))
if err == nil {
for i := 0; i < len(dirs); i++ {
if dirs[i].Type().IsDir() {
media_ids := fetchMediaIds(path.Join(vault.path, "media", dirs[i].Name()))
for j := 0; j < len(media_ids); j++ {
media_id := media_ids[j]
prefixByte := byte(media_id % 256)
prefixByteHex := hex.EncodeToString([]byte{prefixByte})
if dirs[i].Name() != prefixByteHex {
continue
}
exists, _, err := index.file.BinarySearch(media_id)
if err != nil {
vault.index.CancelWrite(index)
LogError(err)
os.Exit(1)
}
if !exists {
// Remove directory
LogInfo("Found missing asset: Media folder not indexed 'media/" + dirs[i].Name() + "/" + fmt.Sprint(media_id) + "' (adding)")
_, _, err = index.file.AddValue(media_id)
if err != nil {
vault.index.CancelWrite(index)
LogError(err)
os.Exit(1)
}
}
}
}
}
}
err = vault.index.EndWrite(index)
if err != nil {
LogError(err)
os.Exit(1)
}
}