diff --git a/server/docs.go b/server/docs.go index 0762e6a..3333514 100644 --- a/server/docs.go +++ b/server/docs.go @@ -3,11 +3,12 @@ package main import ( "context" "fmt" + "io" "log" + "mime" "net/http" "path/filepath" "strings" - "time" s3 "github.com/minio/minio-go/v7" ) @@ -75,18 +76,36 @@ func docsHandler(w http.ResponseWriter, r *http.Request) { var lastErr error for _, key := range candidates { - _, err := s3Client.StatObject(ctx, bucketName, key, s3.GetObjectOptions{}) + obj, err := s3Client.GetObject(ctx, bucketName, key, s3.GetObjectOptions{}) if err != nil { lastErr = err continue } + stat, err := obj.Stat() + if err != nil { + lastErr = err + _ = obj.Close() + continue + } - presigned, err := s3Client.PresignedGetObject(ctx, bucketName, key, 15*time.Minute, nil) - if err != nil { - lastErr = err - continue + ext := filepath.Ext(key) + ctype := mime.TypeByExtension(ext) + if ctype == "" { + if ext == ".html" || strings.HasSuffix(key, "index.html") { + ctype = "text/html; charset=utf-8" + } else { + ctype = "application/octet-stream" + } } - http.Redirect(w, r, presigned.String(), http.StatusFound) + + w.Header().Set("Content-Type", ctype) + w.Header().Set("Content-Length", fmt.Sprintf("%d", stat.Size)) + w.Header().Set("Last-Modified", stat.LastModified.Format(http.TimeFormat)) + + if _, err := io.Copy(w, obj); err != nil { + log.Printf("Error streaming object %s: %v", key, err) + } + _ = obj.Close() return } diff --git a/server/main.go b/server/main.go index b26c65e..1585f2d 100644 --- a/server/main.go +++ b/server/main.go @@ -24,7 +24,7 @@ var ( s3Prefix = os.Getenv("S3_PREFIX") ) -const VERSION = "0.0.4" +const VERSION = "0.0.5" func main() { if hostname == "" {