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 // // stored: if file was stored DBFileUploadsSchema = ` CREATE TABLE IF NOT EXISTS file_uploads ( file_upload_id serial PRIMARY KEY, upload_id text NOT NULL references uploads(upload_id), 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 '', stored boolean DEFAULT 'false', UNIQUE (upload_id, sha256) ); ` // Unique entry per upload // // store: all files for upload where stored DBUploadsSchema = ` CREATE TABLE IF NOT EXISTS uploads ( upload_id text PRIMARY KEY, download_id text DEFAULT '', created timestamp, ask_fee boolean NOT NULL DEFAULT 'false', ask_currency varchar(3) DEFAULT '', ask_amount real DEFAULT 0, status integer NOT NULL DEFAULT 0, settled boolean DEFAULT 'false', admin_token text DEFAULT '', invoice_rhash varchar(64) references invoices(rhash) ); ` DBInvoicesSchema = ` CREATE TABLE IF NOT EXISTS invoices ( rhash varchar(64) PRIMARY KEY, payload text DEFAULT '' ); ` QSetFileStored = `UPDATE file_uploads SET stored = :stored WHERE upload_id = :upload_id AND sha256 = :sha256` QGetByHashID = `SELECT upload_id, sha256, file_name, file_type, file_size, file_ext, stored FROM file_uploads 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<