redirect /r/:id action
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Arnas Udovic 2024-08-12 17:32:27 +03:00
parent 465769a00d
commit c7c23b9e75

25
main.go
View file

@ -175,6 +175,8 @@ func process(_ context.Context, w gemini.ResponseWriter, r *gemini.Request) {
renderTag(w, r, client) renderTag(w, r, client)
case regexp.MustCompile(`^/__a/`).MatchString(r.URL.Path): case regexp.MustCompile(`^/__a/`).MatchString(r.URL.Path):
downloadAttachment(w, r, client) downloadAttachment(w, r, client)
case regexp.MustCompile(`^/r/.+`).MatchString(r.URL.Path):
redirectAction(w, r, client)
default: default:
w.WriteHeader(gemini.StatusNotFound, "Out of space") w.WriteHeader(gemini.StatusNotFound, "Out of space")
} }
@ -522,3 +524,26 @@ func renderTag(w gemini.ResponseWriter, r *gemini.Request, client TreeManagerCli
return return
} }
} }
func redirectAction(w gemini.ResponseWriter, r *gemini.Request, client TreeManagerClient) {
urlParts := strings.Split(r.URL.Path, "/")
id := urlParts[1]
fr := FileRequest{Id: id}
file, err := client.GetFile(context.Background(), &fr)
if err != nil {
log.Fatalf("client.GetFile failed: %v", err)
w.WriteHeader(gemini.StatusTemporaryFailure, "Internal server error")
return
}
w.WriteHeader(
gemini.StatusPermanentRedirect,
fmt.Sprintf(
"/f/%s/%s/%s",
strings.Join(file.File.Category, "/"),
file.File.Id,
file.File.GmiName(),
),
)
}