|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "github.com/davecgh/go-spew/spew" |
| 9 | + "github.com/kiwiirc/plugin-fileuploader/shardedfilestore" |
| 10 | + "github.com/rs/zerolog" |
| 11 | + "github.com/tus/tusd" |
| 12 | +) |
| 13 | + |
| 14 | +// ImageProxyCache is an implementation of httpcache.Cache that supplements the in-memory map with persistent storage |
| 15 | +type ImageProxyCache struct { |
| 16 | + store *shardedfilestore.ShardedFileStore |
| 17 | + log *zerolog.Logger |
| 18 | + urlMap sync.Map |
| 19 | +} |
| 20 | + |
| 21 | +// Get returns the response corresponding to key if present |
| 22 | +func (c *ImageProxyCache) Get(key string) (resp []byte, ok bool) { |
| 23 | + urlHash := getHash(key) |
| 24 | + spew.Dump("Get", key, urlHash) |
| 25 | + |
| 26 | + idInterface, ok := c.urlMap.Load(urlHash) |
| 27 | + if !ok { |
| 28 | + fmt.Println("Not in map") |
| 29 | + fmt.Println("") |
| 30 | + return []byte{}, false |
| 31 | + } |
| 32 | + id := idInterface.(string) |
| 33 | + |
| 34 | + reader, err := c.store.GetReader(id) |
| 35 | + if err != nil { |
| 36 | + fmt.Println("No reader") |
| 37 | + fmt.Println("") |
| 38 | + c.urlMap.Delete(urlHash) |
| 39 | + return []byte{}, false |
| 40 | + } |
| 41 | + |
| 42 | + buffer := new(bytes.Buffer) |
| 43 | + _, err = buffer.ReadFrom(reader) |
| 44 | + if err != nil { |
| 45 | + fmt.Println("Failed read") |
| 46 | + fmt.Println("") |
| 47 | + c.urlMap.Delete(urlHash) |
| 48 | + return []byte{}, false |
| 49 | + } |
| 50 | + |
| 51 | + bytes := buffer.Bytes() |
| 52 | + |
| 53 | + spew.Dump(len(bytes)) |
| 54 | + fmt.Println("Got from cache") |
| 55 | + fmt.Println("") |
| 56 | + return bytes, true |
| 57 | +} |
| 58 | + |
| 59 | +// Set saves a response to the cache as key |
| 60 | +func (c *ImageProxyCache) Set(key string, resp []byte) { |
| 61 | + urlHash := getHash(key) |
| 62 | + spew.Dump("Set", key, len(resp), urlHash) |
| 63 | + |
| 64 | + metaData := tusd.MetaData{ |
| 65 | + "Url": key, |
| 66 | + } |
| 67 | + fileInfo := tusd.FileInfo{ |
| 68 | + Size: int64(len(resp)), |
| 69 | + SizeIsDeferred: false, |
| 70 | + MetaData: metaData, |
| 71 | + IsFinal: false, |
| 72 | + } |
| 73 | + |
| 74 | + id, err := c.store.NewUpload(fileInfo) |
| 75 | + if err != nil { |
| 76 | + c.log.Error(). |
| 77 | + Err(err). |
| 78 | + Msg("Failed to create new upload") |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + _, err = c.store.WriteChunk(id, 0, bytes.NewReader(resp)) |
| 83 | + if err != nil { |
| 84 | + c.log.Error(). |
| 85 | + Err(err). |
| 86 | + Msg("Failed to write chunk") |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + c.store.FinishUpload(id) |
| 91 | + c.urlMap.Store(urlHash, id) |
| 92 | + |
| 93 | + fmt.Println("Set in cache") |
| 94 | + fmt.Println("") |
| 95 | +} |
| 96 | + |
| 97 | +// Delete removes the response with key from the cache |
| 98 | +func (c *ImageProxyCache) Delete(key string) { |
| 99 | + urlHash := getHash(key) |
| 100 | + c.urlMap.Delete(urlHash) |
| 101 | + spew.Dump("Delete", key) |
| 102 | + fmt.Println("") |
| 103 | +} |
| 104 | + |
| 105 | +// NewImageProxyCache returns a new Cache that will store files in basePath |
| 106 | +func NewImageProxyCache(store *shardedfilestore.ShardedFileStore, log *zerolog.Logger) *ImageProxyCache { |
| 107 | + return &ImageProxyCache{ |
| 108 | + store: store, |
| 109 | + log: log, |
| 110 | + } |
| 111 | +} |
0 commit comments