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

View File

@ -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 {

View File

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