Skip to content

Commit 83ac2e4

Browse files
committed
Use shardedfilestore for image-proxy
1 parent 72b8a07 commit 83ac2e4

File tree

3 files changed

+126
-13
lines changed

3 files changed

+126
-13
lines changed

server/embed.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
"github.com/davecgh/go-spew/spew"
2222
"github.com/dyatlov/go-oembed/oembed"
2323
"github.com/gin-gonic/gin"
24-
"github.com/gregjones/httpcache/diskcache"
25-
"github.com/peterbourgon/diskv"
2624
"willnorris.com/go/imageproxy"
2725
)
2826

@@ -72,7 +70,7 @@ func (serv *UploadServer) registerEmbedHandlers(r *gin.Engine, cfg Config) error
7270
rg.GET("", handleEmbed)
7371

7472
// Attach imageproxy
75-
cache := diskCache(cfg.Embed.ImageCachePath, cfg.Embed.ImageCacheMaxSize)
73+
cache := serv.diskCache()
7674
imgProxy = imageproxy.NewProxy(nil, cache)
7775

7876
ic := r.Group("/image-cache/*id")
@@ -83,18 +81,20 @@ func (serv *UploadServer) registerEmbedHandlers(r *gin.Engine, cfg Config) error
8381
func handleImageCache(c *gin.Context) {
8482
r := c.Request
8583
r.URL.Path = strings.Replace(r.URL.Path, "/image-cache", "", -1)
86-
spew.Dump(r.URL.Path)
8784
imgProxy.ServeHTTP(c.Writer, c.Request)
85+
fmt.Println("------------------------------------------------------")
8886
}
8987

90-
func diskCache(path string, maxSize uint64) *diskcache.Cache {
91-
d := diskv.New(diskv.Options{
92-
BasePath: path,
93-
CacheSizeMax: maxSize,
94-
// For file "c0ffee", store file as "c0/ff/c0ffee"
95-
Transform: func(s string) []string { return []string{s[0:2], s[2:4]} },
96-
})
97-
return diskcache.NewWithDiskv(d)
88+
func (serv *UploadServer) diskCache() *ImageProxyCache {
89+
// d := diskv.New(diskv.Options{
90+
// BasePath: path,
91+
// CacheSizeMax: maxSize,
92+
// // For file "c0ffee", store file as "c0/ff/c0ffee"
93+
// Transform: func(s string) []string { return []string{s[0:2], s[2:4]} },
94+
// })
95+
// return diskcache.NewWithDiskv(d)
96+
d := NewImageProxyCache(serv.store, serv.log)
97+
return d
9898
}
9999

100100
func handleEmbed(c *gin.Context) {

server/image-proxy-cache.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

server/uploadserver.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package server
22

33
import (
4+
"context"
45
"net/http"
56
"sync"
67

@@ -18,6 +19,7 @@ import (
1819
type UploadServer struct {
1920
DBConn *db.DatabaseConnection
2021
Router *gin.Engine
22+
ctx *RunContext
2123

2224
cfg Config
2325
log *zerolog.Logger
@@ -109,7 +111,7 @@ func (serv *UploadServer) Shutdown() {
109111

110112
// wait for all requests to finish
111113
if serv.httpServer != nil {
112-
serv.httpServer.Shutdown(nil)
114+
serv.httpServer.Shutdown(context.TODO())
113115
}
114116

115117
// stop running FileStore GC cycles

0 commit comments

Comments
 (0)