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
/
controller_storage.go
133 lines (108 loc) · 3.13 KB
/
controller_storage.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"fmt"
"net/http"
"path/filepath"
"github.com/boltdb/bolt"
"time"
"encoding/json"
"gopkg.in/kyokomi/emoji.v1"
"os"
"io"
"bytes"
)
/* verifyAndRetrieveFile
Checks original source for the requested file and retrieves it when available.
====================================================================== */
func verifyAndRetrieveFile(localFile string, originURL string, w http.ResponseWriter, r *http.Request) {
// Retrieve and verify the Headers before moving forward
head, err := http.Head(originURL)
if err != nil {
fmt.Println("Error while retreiving Headers from", originURL, "\n-", err)
return
}
if head.StatusCode == http.StatusOK {
// Retrieve file from host
response, err := http.Get(originURL)
defer response.Body.Close()
var buf bytes.Buffer
tee := io.TeeReader(response.Body, &buf)
if err == nil {
for name := range head.Header {
w.Header().Set(name, head.Header.Get(name))
}
io.Copy(w, tee)
allowed := verifyContentType(head.Header)
ignore, revalidate, maxAge := defineCacheControl(head.Header)
if allowed == true && ignore != true {
// Create directories
fileDir := filepath.Dir(localFile)
if os.MkdirAll(fileDir, 0755) == nil {
// Prepare file for writing
output, err := os.Create(localFile)
if err != nil {
fmt.Println("Error while creating", fileDir, "\n-", err)
return
}
defer output.Close()
// Write data to file
n, err := io.Copy(output, &buf)
if err != nil {
fmt.Println("Error while writing", fileDir, "\n-", err)
return
}
relPath, _ := filepath.Rel(cacheDir, localFile)
emoji.Println("\t:truck: Retrieving file:", originURL, "\n\t:memo: Writing", n, "bytes to", relPath)
file := File{
Reference: createHash(originURL),
URL: originURL,
LocalFile: localFile,
Timestamp: time.Now().Unix(),
MaxAge: int64(maxAge),
Revalidate: revalidate,
ETAG: head.Header.Get("etag"),
Header: head.Header}
file.Register()
}
}
} else {
fmt.Println("Error while downloading", originURL, "\n-", err)
http.Redirect(w, r, originURL, 302)
}
}
}
/* validateCache
Checks whether the file is still valid based on saved cache rules.
====================================================================== */
func validateCache(originURL string) (valid bool) {
now := time.Now().Unix()
refHash := createHash(originURL)
valid = false
file := File{}
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Cache"))
v := b.Get([]byte(refHash))
json.Unmarshal(v, &file)
return nil
})
if now <= (file.Timestamp + file.MaxAge) {
valid = true
} else if file.Revalidate {
response, err := http.Head(originURL)
if err != nil {
fmt.Println("Error while retreiving Headers from", originURL, "\n-", err)
return
}
if response.Header.Get("etag") == file.ETAG {
valid = true
} else {
file.ETAG = response.Header.Get("etag")
}
}
if !valid {
relPath, _ := filepath.Rel(cacheDir, file.LocalFile)
emoji.Println(":anger: File no longer valid:", relPath)
file.Remove()
}
return valid
}