s3browser-backend/internal/httpServer.go

106 lines
2.7 KiB
Go
Raw Normal View History

2021-08-03 21:10:23 +00:00
package s3browser
import (
"context"
"fmt"
"io"
"mime"
"net/http"
"path/filepath"
2021-08-06 12:13:08 +00:00
"github.com/graph-gophers/dataloader"
2021-08-03 21:10:23 +00:00
"github.com/graphql-go/graphql"
"github.com/graphql-go/handler"
"github.com/minio/minio-go/v7"
)
// initHttp setup and start the http server. Blocking
2021-08-06 14:31:07 +00:00
func initHttp(resolveContext context.Context, schema graphql.Schema) error {
2021-08-03 21:10:23 +00:00
h := handler.New(&handler.Config{
Schema: &schema,
Pretty: true,
GraphiQL: false,
Playground: true,
})
http.HandleFunc("/graphql", func(rw http.ResponseWriter, r *http.Request) {
h.ContextHandler(resolveContext, rw, r)
})
http.HandleFunc("/api/file", func(rw http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
2021-08-06 12:13:08 +00:00
httpGetFile(resolveContext, rw, r)
2021-08-03 21:10:23 +00:00
return
}
if r.Method == "POST" {
2021-08-06 12:13:08 +00:00
httpPostFile(resolveContext, rw, r)
2021-08-03 21:10:23 +00:00
return
}
})
2021-08-06 14:31:07 +00:00
return http.ListenAndServe(":8080", nil)
2021-08-03 21:10:23 +00:00
}
2021-08-06 12:13:08 +00:00
func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
s3Client := ctx.Value("s3Client").(*minio.Client)
2021-08-03 21:10:23 +00:00
id := r.URL.Query().Get("id")
objInfo, err := s3Client.StatObject(context.Background(), bucketName, id, minio.GetObjectOptions{})
if err != nil {
2021-08-06 14:31:07 +00:00
rw.WriteHeader(http.StatusInternalServerError)
2021-08-03 21:10:23 +00:00
return
}
reqEtag := r.Header.Get("If-None-Match")
if reqEtag == objInfo.ETag {
2021-08-06 14:31:07 +00:00
rw.WriteHeader(http.StatusNotModified)
2021-08-03 21:10:23 +00:00
return
}
obj, err := s3Client.GetObject(context.Background(), bucketName, id, minio.GetObjectOptions{})
if err != nil {
2021-08-06 14:31:07 +00:00
rw.WriteHeader(http.StatusInternalServerError)
2021-08-03 21:10:23 +00:00
return
}
rw.Header().Set("Cache-Control", "must-revalidate")
rw.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filepath.Base((objInfo.Key))))
rw.Header().Set("Content-Type", objInfo.ContentType)
rw.Header().Set("ETag", objInfo.ETag)
2021-08-06 14:31:07 +00:00
_, err = io.Copy(rw, obj)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
2021-08-03 21:10:23 +00:00
}
2021-08-06 12:13:08 +00:00
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)
2021-08-03 21:10:23 +00:00
2021-08-06 12:13:08 +00:00
id := r.URL.Query().Get("id")
2021-08-06 11:49:00 +00:00
2021-08-03 21:10:23 +00:00
contentType := r.Header.Get("Content-Type")
2021-08-06 11:49:00 +00:00
mimeType, _, _ := mime.ParseMediaType(contentType)
2021-08-03 21:10:23 +00:00
2021-08-06 14:31:07 +00:00
info, err := s3Client.PutObject(context.Background(), bucketName, id, r.Body, r.ContentLength, minio.PutObjectOptions{
2021-08-06 11:49:00 +00:00
ContentType: mimeType,
})
2021-08-03 21:10:23 +00:00
2021-08-06 14:31:07 +00:00
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
2021-08-06 12:13:08 +00:00
// Invalidate cache
2021-08-06 14:31:07 +00:00
loader["getFile"].Clear(ctx, dataloader.StringKey(info.Key))
loader["listObjects"].Clear(ctx, dataloader.StringKey(info.Key))
loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(info.Key)))
2021-08-06 12:13:08 +00:00
2021-08-06 14:31:07 +00:00
rw.WriteHeader(http.StatusCreated)
2021-08-03 21:10:23 +00:00
}