From 5746e79e8f5e495a8f61cd754562bc77ecf428ab Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Tue, 27 Jul 2021 23:01:23 +0200 Subject: [PATCH] implmented download file --- internal/s3Broswer.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/internal/s3Broswer.go b/internal/s3Broswer.go index 76343ed..05651ee 100644 --- a/internal/s3Broswer.go +++ b/internal/s3Broswer.go @@ -2,8 +2,11 @@ package s3browser import ( "context" + "fmt" + "io" "log" "net/http" + "path/filepath" "github.com/graph-gophers/dataloader" "github.com/graphql-go/graphql" @@ -56,6 +59,30 @@ func initHttp(schema graphql.Schema, s3Client *minio.Client, loaderMap map[strin h.ContextHandler(resolveContext, rw, r) }) + http.HandleFunc("/api/file", func(rw http.ResponseWriter, r *http.Request) { + id := r.URL.Query().Get("id") + objInfo, err := s3Client.StatObject(context.Background(), bucketName, id, minio.GetObjectOptions{}) + + if err != nil { + rw.WriteHeader(500) + return + } + + obj, err := s3Client.GetObject(context.Background(), bucketName, id, minio.GetObjectOptions{}) + + if err != nil { + rw.WriteHeader(500) + return + } + + rw.Header().Set("Cache-Control", "no-store") + 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) + + io.Copy(rw, obj) + }) + http.ListenAndServe(":8080", nil) }