deletebannedfile.go 1.22 KiB
package routes
import (
"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/rs/zerolog/log"
)
// DeleteBannedFile deletes a FileBan.
func DeleteBannedFile(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
banned, err := db.CheckIfFileBanExists(sha256)
if err != nil {
log.Error().Err(err).Msg("failed to check if file is banned in DeleteBannedFile")
panic(apierrors.InternalServerError)
}
if !banned {
panic(apierrors.FileIsNotBanned)
}
// Unban the file
err = db.DeleteBannedFile(sha256)
if err != nil {
log.Error().Err(err).Msg("failed to delete file ban")
panic(apierrors.InternalServerError)
}
// Return success response
w.WriteHeader(http.StatusNoContent)
}