Newer
Older
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
54
55
56
package routes
import (
"database/sql"
"encoding/hex"
"net/http"
"strings"
"owo.codes/whats-this/api/lib/apierrors"
"owo.codes/whats-this/api/lib/db"
"owo.codes/whats-this/api/lib/middleware"
"github.com/go-chi/render"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
// bannedFileResponse is the response format for Object.
type bannedFileResponse struct {
Success bool `json:"success"`
Data db.FileBan `json:"data"`
}
// BannedFile returns metadata about a banned file to an administrator.
func BannedFile(w http.ResponseWriter, r *http.Request) {
// Only authorized admin users can use this route
user := middleware.GetAuthorizedUser(r)
if user.ID == "" || user.IsBlocked || !user.IsAdmin {
panic(apierrors.Unauthorized)
}
// Get the SHA256 hash
sha256String := r.URL.Path
if strings.HasPrefix(sha256String, "/bannedfiles/") {
sha256String = sha256String[13:]
}
sha256, err := hex.DecodeString(sha256String)
if err != nil {
panic(apierrors.BadFileID)
}
// Get the file ban
bannedFile, err := db.GetBannedFile(sha256)
switch {
case errors.Cause(err) == sql.ErrNoRows:
panic(apierrors.FileIsNotBanned)
case err != nil:
log.Error().Err(err).Msg("failed to get FileBan")
panic(apierrors.InternalServerError)
}
// Return response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
render.JSON(w, r, bannedFileResponse{true, bannedFile})
}