package storage import ( "database/sql" "encoding/binary" "encoding/json" "errors" "fmt" "log" "math/bits" "git.sp4ke.com/sp4ke/bit4sat/db" "git.sp4ke.com/sp4ke/bit4sat/ln" "github.com/jmoiron/sqlx" "github.com/lib/pq" "github.com/mediocregopher/radix/v3" ) var DB = db.DB const ( //TODO: sync upload status from redis // TODO: status is currently handled in cache not here DBUploadSchema = ` CREATE TABLE IF NOT EXISTS upload ( id serial PRIMARY KEY, upload_id varchar(9) NOT NULL, sha256 varchar(64) NOT NULL, file_name varchar(255) NOT NULL, file_type varchar(255) DEFAULT '', file_size integer NOT NULL, file_ext varchar(255) DEFAULT '', ask_fee integer NOT NULL DEFAULT 0, stored boolean DEFAULT '0', UNIQUE (upload_id, sha256) ); ` QNewUpload = `INSERT INTO upload (upload_id, sha256, file_name, file_type, file_size, file_ext, stored, ask_fee) VALUES (:upload_id, :sha256, :file_name, :file_type, :file_size, :file_ext, :stored, :ask_fee)` QSetStored = `UPDATE upload SET stored = :stored WHERE upload_id = :upload_id ` QGetByHashID = `SELECT upload_id, sha256, file_name, file_type, file_size, file_ext, ask_fee, stored FROM upload WHERE sha256 = $1 AND upload_id = $2` ) type UpStatus uint32 func (st UpStatus) MarshalJSON() ([]byte, error) { res := map[string]string{} res["pay_status"] = st.PrintPayStatus() res["store_status"] = st.PrintStoreStatus() return json.Marshal(res) } func (st *UpStatus) UnmarshalBinary(b []byte) error { //fmt.Printf("%#v\n", b) // first 4 bits are reseved // Single byte will be for 0 value if len(b) == 1 { *st = 0 return nil } view := binary.BigEndian.Uint32(b) view = bits.Reverse32(view) //fmt.Printf("%32b\n", view) //log.Printf("%d\n", view) *st = UpStatus(view) return nil } // Get list of positions set func (st UpStatus) GetFlagPositions() []int { var i uint32 var setBits []int for i = 31; i > 0; i-- { if st&(1<