Working upload process + redis cache

This commit is contained in:
Chakib Benziane 2019-03-26 17:03:13 +01:00
parent 16da18eda3
commit 60ea287ccd
3 changed files with 44 additions and 27 deletions

View File

@ -28,8 +28,6 @@ func (ctrl UploadCtrl) New(c *gin.Context) {
// Create unique id // Create unique id
id := ksuid.New() id := ksuid.New()
log.Println(id)
log.Println(len(id.String()))
tx, err := db.DB.Sql.Beginx() tx, err := db.DB.Sql.Beginx()
if err != nil { if err != nil {
@ -39,9 +37,8 @@ func (ctrl UploadCtrl) New(c *gin.Context) {
for _, file := range uploadForm.Files { for _, file := range uploadForm.Files {
up := &Upload{} up := &Upload{}
up.ID = id.String() up.ID = id
log.Println(file.Name)
up.FileName, up.FileExt = utils.CleanFileName(file.Name) up.FileName, up.FileExt = utils.CleanFileName(file.Name)
up.FileSize = file.Size up.FileSize = file.Size
@ -63,6 +60,12 @@ func (ctrl UploadCtrl) New(c *gin.Context) {
return return
} }
err = CacheSetUploadStatus(id.String(), UpNew)
if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
return
}
log.Println("new upload created") log.Println("new upload created")
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK, "status": http.StatusOK,
@ -183,6 +186,12 @@ func (ctrl UploadCtrl) Upload(c *gin.Context) {
return return
} }
err = CacheSetUploadStatus(id, UpStored)
if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK, "status": http.StatusOK,
"result": fmt.Sprintf("%d files uploaded uploaded !", len(files)), "result": fmt.Sprintf("%d files uploaded uploaded !", len(files)),

View File

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

View File

@ -59,7 +59,7 @@ async function newUpload(files){
postMessage({msg: 'upload-id', id: id}) postMessage({msg: 'upload-id', id: id})
// Send the files // Send the files
//upload.send() upload.send()
}) })
} }