-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanifest.go
74 lines (59 loc) · 1.62 KB
/
manifest.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
package staticfiles
import (
"encoding/json"
"errors"
"io/ioutil"
"path/filepath"
)
// Manifest file name. It will be stored in the Storage.OutputDir directory.
const ManifestFilename string = "staticfiles.json"
const ManifestVersion int = 1
var ErrManifestVersionMismatch = errors.New("manifest version mismatch")
// Manifest contains mapping of the original relative file paths
// to the storage relative file paths.
type ManifestScheme struct {
Paths map[string]string `json:"paths"`
Version int `json:"version"`
}
func saveManifest(dir string, filesMap map[string]*StaticFile) error {
manifestPath := filepath.Join(dir, ManifestFilename)
manifest := ManifestScheme{
Paths: make(map[string]string),
Version: ManifestVersion,
}
for _, sf := range filesMap {
manifest.Paths[sf.RelPath] = sf.StorageRelPath
}
data, err := json.Marshal(manifest)
if err != nil {
return err
}
err = ioutil.WriteFile(manifestPath, data, 0644)
if err != nil {
return err
}
return err
}
func loadManifest(dir string) (map[string]*StaticFile, error) {
var manifest *ManifestScheme
filesMap := make(map[string]*StaticFile)
manifestPath := filepath.Join(dir, ManifestFilename)
data, err := ioutil.ReadFile(manifestPath)
if err != nil {
return filesMap, err
}
err = json.Unmarshal(data, &manifest)
if err != nil {
return filesMap, err
}
if manifest.Version != ManifestVersion {
return filesMap, ErrManifestVersionMismatch
}
for relPath, storageRelPath := range manifest.Paths {
filesMap[relPath] = &StaticFile{
RelPath: relPath,
StorageRelPath: storageRelPath,
}
}
return filesMap, nil
}