quantum-inspiration / files / http-cache.go
http-cache.go
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | func getCachedUrl( |
| 13 | url string, |
| 14 | id string, |
| 15 | cacheFileTimeFormat string, |
| 16 | ) ([]byte, error) { |
| 17 | return getCachedUrlFunc(url, id, cacheFileTimeFormat, func(bytes []byte) ([]byte, error) { |
| 18 | return bytes, nil |
| 19 | }) |
| 20 | } |
| 21 | func getCachedUrlFunc( |
| 22 | url string, |
| 23 | id string, |
| 24 | cacheFileTimeFormat string, |
| 25 | byteProcessor func([]byte) ([]byte, error), |
| 26 | ) ([]byte, error) { |
| 27 | now := time.Now() |
| 28 | nowFormatted := now.Format(cacheFileTimeFormat) |
| 29 | cacheFile := fmt.Sprintf("cache/%s-%s.txt", id, nowFormatted) |
| 30 | dir := filepath.Dir(cacheFile) |
| 31 | _, err := os.Stat(dir) |
| 32 | dirExists := err == nil || !os.IsNotExist(err) |
| 33 | if !dirExists { |
| 34 | err = os.Mkdir(dir, 0755) |
| 35 | if err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | _, err = os.Stat(cacheFile) |
| 41 | fileExists := err == nil || !os.IsNotExist(err) |
| 42 | if fileExists { |
| 43 | f, err := os.Open(cacheFile) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | defer f.Close() |
| 48 | bytes, err := io.ReadAll(f) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | return bytes, nil |
| 53 | } else { |
| 54 | client := http.Client{} |
| 55 | |
| 56 | req, err := http.NewRequest("GET", url, nil) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | req.Header.Set("User-Agent", "QuantumInspirationBot/1.0 (https://apps.fuzzyworld.net/quantum-inspiration/)") |
| 61 | |
| 62 | resp, err := client.Do(req) |
| 63 | if err != nil { |
| 64 | return nil, err |
| 65 | } |
| 66 | defer resp.Body.Close() |
| 67 | |
| 68 | body, err := io.ReadAll(resp.Body) |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | |
| 73 | body, err = byteProcessor(body) |
| 74 | if err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | |
| 78 | f, err := os.Create(cacheFile) |
| 79 | if err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | defer f.Close() |
| 83 | _, err = f.Write(body) |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | |
| 88 | return body, nil |
| 89 | } |
| 90 | } |