|
|
|
@ -3,11 +3,14 @@ package storage
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
|
|
|
|
|
"git.sp4ke.com/sp4ke/bit4sat/db"
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
|
"github.com/mattn/go-sqlite3"
|
|
|
|
|
"github.com/mediocregopher/radix/v3"
|
|
|
|
|
"github.com/segmentio/ksuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var DB = db.DB
|
|
|
|
@ -16,8 +19,8 @@ const (
|
|
|
|
|
DBUploadSchema = `
|
|
|
|
|
CREATE TABLE IF NOT EXISTS upload (
|
|
|
|
|
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
|
|
|
|
upload_id CHAR(20) NOT NULL,
|
|
|
|
|
sha256 CHAR(32) NOT NULL,
|
|
|
|
|
upload_id CHAR(27) NOT NULL,
|
|
|
|
|
sha256 CHAR(64) NOT NULL,
|
|
|
|
|
file_name CHAR(255) NOT NULL,
|
|
|
|
|
file_type CHAR(255) DEFAULT '',
|
|
|
|
|
file_size INT NOT NULL,
|
|
|
|
@ -57,11 +60,14 @@ upload.status = upload_status.type
|
|
|
|
|
|
|
|
|
|
QSetStatus = `UPDATE upload SET status = :status WHERE upload_id = :upload_id `
|
|
|
|
|
|
|
|
|
|
QGetByHashID = `SELECT * FROM upload
|
|
|
|
|
WHERE
|
|
|
|
|
sha256 = ?
|
|
|
|
|
AND
|
|
|
|
|
upload_id = ?`
|
|
|
|
|
QGetByHashID = `SELECT upload_id,
|
|
|
|
|
sha256,
|
|
|
|
|
file_name,
|
|
|
|
|
file_type,
|
|
|
|
|
file_size,
|
|
|
|
|
file_ext,
|
|
|
|
|
status
|
|
|
|
|
FROM upload WHERE sha256 = ? AND upload_id = ?`
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
@ -84,26 +90,27 @@ var (
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Upload struct {
|
|
|
|
|
ID string `db:"upload_id"`
|
|
|
|
|
SHA256 string `db:"sha256"`
|
|
|
|
|
FileName string `db:"file_name"`
|
|
|
|
|
FileType string `db:"file_type"`
|
|
|
|
|
FileSize int64 `db:"file_size"`
|
|
|
|
|
FileExt string `db:"file_ext"`
|
|
|
|
|
Status int `db:"status"`
|
|
|
|
|
ID ksuid.KSUID `db:"upload_id"`
|
|
|
|
|
SHA256 string `db:"sha256"`
|
|
|
|
|
FileName string `db:"file_name"`
|
|
|
|
|
FileType string `db:"file_type"`
|
|
|
|
|
FileSize int64 `db:"file_size"`
|
|
|
|
|
FileExt string `db:"file_ext"`
|
|
|
|
|
Status int `db:"status"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CacheSetUploadStatus(id string, status int) error {
|
|
|
|
|
key := fmt.Sprintf("upload_status_%s", id)
|
|
|
|
|
|
|
|
|
|
return DB.Redis.Do(radix.FlatCmd(nil, "SET", key, status))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true if id exists in DB
|
|
|
|
|
func IdExists(id string) (exists bool, err error) {
|
|
|
|
|
qUploadExists := `
|
|
|
|
|
SELECT EXISTS (SELECT upload_id FROM upload where upload_id = ?)
|
|
|
|
|
`
|
|
|
|
|
err = DB.Sql.Get(&exists, qUploadExists, id)
|
|
|
|
|
key := fmt.Sprintf("upload_status_%s", id)
|
|
|
|
|
log.Println("calling exists")
|
|
|
|
|
|
|
|
|
|
// No result found is also no result
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
err = nil
|
|
|
|
|
}
|
|
|
|
|
err = DB.Redis.Do(radix.Cmd(&exists, "EXISTS", key))
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@ -111,6 +118,7 @@ func IdExists(id string) (exists bool, err error) {
|
|
|
|
|
// Get a file by upload id and hash
|
|
|
|
|
func GetByHashID(sha256 string, id string) (*Upload, error) {
|
|
|
|
|
var up Upload
|
|
|
|
|
|
|
|
|
|
err := DB.Sql.Get(&up, QGetByHashID, sha256, id)
|
|
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|