Improve Sheet model
This commit is contained in:
@@ -18,7 +18,7 @@ func InitDatabase() {
|
||||
}
|
||||
|
||||
// Auto migrate the schema
|
||||
err = DB.AutoMigrate(&models.User{}, &models.MusicSheet{})
|
||||
err = DB.AutoMigrate(&models.User{}, &models.Sheet{})
|
||||
if err != nil {
|
||||
log.Fatal("Failed to migrate database:", err)
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ require (
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.14.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
|
||||
@@ -29,6 +29,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
var jwtSecret = []byte("your-secret-key") // In production, use environment variable
|
||||
var jwtSecret = []byte("your-secret-key") // TODO: In production, use environment variable
|
||||
|
||||
type RegisterRequest struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -8,10 +9,12 @@ import (
|
||||
"path/filepath"
|
||||
"sheetless-server/database"
|
||||
"sheetless-server/models"
|
||||
"sheetless-server/utils"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const uploadDir = "./uploads"
|
||||
@@ -79,14 +82,29 @@ func UploadSheet(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
fileHash, err := utils.FileHashFromUpload(out)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed calculating hash"})
|
||||
return
|
||||
}
|
||||
|
||||
uuid, err := GenerateNonexistentSheetUuid()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed generating sheet uuid"})
|
||||
return
|
||||
}
|
||||
|
||||
// Create database record
|
||||
sheet := models.MusicSheet{
|
||||
sheet := models.Sheet{
|
||||
Uuid: *uuid,
|
||||
Title: title,
|
||||
Composer: composer,
|
||||
Description: description,
|
||||
FilePath: filePath,
|
||||
FileSize: fileInfo.Size(),
|
||||
UserID: userID.(uint),
|
||||
FileHash: fileHash,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := database.DB.Create(&sheet).Error; err != nil {
|
||||
@@ -103,7 +121,7 @@ func UploadSheet(c *gin.Context) {
|
||||
}
|
||||
|
||||
func ListSheets(c *gin.Context) {
|
||||
var sheets []models.MusicSheet
|
||||
var sheets []models.Sheet
|
||||
|
||||
query := database.DB.Preload("User")
|
||||
|
||||
@@ -131,7 +149,7 @@ func DownloadSheet(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var sheet models.MusicSheet
|
||||
var sheet models.Sheet
|
||||
if err := database.DB.First(&sheet, uint(id)).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Sheet not found"})
|
||||
return
|
||||
@@ -148,3 +166,25 @@ func DownloadSheet(c *gin.Context) {
|
||||
c.Header("Content-Type", "application/pdf")
|
||||
c.File(sheet.FilePath)
|
||||
}
|
||||
|
||||
func GenerateNonexistentSheetUuid() (*uuid.UUID, error) {
|
||||
for i := 0; i < 10; i++ {
|
||||
uuid := uuid.New()
|
||||
|
||||
var exists bool
|
||||
err := database.DB.Model(&models.Sheet{}).
|
||||
Select("count(*) > 0").
|
||||
Where("uuid = ?", uuid).
|
||||
Find(&exists).
|
||||
Error
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return &uuid, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("Somehow unable to generate new uuid for sheet.")
|
||||
}
|
||||
|
||||
@@ -3,17 +3,18 @@ package models
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type MusicSheet struct {
|
||||
ID uint `json:"id" gorm:"primaryKey"`
|
||||
type Sheet struct {
|
||||
Uuid uuid.UUID `json:"uuid" gorm:"primaryKey"`
|
||||
Title string `json:"title" gorm:"not null"`
|
||||
Composer string `json:"composer"`
|
||||
Description string `json:"description"`
|
||||
FilePath string `json:"file_path" gorm:"not null"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
UserID uint `json:"user_id"`
|
||||
FileHash uint64 `json:"file_hash"`
|
||||
User User `json:"user" gorm:"foreignKey:UserID"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
@@ -22,8 +22,8 @@ func SetupRoutes(r *gin.Engine) {
|
||||
sheets := api.Group("/sheets")
|
||||
{
|
||||
sheets.POST("/upload", handlers.UploadSheet)
|
||||
sheets.GET("", handlers.ListSheets)
|
||||
sheets.GET("/download/:id", handlers.DownloadSheet)
|
||||
sheets.GET("/list", handlers.ListSheets)
|
||||
sheets.GET("/get/:id", handlers.DownloadSheet)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BIN
src/sheetless-server
Executable file
BIN
src/sheetless-server
Executable file
Binary file not shown.
32
src/utils/filehash.go
Normal file
32
src/utils/filehash.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"hash/fnv"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
)
|
||||
|
||||
func FileHashFromUpload(file multipart.File) (uint64, error) {
|
||||
h := fnv.New64a()
|
||||
if _, err := io.Copy(h, file); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return h.Sum64(), nil
|
||||
}
|
||||
|
||||
func FileHash(path string) (uint64, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
h := fnv.New64a()
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return h.Sum64(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user