package model import "time" import ( "sour.is/x/dbm" "database/sql" "bytes" "fmt" "log" "crypto/sha256" "encoding/base64" ) type Paste struct{ Id string `json:"paste_id"` Text string `json:"paste_text"` Chk string `json:"paste_chk"` Lang string `json:"paste_lang"` Zip bool `json:"paste_zip"` Burn bool `json:"paste_burn"` CreatedOn time.Time `json:"created_on"` CreatedBy string `json:"created_by"` ExpiresOn *time.Time `json:"expires_on"` Deleted bool `json:"deleted"` } /* func GetPaste(id string) (p Paste, err error) { db := dbm.GetDB() stmt, _ := db.Prepare(` select paste_id, paste_text, created_on, created_by, paste_lang, expires_on, paste_zip, paste_burn, paste_chk from paste where paste_id = ?`) var lang sql.NullString var zip sql.NullBool var burn sql.NullBool var expiresOn NullTime err = stmt.QueryRow(id).Scan(&p.Id, &p.Text, &p.CreatedOn, &p.CreatedBy, &lang, &expiresOn, &zip, &burn, &p.Chk); if err != nil { log.Print(err) return p, err } if lang.Valid { p.Lang = lang.String } if zip.Valid { p.Zip = zip.Bool } if burn.Valid { p.Burn = burn.Bool } if expiresOn.Valid { p.ExpiresOn = &expiresOn.Time } return } func CreatePaste(p Paste) (Paste, error) { db := dbm.GetDB() s256 := sha256.Sum256([]byte(p.String())) p.Id = base64.RawURLEncoding.EncodeToString(s256[12:]) count, err := db.Prepare(` SELECT count(1) ok FROM paste WHERE paste_id=?`) checkErr(err) insert, err := db.Prepare(` INSERT paste SET paste_id = ?, paste_text = ?, paste_chk = ?, created_by = ?, paste_lang = ?, paste_zip = ?, paste_burn = ?, expires_on = ?`) checkErr(err) update, err := db.Prepare(` UPDATE paste SET paste_text = ?, paste_chk = ?, created_by = ?, paste_lang = ?, paste_zip = ?, paste_burn = ?, expires_on = ? WHERE paste_id = ?`) checkErr(err) var ok bool err = count.QueryRow(p.Id).Scan(&ok) checkErr(err) if ok { _, err = update.Exec(p.Text, p.Chk, p.CreatedBy, p.Lang, p.Zip, p.Burn, p.ExpiresOn, p.Id) checkErr(err) } else { _, err = insert.Exec(p.Id, p.Text, p.Chk, p.CreatedBy, p.Lang, p.Zip, p.Burn, p.ExpiresOn) checkErr(err) } np, err := GetPaste(p.Id) checkErr(err) return np, err } */ func (p Paste) String() string { var b bytes.Buffer b.WriteString(fmt.Sprintf("; id:\t%s\n", p.Id)) b.WriteString(fmt.Sprintf("chk:\t%s\n", p.Chk)) b.WriteString(fmt.Sprintf("lang:\t%s\n", p.Lang)) if p.ExpiresOn != nil { b.WriteString(fmt.Sprintf("exp:\t%d\n", p.ExpiresOn.Unix())) } if p.Zip { b.WriteString(fmt.Sprintf("zip:\t%t\n", p.Zip)) } if p.Burn { b.WriteString(fmt.Sprintf("burn:\t%t\n", p.Burn)) } b.WriteString("\n") b.WriteString(p.Text) return b.String() }