FIX: Checked If File Exists Before Submitting Queue Job

This commit is contained in:
FahdShalhoub
2026-08-26 18:45:32 +03:00
parent cb3ab1f442
commit 858b277c9d
2 changed files with 12 additions and 21 deletions
+9 -14
View File
@@ -10,7 +10,6 @@ import (
"net/http"
"path/filepath"
"slices"
"strconv"
"strings"
"thamanyah/cms/v2/internal/api"
"thamanyah/cms/v2/internal/db/repositories"
@@ -21,7 +20,7 @@ import (
const (
videoUploadPrefix = "videos"
uploadURLExpiry = 15 * time.Minute
uploadURLExpiry = 60 * time.Minute
maxJSONBodySize = 1 << 20 // 1 MiB
)
@@ -92,7 +91,7 @@ func PresignVideoUpload(w http.ResponseWriter, r *http.Request) {
}
key := videoUploadPrefix + "/" + filename
presignedURL, err := services.S3Client.GetPresignedURL(r.Context(), key, req.ContentType, time.Hour)
presignedURL, err := services.S3Client.GetPresignedURL(r.Context(), key, req.ContentType, uploadURLExpiry)
if err != nil {
writeProblem(w, http.StatusInternalServerError, "Could not prepare upload.",
"A presigned upload URL could not be issued for the storage bucket. This is a server-side fault; no upload slot was reserved, so retry the request.")
@@ -145,6 +144,13 @@ func CompleteVideoUpload(w http.ResponseWriter, r *http.Request) {
return
}
exists := services.S3Client.DoesFileExist(r.Context(), key)
if exists {
writeProblem(w, http.StatusUnprocessableEntity, "Failed to find video in storage",
"The video file that was supposed to be uploaded via the url sent in the PresignVideoUpload endpoint was not uploaded before calling this endpoint")
return
}
err := validateCatagoryIDS(r.Context(), req.CategoryIDs)
if err != nil {
log.Printf("Something Went Wrong Loading Categories: %s", err)
@@ -210,17 +216,6 @@ func validateCatagoryIDS(ctx context.Context, submitted []int16) (err error) {
return nil
}
func formatCategoryIDs(ids []int16) string {
formatted := make([]string, 0, len(ids))
for _, id := range ids {
formatted = append(formatted, strconv.Itoa(int(id)))
}
if len(formatted) == 1 {
return "id " + formatted[0]
}
return "ids " + strings.Join(formatted, ", ")
}
func randomFilename(original string) (string, error) {
ext := filepath.Ext(filepath.Base(original))
buf := make([]byte, 16)
+3 -7
View File
@@ -14,7 +14,7 @@ var S3Client S3
type S3 interface {
GetPresignedURL(ctx context.Context, key, contentType string, expiry time.Duration) (string, error)
DoesFileExist(ctx context.Context, key string) (bool, error)
DoesFileExist(ctx context.Context, key string) bool
}
type S3Concrete struct {
@@ -37,17 +37,13 @@ func (svc S3Concrete) GetPresignedURL(ctx context.Context, key, contentType stri
return request.URL, nil
}
func (svc S3Concrete) DoesFileExist(ctx context.Context, key string) (bool, error) {
func (svc S3Concrete) DoesFileExist(ctx context.Context, key string) bool {
_, error := svc.S3Client.HeadObject(ctx, &s3.HeadObjectInput{
Bucket: &svc.Bucket,
Key: &key,
})
if error != nil {
return false, error
}
return true, nil
return error != nil
}
// AssertSuccessfulConnection verifies that the bucket is reachable and that