mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-03-28 18:24:13 -04:00
Compare commits
1 Commits
2.0
...
fix-double
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
62c821eb9f |
3
main.go
3
main.go
@@ -28,6 +28,9 @@ func wrapHandler(h handler) http.Handler {
|
|||||||
}()
|
}()
|
||||||
err := h(w, r)
|
err := h(w, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if render.IsRendererError(err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
utils.RenderError(w, r, 500, err.Error())
|
utils.RenderError(w, r, 500, err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,9 +34,6 @@ func HandleUserAvatar(w http.ResponseWriter, r *http.Request) error {
|
|||||||
|
|
||||||
func handleMedia(w http.ResponseWriter, r *http.Request, url string) error {
|
func handleMedia(w http.ResponseWriter, r *http.Request, url string) error {
|
||||||
utils.SetHeaders(w)
|
utils.SetHeaders(w)
|
||||||
if !utils.Config.RestrictiveCORS {
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
}
|
|
||||||
path := r.URL.Path
|
path := r.URL.Path
|
||||||
|
|
||||||
if utils.Config.ForceWebp &&
|
if utils.Config.ForceWebp &&
|
||||||
@@ -87,6 +84,6 @@ func handleMedia(w http.ResponseWriter, r *http.Request, url string) error {
|
|||||||
w.Header().Set("Content-Range", res.Header.Get("Content-Range"))
|
w.Header().Set("Content-Range", res.Header.Get("Content-Range"))
|
||||||
}
|
}
|
||||||
|
|
||||||
io.Copy(w, res.Body)
|
_, err = io.Copy(w, res.Body)
|
||||||
return nil // don't call RenderError after this point
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
package render
|
package render
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
@@ -63,14 +64,46 @@ func Initialize(views fs.FS) {
|
|||||||
func (r *renderer) Render(out io.Writer, name string, bind map[string]any) error {
|
func (r *renderer) Render(out io.Writer, name string, bind map[string]any) error {
|
||||||
tmpl := r.templates[name]
|
tmpl := r.templates[name]
|
||||||
if tmpl == nil {
|
if tmpl == nil {
|
||||||
return fmt.Errorf("render: template %s does not exist", name)
|
return re(fmt.Errorf("render: template %s does not exist", name))
|
||||||
}
|
}
|
||||||
parsed, err := tmpl.Exec(bind)
|
parsed, err := tmpl.Exec(bind)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("render: %w", err)
|
return re(fmt.Errorf("render: %w", err))
|
||||||
}
|
}
|
||||||
if _, err = out.Write([]byte(parsed)); err != nil {
|
if _, err = out.Write([]byte(parsed)); err != nil {
|
||||||
return fmt.Errorf("render: %w", err)
|
return re(fmt.Errorf("render: %w", err))
|
||||||
}
|
}
|
||||||
return err
|
return re(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var reSentinel = &struct{}{}
|
||||||
|
|
||||||
|
type RendererError struct {
|
||||||
|
u *struct{}
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func re(err error) error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return RendererError{reSentinel, err}
|
||||||
|
}
|
||||||
|
func (re RendererError) Error() string {
|
||||||
|
return re.Error()
|
||||||
|
}
|
||||||
|
func (re RendererError) Unwrap() error {
|
||||||
|
e1, ok := re.err.(interface{ Unwrap() error })
|
||||||
|
if ok {
|
||||||
|
return e1.Unwrap()
|
||||||
|
}
|
||||||
|
e2, ok := re.err.(interface{ Unwrap() []error })
|
||||||
|
if ok {
|
||||||
|
return errors.Join(e2.Unwrap()...)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func IsRendererError(err error) bool {
|
||||||
|
re, ok := err.(RendererError)
|
||||||
|
return ok && re.u == reSentinel
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ type config struct {
|
|||||||
ProtocolDetection bool
|
ProtocolDetection bool
|
||||||
Secure bool
|
Secure bool
|
||||||
ForceWebp bool
|
ForceWebp bool
|
||||||
RestrictiveCORS bool
|
|
||||||
ImageCache bool
|
ImageCache bool
|
||||||
CleanupInterval time.Duration
|
CleanupInterval time.Duration
|
||||||
CacheDir string
|
CacheDir string
|
||||||
@@ -40,7 +39,6 @@ func LoadConfig() {
|
|||||||
ProtocolDetection: envBool("PROTOCOL_DETECTION"),
|
ProtocolDetection: envBool("PROTOCOL_DETECTION"),
|
||||||
Secure: envBool("SECURE"),
|
Secure: envBool("SECURE"),
|
||||||
ForceWebp: envBool("FORCE_WEBP"),
|
ForceWebp: envBool("FORCE_WEBP"),
|
||||||
RestrictiveCORS: envBool("RESTRICTIVE_CORS"),
|
|
||||||
Privacy: map[string]interface{}{
|
Privacy: map[string]interface{}{
|
||||||
"set": os.Getenv("PRIVACY_NOT_COLLECTED") != "",
|
"set": os.Getenv("PRIVACY_NOT_COLLECTED") != "",
|
||||||
"policy": os.Getenv("PRIVACY_POLICY"),
|
"policy": os.Getenv("PRIVACY_POLICY"),
|
||||||
|
|||||||
Reference in New Issue
Block a user