Skip to content

Commit

Permalink
Add mime type to google-storage-proxy to support http responses (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
knjoroge authored Nov 15, 2024
1 parent 400b804 commit 122385f
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions proxy/http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package http_cache

import (
"bufio"
"cloud.google.com/go/storage"
"context"
"fmt"
"log"
"mime"
"net"
"net/http"
"path/filepath"

"cloud.google.com/go/storage"
)

type StorageProxy struct {
Expand Down Expand Up @@ -45,14 +48,15 @@ func (proxy StorageProxy) handler(w http.ResponseWriter, r *http.Request) {
if key[0] == '/' {
key = key[1:]
}
if r.Method == "GET" {
switch r.Method {
case "GET":
proxy.downloadBlob(w, key)
} else if r.Method == "HEAD" {
case "HEAD":
proxy.checkBlobExists(w, key)
} else if r.Method == "POST" {
proxy.uploadBlob(w, r, key)
} else if r.Method == "PUT" {
case "POST", "PUT":
proxy.uploadBlob(w, r, key)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

Expand All @@ -68,6 +72,15 @@ func (proxy StorageProxy) downloadBlob(w http.ResponseWriter, name string) {
return
}
defer reader.Close()

// Determine MIME type
ext := filepath.Ext(name)
mimeType := mime.TypeByExtension(ext)
if mimeType == "" {
mimeType = "application/octet-stream"
}
w.Header().Set("Content-Type", mimeType)

bufferedReader := bufio.NewReader(reader)
_, err = bufferedReader.WriteTo(w)
if err != nil {
Expand Down

0 comments on commit 122385f

Please sign in to comment.