From c7c23b9e752b0327e3fc2341a5a6aba1d8723d50 Mon Sep 17 00:00:00 2001 From: Arnas Udovic Date: Mon, 12 Aug 2024 17:32:27 +0300 Subject: [PATCH] redirect /r/:id action --- main.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/main.go b/main.go index b88a566..22a681a 100644 --- a/main.go +++ b/main.go @@ -175,6 +175,8 @@ func process(_ context.Context, w gemini.ResponseWriter, r *gemini.Request) { renderTag(w, r, client) case regexp.MustCompile(`^/__a/`).MatchString(r.URL.Path): downloadAttachment(w, r, client) + case regexp.MustCompile(`^/r/.+`).MatchString(r.URL.Path): + redirectAction(w, r, client) default: w.WriteHeader(gemini.StatusNotFound, "Out of space") } @@ -522,3 +524,26 @@ func renderTag(w gemini.ResponseWriter, r *gemini.Request, client TreeManagerCli 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(), + ), + ) +}