mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-20 03:20:44 -06:00
refactoring: implement review feedback
This commit is contained in:
@@ -42,7 +42,7 @@ func HealthWithConfig(cfg *config.Config) []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "debug-addr",
|
||||
Value: "0.0.0.0:9114",
|
||||
Value: "0.0.0.0:9189",
|
||||
Usage: "Address to debug endpoint",
|
||||
EnvVars: []string{"THUMBNAILS_DEBUG_ADDR"},
|
||||
Destination: &cfg.Debug.Addr,
|
||||
@@ -89,7 +89,7 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "debug-addr",
|
||||
Value: "0.0.0.0:9194",
|
||||
Value: "0.0.0.0:9189",
|
||||
Usage: "Address to bind debug server",
|
||||
EnvVars: []string{"THUMBNAILS_DEBUG_ADDR"},
|
||||
Destination: &cfg.Debug.Addr,
|
||||
@@ -115,7 +115,7 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "http-addr",
|
||||
Value: "0.0.0.0:9190",
|
||||
Value: "0.0.0.0:9185",
|
||||
Usage: "Address to bind http server",
|
||||
EnvVars: []string{"THUMBNAILS_HTTP_ADDR"},
|
||||
Destination: &cfg.HTTP.Addr,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -24,7 +25,7 @@ func NewService(opts ...Option) Service {
|
||||
m := chi.NewMux()
|
||||
m.Use(options.Middleware...)
|
||||
|
||||
svc := Thumbnails{
|
||||
svc := Thumbnail{
|
||||
config: options.Config,
|
||||
mux: m,
|
||||
manager: thumbnails.SimpleManager{
|
||||
@@ -40,8 +41,8 @@ func NewService(opts ...Option) Service {
|
||||
return svc
|
||||
}
|
||||
|
||||
// Thumbnails defines implements the business logic for Service.
|
||||
type Thumbnails struct {
|
||||
// Thumbnail implements the business logic for Service.
|
||||
type Thumbnail struct {
|
||||
config *config.Config
|
||||
mux *chi.Mux
|
||||
manager thumbnails.Manager
|
||||
@@ -49,12 +50,12 @@ type Thumbnails struct {
|
||||
}
|
||||
|
||||
// ServeHTTP implements the Service interface.
|
||||
func (g Thumbnails) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
func (g Thumbnail) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
g.mux.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// Thumbnails provides the endpoint to retrieve a thumbnail for an image
|
||||
func (g Thumbnails) Thumbnails(w http.ResponseWriter, r *http.Request) {
|
||||
func (g Thumbnail) Thumbnails(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query()
|
||||
width, _ := strconv.Atoi(query.Get("width"))
|
||||
height, _ := strconv.Atoi(query.Get("height"))
|
||||
@@ -84,11 +85,9 @@ func (g Thumbnails) Thumbnails(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
auth := r.Header.Get("Authorization")
|
||||
|
||||
sCtx := imgsource.NewContext()
|
||||
sCtx.Set(imgsource.WebDavAuth, auth)
|
||||
sCtx := context.WithValue(r.Context(), imgsource.WebDavAuth, auth)
|
||||
// TODO: clean up error handling
|
||||
img, err := g.source.Get(ctx.ImagePath, sCtx)
|
||||
img, err := g.source.Get(sCtx, ctx.ImagePath)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
|
||||
@@ -1,33 +1,11 @@
|
||||
package imgsource
|
||||
|
||||
import "image"
|
||||
import (
|
||||
"context"
|
||||
"image"
|
||||
)
|
||||
|
||||
// Source defines the interface for image sources
|
||||
type Source interface {
|
||||
Get(path string, ctx SourceContext) (image.Image, error)
|
||||
}
|
||||
|
||||
// NewContext creates a new SourceContext instance
|
||||
func NewContext() SourceContext {
|
||||
return SourceContext{
|
||||
m: make(map[string]interface{}),
|
||||
}
|
||||
}
|
||||
|
||||
// SourceContext is used to pass source specific parameters
|
||||
type SourceContext struct {
|
||||
m map[string]interface{}
|
||||
}
|
||||
|
||||
// GetString tries to cast the value to a string
|
||||
func (s SourceContext) GetString(key string) string {
|
||||
if s, ok := s.m[key].(string); ok {
|
||||
return s
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Set sets a value
|
||||
func (s SourceContext) Set(key string, val interface{}) {
|
||||
s.m[key] = val
|
||||
Get(ctx context.Context, path string) (image.Image, error)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package imgsource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"image"
|
||||
"net/http"
|
||||
@@ -28,7 +29,7 @@ const (
|
||||
)
|
||||
|
||||
// Get downloads the file from a webdav service
|
||||
func (s WebDav) Get(file string, ctx SourceContext) (image.Image, error) {
|
||||
func (s WebDav) Get(ctx context.Context, file string) (image.Image, error) {
|
||||
u, _ := url.Parse(s.baseURL)
|
||||
u.Path = path.Join(u.Path, file)
|
||||
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
||||
@@ -36,7 +37,10 @@ func (s WebDav) Get(file string, ctx SourceContext) (image.Image, error) {
|
||||
return nil, fmt.Errorf("could not get the image \"%s\" error: %s", file, err.Error())
|
||||
}
|
||||
|
||||
auth := ctx.GetString(WebDavAuth)
|
||||
auth, ok := authorization(ctx)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("could not get image \"%s\" error: authorization is missing", file)
|
||||
}
|
||||
req.Header.Add("Authorization", auth)
|
||||
|
||||
client := &http.Client{}
|
||||
@@ -55,3 +59,8 @@ func (s WebDav) Get(file string, ctx SourceContext) (image.Image, error) {
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
|
||||
func authorization(ctx context.Context) (string, bool) {
|
||||
auth, ok := ctx.Value(WebDavAuth).(string)
|
||||
return auth, ok
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user