mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-02-02 19:56:33 +00:00
simplify SplitNameExt
This commit is contained in:
6
main.go
6
main.go
@@ -87,7 +87,7 @@ func main() {
|
||||
app.Handle("GET /t/{tag}", wrapHandler(func(w http.ResponseWriter, r *http.Request) error {
|
||||
name, ext := utils.SplitNameExt(r.PathValue("tag"))
|
||||
if ext != "" {
|
||||
r.SetPathValue("tag", name[0:len(name)-1])
|
||||
r.SetPathValue("tag", name)
|
||||
r.SetPathValue("type", ext)
|
||||
return pages.HandleTagRSS(w, r)
|
||||
}
|
||||
@@ -98,7 +98,7 @@ func main() {
|
||||
app.Handle("GET /user/{userID}", wrapHandler(func(w http.ResponseWriter, r *http.Request) error {
|
||||
name, ext := utils.SplitNameExt(r.PathValue("userID"))
|
||||
if ext != "" {
|
||||
r.SetPathValue("userID", name[0:len(name)-1])
|
||||
r.SetPathValue("userID", name)
|
||||
r.SetPathValue("type", ext)
|
||||
return pages.HandleUserRSS(w, r)
|
||||
}
|
||||
@@ -133,7 +133,7 @@ func main() {
|
||||
r.SetPathValue("baseName", baseName)
|
||||
r.SetPathValue("extension", extension)
|
||||
switch extension {
|
||||
case "png", "gif", "jpg", "jpeg", "webp", "mp4", "webm":
|
||||
case ".png", ".gif", ".jpg", ".jpeg", ".webp", ".mp4", ".webm":
|
||||
return pages.HandleMedia(w, r)
|
||||
}
|
||||
fallthrough
|
||||
|
||||
@@ -14,7 +14,7 @@ func HandleMedia(w http.ResponseWriter, r *http.Request) error {
|
||||
w.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'self'; img-src 'self'")
|
||||
baseName, extension := r.PathValue("baseName"), r.PathValue("extension")
|
||||
if strings.HasPrefix(r.URL.Path, "/stack") {
|
||||
return handleMedia(w, r, "https://i.stack.imgur.com/"+baseName[5:]+extension)
|
||||
return handleMedia(w, r, "https://i.stack.imgur.com/"+baseName+extension)
|
||||
} else {
|
||||
return handleMedia(w, r, "https://i.imgur.com/"+baseName+extension)
|
||||
}
|
||||
|
||||
@@ -116,19 +116,19 @@ func handleFeed(w http.ResponseWriter, r *http.Request, instance string, feed *f
|
||||
|
||||
w.Header().Set("Content-Type", mime.TypeByExtension("."+r.PathValue("type")))
|
||||
switch r.PathValue("type") {
|
||||
case "atom":
|
||||
case ".atom":
|
||||
body, err := feed.ToAtom()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w.Write([]byte(body))
|
||||
case "json":
|
||||
case ".json":
|
||||
body, err := feed.ToJSON()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w.Write([]byte(body))
|
||||
case "rss":
|
||||
case ".rss":
|
||||
body, err := feed.ToRss()
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -1,16 +1,8 @@
|
||||
package utils
|
||||
|
||||
import "path/filepath"
|
||||
|
||||
func SplitNameExt(path string) (name, ext string) {
|
||||
name, ext = path, ""
|
||||
for range 5 {
|
||||
if len(name) == 0 || name[len(name)-1] == '.' || name[len(name)-1] == '/' {
|
||||
break
|
||||
}
|
||||
name = name[:len(name)-1]
|
||||
ext = path[len(name):]
|
||||
}
|
||||
if len(name) == 0 || name[len(name)-1] != '.' {
|
||||
return path, ""
|
||||
}
|
||||
return
|
||||
ext = filepath.Ext(path)
|
||||
return path[:len(path)-len(ext)], ext
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user