This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
model_file.go
64 lines (54 loc) · 1.48 KB
/
model_file.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
package main
import (
"encoding/json"
"net/http"
"os"
"github.com/boltdb/bolt"
)
/* File
The file model
====================================================================== */
type File struct {
Reference string `json:"reference"`
URL string `json:"url"`
LocalFile string `json:"localfile"`
Timestamp int64 `json:"timestamp"`
MaxAge int64 `json:"maxage"`
Revalidate bool `json:"revalidate"`
ETAG string `json:"etag"`
Header http.Header `json:"header"`
}
/* Register
Registers metadata of the retrieved file to the database.
====================================================================== */
func (f *File) Register() {
fileJSON, _ := json.Marshal(f)
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Cache"))
err := b.Put([]byte(f.Reference), fileJSON)
return err
})
}
/* Remove
Removes file and it's database entry.
====================================================================== */
func (f *File) Remove() {
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Cache"))
b.Delete([]byte(f.Reference))
return nil
})
os.Remove(f.LocalFile)
}
/* Retrieve
Retrieve file metadata from storage.
====================================================================== */
func (f *File) Retrieve(originURL string) {
refHash := createHash(originURL)
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Cache"))
v := b.Get([]byte(refHash))
json.Unmarshal(v, &f)
return nil
})
}