diff --git a/model/exists.go b/model/exists.go index b98bbcc7..fbe1090b 100644 --- a/model/exists.go +++ b/model/exists.go @@ -66,3 +66,16 @@ func HashExists(ctx context.Context, exec boil.ContextExecutor, hash string) (bo } return ok, nil } + +// UUIDExists returns true if the file record exists in the database using a UUID. +func UUIDExists(ctx context.Context, exec boil.ContextExecutor, uuid string) (bool, error) { + if invalidExec(exec) { + return false, ErrDB + } + ok, err := models.Files(models.FileWhere.UUID.EQ(null.StringFrom(uuid)), + qm.WithDeleted()).Exists(ctx, exec) + if err != nil { + return false, fmt.Errorf("exist file uuid %s: %w", uuid, err) + } + return ok, nil +} diff --git a/model/insert.go b/model/insert.go index 84c99779..bdc85fca 100644 --- a/model/insert.go +++ b/model/insert.go @@ -7,6 +7,7 @@ import ( "database/sql" "fmt" "net/url" + "os" "time" "github.com/Defacto2/server/handler/pouet" @@ -84,6 +85,11 @@ func InsertUpload(ctx context.Context, tx *sql.Tx, values url.Values, key string return 0, noID, fmt.Errorf("uuid.NewV7: %w", err) } unique := null.StringFrom(uid.String()) + if exist, err := UUIDExists(ctx, tx, uid.String()); err != nil { + return 0, noID, fmt.Errorf("UUIDExists: %w", err) + } else if exist { + return 0, noID, fmt.Errorf("insert uload %w, does the uuid already exist in the table?: %s", ErrUUID, uid.String()) + } deleteT := null.TimeFromPtr(&now) if !deleteT.Valid || deleteT.Time.IsZero() { return 0, noID, fmt.Errorf("%w: %v", ErrTime, deleteT.Time) @@ -101,11 +107,12 @@ func InsertUpload(ctx context.Context, tx *sql.Tx, values url.Values, key string if err != nil { return 0, noID, fmt.Errorf("upload: %w", err) } + fmt.Fprintf(os.Stderr, "\ninsert upload f.Insert: %+v\n", f) if err = f.Insert(ctx, tx, boil.Infer()); err != nil { - return 0, noID, fmt.Errorf("f.Insert: %w", err) + return 0, noID, fmt.Errorf("insert upload key %q: %w", key, err) } if err = tx.Commit(); err != nil { - return 0, noID, fmt.Errorf("tx.Commit: %w", err) + return 0, noID, fmt.Errorf("insert upload key %q tx.commit: %w", key, err) } return f.ID, uid, nil }