summaryrefslogtreecommitdiff
path: root/pages/get.go
diff options
context:
space:
mode:
Diffstat (limited to 'pages/get.go')
-rw-r--r--pages/get.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/pages/get.go b/pages/get.go
new file mode 100644
index 0000000..7f7df2f
--- /dev/null
+++ b/pages/get.go
@@ -0,0 +1,53 @@
+package pages
+
+import (
+ "delayed.link/storage"
+ _ "embed"
+ "errors"
+ "fmt"
+ "github.com/google/uuid"
+ "net/http"
+ "time"
+)
+
+func Get(w http.ResponseWriter, r *http.Request) {
+ id := r.PathValue("id")
+ uid, err := uuid.Parse(id)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ return
+ }
+ l, err := storage.Current.Load(uid)
+ if err != nil && errors.Is(err, storage.NotFoundError) {
+ // if not found error, return 404
+ http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+ return
+ } else if err != nil {
+ // else return internal error
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ valid := l.Valid()
+ if errors.Is(valid, storage.NotFoundError) {
+ http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+ return
+ }
+ if errors.Is(valid, storage.NoOpensError) {
+ http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+ return
+ }
+ if errors.Is(valid, storage.NotYetOpenError) {
+ http.Error(w, http.StatusText(http.StatusLocked), http.StatusLocked)
+ msg := fmt.Sprintf("\n\nThis link will be usable in %v minutes.", int(l.OpensFrom.Sub(time.Now()).Minutes()))
+ w.Write([]byte(msg))
+ return
+ }
+
+ err = l.Use()
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ }
+
+ http.Redirect(w, r, l.Target, http.StatusFound)
+}