@@ -2,7 +2,6 @@ package short
2
2
3
3
import (
4
4
"errors"
5
- "fmt"
6
5
"net/http"
7
6
"strings"
8
7
@@ -28,66 +27,65 @@ func NewHandlers(db sb.Client, e *echo.Echo) *Short {
28
27
29
28
func (s * Short ) redirectLink (c echo.Context ) error {
30
29
path := c .Param ("path" )
31
- // path := fmt.Sprintf("/%s", c.Param("path"))
32
30
33
31
// Split path by "/"
34
32
paths := strings .Split (path , "/" )
35
33
36
- var (
37
- links []models.Link
38
- link models.Link
39
- ns models.Namespace
40
- err error
34
+ const (
35
+ uaTypeBrowser = iota
36
+ uaTypeCLI
41
37
)
42
38
43
- switch {
44
- case len (paths ) == 1 :
45
- link , err = s .DB .GetLinkByPath (c .Request ().Context (), "/" + path , "default" )
46
- if err != nil {
47
- if errors .Is (err , models .ErrNotFound ) {
48
- // Link is not found, find if namespace exists and redirect to namespace page
49
- ns , err = s .DB .GetNamespace (c .Request ().Context (), paths [0 ])
50
- if err != nil {
51
- goto RETURNERROR
52
- }
53
-
54
- return c .HTML (http .StatusTemporaryRedirect , "/u/" + ns .Name )
39
+ var (
40
+ ns = "default"
41
+ target = paths [0 ]
42
+
43
+ uaType = func () int {
44
+ ua := c .Request ().UserAgent ()
45
+
46
+ // Check if UserAgent is a web browser
47
+ switch {
48
+ case strings .Contains (ua , "curl" ) || strings .Contains (ua , "wget" ):
49
+ return uaTypeCLI
50
+ default :
51
+ return uaTypeBrowser
55
52
}
56
- goto RETURNERROR
57
- }
58
- goto REDIRECT
59
- default :
60
- // find link by path and namespace
61
- ns , err = s .DB .GetNamespace (c .Request ().Context (), paths [0 ])
62
- if err != nil {
63
- goto RETURNERROR
64
- }
65
-
66
- links , err = s .DB .ListLinks (c .Request ().Context (), ns .Name )
67
- if err != nil {
68
- goto RETURNERROR
69
- }
53
+ }()
54
+ )
70
55
71
- for _ , l := range links {
72
- if l .SourcePath == fmt .Sprintf ("/%s" , paths [1 ]) && l .NameSpace == ns .Name {
73
- link = l
74
- goto REDIRECT
75
- }
76
- }
56
+ if len (paths ) != 1 {
57
+ ns = paths [0 ]
58
+ target = paths [1 ]
59
+ }
77
60
78
- err = models .ErrNotFound
61
+ link , err := s .DB .GetLinkByPath (c .Request ().Context (), "/" + target , ns )
62
+ if err != nil {
79
63
goto RETURNERROR
80
64
}
81
65
66
+ goto REDIRECT
67
+
82
68
RETURNERROR:
69
+
83
70
switch {
84
- // TODO add html PAGE if UserAgent is a web browser and json output for other UserAgents
85
71
case errors .Is (err , models .ErrNotFound ):
86
- return c .HTML (http .StatusNotFound , "Not Found" )
72
+ switch uaType {
73
+ case uaTypeBrowser :
74
+ return s .handleHTML404 (c )
75
+ case uaTypeCLI :
76
+ return c .JSON (http .StatusNotFound , map [string ]string {
77
+ "error" : "not found" ,
78
+ })
79
+ }
87
80
case err != nil :
88
81
return c .JSON (http .StatusInternalServerError , err )
89
82
}
90
83
91
84
REDIRECT:
92
- return c .Redirect (http .StatusTemporaryRedirect , link .TargetURL )
85
+ switch uaType {
86
+ case uaTypeCLI :
87
+ return c .String (http .StatusTemporaryRedirect , link .TargetURL )
88
+ default :
89
+ return c .Redirect (http .StatusTemporaryRedirect , link .TargetURL )
90
+ }
93
91
}
0 commit comments