file upload cache invalidation
This commit is contained in:
parent
e095a6ceb0
commit
acc9d6b5c3
@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/graph-gophers/dataloader"
|
||||||
"github.com/graphql-go/graphql"
|
"github.com/graphql-go/graphql"
|
||||||
"github.com/graphql-go/handler"
|
"github.com/graphql-go/handler"
|
||||||
"github.com/minio/minio-go/v7"
|
"github.com/minio/minio-go/v7"
|
||||||
@ -22,20 +23,18 @@ func initHttp(schema graphql.Schema, resolveContext context.Context) {
|
|||||||
Playground: true,
|
Playground: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
s3Client := resolveContext.Value("s3Client").(*minio.Client)
|
|
||||||
|
|
||||||
http.HandleFunc("/graphql", func(rw http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/graphql", func(rw http.ResponseWriter, r *http.Request) {
|
||||||
h.ContextHandler(resolveContext, rw, r)
|
h.ContextHandler(resolveContext, rw, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
http.HandleFunc("/api/file", func(rw http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/api/file", func(rw http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method == "GET" {
|
||||||
httpGetFile(rw, r, s3Client)
|
httpGetFile(resolveContext, rw, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Method == "POST" {
|
if r.Method == "POST" {
|
||||||
httpPostFile(rw, r, s3Client)
|
httpPostFile(resolveContext, rw, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -43,7 +42,8 @@ func initHttp(schema graphql.Schema, resolveContext context.Context) {
|
|||||||
http.ListenAndServe(":8080", nil)
|
http.ListenAndServe(":8080", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpGetFile(rw http.ResponseWriter, r *http.Request, s3Client *minio.Client) {
|
func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
|
||||||
|
s3Client := ctx.Value("s3Client").(*minio.Client)
|
||||||
id := r.URL.Query().Get("id")
|
id := r.URL.Query().Get("id")
|
||||||
objInfo, err := s3Client.StatObject(context.Background(), bucketName, id, minio.GetObjectOptions{})
|
objInfo, err := s3Client.StatObject(context.Background(), bucketName, id, minio.GetObjectOptions{})
|
||||||
|
|
||||||
@ -73,15 +73,22 @@ func httpGetFile(rw http.ResponseWriter, r *http.Request, s3Client *minio.Client
|
|||||||
io.Copy(rw, obj)
|
io.Copy(rw, obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpPostFile(rw http.ResponseWriter, r *http.Request, s3Client *minio.Client) {
|
func httpPostFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
|
||||||
|
s3Client := ctx.Value("s3Client").(*minio.Client)
|
||||||
|
loader := ctx.Value("loader").(map[string]*dataloader.Loader)
|
||||||
|
|
||||||
filename := r.URL.Query().Get("id")
|
id := r.URL.Query().Get("id")
|
||||||
|
|
||||||
contentType := r.Header.Get("Content-Type")
|
contentType := r.Header.Get("Content-Type")
|
||||||
mimeType, _, _ := mime.ParseMediaType(contentType)
|
mimeType, _, _ := mime.ParseMediaType(contentType)
|
||||||
|
|
||||||
s3Client.PutObject(context.Background(), bucketName, filename, r.Body, r.ContentLength, minio.PutObjectOptions{
|
s3Client.PutObject(context.Background(), bucketName, id, r.Body, r.ContentLength, minio.PutObjectOptions{
|
||||||
ContentType: mimeType,
|
ContentType: mimeType,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Invalidate cache
|
||||||
|
loader["getFile"].Clear(ctx, dataloader.StringKey(id))
|
||||||
|
loader["listObjects"].Clear(ctx, dataloader.StringKey(id))
|
||||||
|
loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(id)))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user