implmented download file

This commit is contained in:
Djeeberjr 2021-07-27 23:01:23 +02:00
parent f5a1bd77d1
commit 5746e79e8f

View File

@ -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)
}