1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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)
}
|