Skip to content
Snippets Groups Projects
Commit a2941710 authored by Valera Shaitorov's avatar Valera Shaitorov :alien:
Browse files

Перенесен пропущенный transport/http/ в Files/SDK

parent 547f79f0
No related tags found
No related merge requests found
package transporthttp
import (
"context"
"net/http"
"git.perx.ru/perxis/perxis-go/pkg/files"
"git.perx.ru/perxis/perxis-go/pkg/urlsigner"
"github.com/gorilla/mux"
)
func NewHTTPHandler(fs files.Files, s urlsigner.URLSigner) http.Handler {
m := mux.NewRouter()
m.Methods("GET").
Path("/{id}").
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !s.Check(r.URL) {
w.WriteHeader(http.StatusForbidden)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
f := &files.File{
ID: mux.Vars(r)["id"],
}
res, err := fs.GetFile(context.Background(), f)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
if res == nil || res.URL == "" {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Location", res.URL)
w.WriteHeader(http.StatusMovedPermanently)
})
return m
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment