Skip to content

Commit

Permalink
Insert validations.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarrett committed Apr 14, 2024
1 parent a37de75 commit aef7604
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 127 deletions.
30 changes: 3 additions & 27 deletions handler/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,30 +1028,6 @@ func Updated(t any, s string) string {
}
}

// ValidD returns a valid day or a null value.
func ValidD(d int16) null.Int16 {
if d < 1 || d > 31 {
return null.Int16{Int16: 0, Valid: false}
}
return null.Int16{Int16: d, Valid: true}
}

// ValidM returns a valid month or a null value.
func ValidM(m int16) null.Int16 {
if m < 1 || m > 12 {
return null.Int16{Int16: 0, Valid: false}
}
return null.Int16{Int16: m, Valid: true}
}

// ValidY returns a valid year or a null value.
func ValidY(y int16) null.Int16 {
if y < 1980 || y > int16(time.Now().Year()) {
return null.Int16{Int16: 0, Valid: false}
}
return null.Int16{Int16: y, Valid: true}
}

// websiteIcon returns a Bootstrap icon name for the given website url.
func WebsiteIcon(url string) template.HTML {
icon := websiteIcon(url)
Expand Down Expand Up @@ -1092,9 +1068,9 @@ func YMDEdit(c echo.Context) error {
if err != nil {
return err
}
y := ValidY(f.Year)
m := ValidM(f.Month)
d := ValidD(f.Day)
y := model.ValidY(f.Year)
m := model.ValidM(f.Month)
d := model.ValidD(f.Day)
if err = model.UpdateYMD(c, int64(f.ID), y, m, d); err != nil {
return badRequest(c, err)
}
Expand Down
2 changes: 1 addition & 1 deletion handler/app/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const (
)

const (
epoch = 1980 // epoch is the default year for MS-DOS files without a timestamp
epoch = model.EpochYear // epoch is the default year for MS-DOS files without a timestamp
)

// Artifact404 renders the error page for the artifact links.
Expand Down
99 changes: 0 additions & 99 deletions model/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@ import (
"encoding/hex"
"errors"
"fmt"
"net/url"
"strconv"
"time"

"github.com/Defacto2/server/internal/demozoo"
"github.com/Defacto2/server/internal/helper"
"github.com/Defacto2/server/internal/postgres"
"github.com/Defacto2/server/internal/postgres/models"
"github.com/google/uuid"
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)

Expand Down Expand Up @@ -89,32 +83,6 @@ func FindDemozooFile(ctx context.Context, db *sql.DB, id int64) (bool, int64, er
return deleted, f.ID, nil
}

// InsertDemozooFile inserts a new file record into the database using a Demozoo production ID.
// This will not check if the Demozoo production ID already exists in the database.
// When successful the function will return the new record ID.
func InsertDemozooFile(ctx context.Context, db *sql.DB, id int64) (int64, error) {
if db == nil {
return 0, ErrDB
}
if id < startID || id > demozoo.Sanity {
return 0, fmt.Errorf("%w: %d", ErrID, id)
}
uid, err := uuid.NewV7()
if err != nil {
return 0, err
}
now := time.Now()
f := models.File{
UUID: null.StringFrom(uid.String()),
WebIDDemozoo: null.Int64From(id),
Deletedat: null.TimeFromPtr(&now),
}
if err = f.Insert(ctx, db, boil.Infer()); err != nil {
return 0, err
}
return f.ID, nil
}

// ExistPouetFile returns true if the file record exists in the database using a Pouet production ID.
// This function will also return true for records that have been marked as deleted.
func ExistPouetFile(ctx context.Context, db *sql.DB, id int64) (bool, error) {
Expand Down Expand Up @@ -145,73 +113,6 @@ func FindPouetFile(ctx context.Context, db *sql.DB, id int64) (bool, int64, erro
return deleted, f.ID, nil
}

// InsertPouetFile inserts a new file record into the database using a Pouet production ID.
// This will not check if the Pouet production ID already exists in the database.
// When successful the function will return the new record ID.
func InsertPouetFile(ctx context.Context, db *sql.DB, id int64) (int64, error) {
if db == nil {
return 0, ErrDB
}
if id < startID || id > demozoo.Sanity {
return 0, fmt.Errorf("%w: %d", ErrID, id)
}
uid, err := uuid.NewV7()
if err != nil {
return 0, err
}
now := time.Now()
f := models.File{
UUID: null.StringFrom(uid.String()),
WebIDPouet: null.Int64From(id),
Deletedat: null.TimeFromPtr(&now),
}
if err = f.Insert(ctx, db, boil.Infer()); err != nil {
return 0, err
}
return f.ID, nil
}

func InsertUpload(ctx context.Context, db *sql.DB, values url.Values) (int64, error) {
if db == nil {
return 0, ErrDB
}
uid, err := uuid.NewV7()
if err != nil {
return 0, err
}
now := time.Now()

y, _ := strconv.ParseInt(values.Get("year"), 10, 16)
year := int16(y)
m, _ := strconv.ParseInt(values.Get("month"), 10, 16)
month := int16(m)
s, _ := strconv.ParseInt(values.Get("size"), 10, 64)
size := int64(s)

f := models.File{
UUID: null.StringFrom(uid.String()),
Deletedat: null.TimeFromPtr(&now),
Createdat: null.TimeFromPtr(&now),
WebIDYoutube: null.StringFrom(values.Get("youtube")), // validate
GroupBrandFor: null.StringFrom(values.Get("group")), // validate and format
GroupBrandBy: null.StringFrom(values.Get("brand")), // validate and format
RecordTitle: null.StringFrom(values.Get("title")), // validate and format
DateIssuedYear: null.Int16From(year),
DateIssuedMonth: null.Int16From(month),
Filename: null.StringFrom(values.Get("filename")), // validate
Filesize: size,
FileMagicType: null.StringFrom(values.Get("magic")), // validate
FileIntegrityStrong: null.StringFrom(values.Get("integrity")), // validate
FileLastModified: null.TimeFromPtr(&now), // collect from form and validate
Platform: null.StringFrom(values.Get("platform")), // validate
Section: null.StringFrom(values.Get("section")), // hardcode value and validate
}
if err = f.Insert(ctx, db, boil.Infer()); err != nil {
return 0, err
}
return f.ID, nil
}

// FindObf retrieves a single file record from the database using the obfuscated record key.
func FindObf(key string) (*models.File, error) {
return recordObf(false, key)
Expand Down
Loading

0 comments on commit aef7604

Please sign in to comment.