You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
1.4 KiB
Go
89 lines
1.4 KiB
Go
package storage
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"git.sp4ke.com/sp4ke/bit4sat/db"
|
|
"github.com/mediocregopher/radix/v3"
|
|
)
|
|
|
|
var DB = db.DB
|
|
|
|
const (
|
|
UpNew = iota
|
|
UpStored
|
|
UpWaitingPayment
|
|
UpReady
|
|
)
|
|
|
|
var UploadStatus = map[int]string{
|
|
UpNew: "new",
|
|
UpStored: "stored",
|
|
UpWaitingPayment: "waiting-payment",
|
|
UpReady: "ready",
|
|
}
|
|
|
|
type SHA256 string
|
|
|
|
func (sha SHA256) MarshalText() ([]byte, error) {
|
|
return []byte(sha), nil
|
|
}
|
|
|
|
type Upload struct {
|
|
ID string
|
|
Status uint8
|
|
Files map[SHA256]*UpFile
|
|
}
|
|
|
|
type UpFile struct {
|
|
FileName string
|
|
FileType string
|
|
FileSize int64
|
|
FileExt string
|
|
}
|
|
|
|
func NewUpload() *Upload {
|
|
files := make(map[SHA256]*UpFile)
|
|
return &Upload{
|
|
Files: files,
|
|
}
|
|
}
|
|
|
|
// Returns true if id exists in DB
|
|
func IdExists(id string) (exists bool, err error) {
|
|
key := fmt.Sprintf("upload:%s", id)
|
|
|
|
err = DB.Do(radix.Cmd(&exists, "EXISTS", key))
|
|
|
|
return
|
|
}
|
|
|
|
// Get a file by upload id and hash
|
|
func GetByID(id string) (*Upload, error) {
|
|
var buf []byte
|
|
key := fmt.Sprintf("upload:%s", id)
|
|
|
|
err := DB.Do(radix.Cmd(&buf, "GET", key))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var up Upload
|
|
err = json.Unmarshal(buf, &up)
|
|
|
|
return &up, err
|
|
}
|
|
|
|
func (u *Upload) Write() error {
|
|
// Create a new upload hash upload:id --> [Files]
|
|
key := fmt.Sprintf("upload:%s", u.ID)
|
|
|
|
enc, err := json.Marshal(u)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return DB.Do(radix.FlatCmd(nil, "SET", key, enc))
|
|
}
|