add to db
This commit is contained in:
parent
a3312ef6f8
commit
93aec1336d
5 changed files with 39 additions and 5 deletions
|
@ -1,7 +1,9 @@
|
|||
package routes
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
|
@ -15,7 +17,6 @@ func RegisterUploadRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
|||
user := claims["user"].(auth.User)
|
||||
|
||||
uid := user.ID
|
||||
|
||||
if uid == "" {
|
||||
c.JSON(401, gin.H{"error": "unauthorized"})
|
||||
return
|
||||
|
@ -28,12 +29,30 @@ func RegisterUploadRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
|||
}
|
||||
|
||||
filePath := filepath.Join(cfg.ImagePath, uid, file.Filename)
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
|
||||
c.JSON(500, gin.H{"error": "failed to create directory"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.SaveUploadedFile(file, filePath); err != nil {
|
||||
c.JSON(500, gin.H{"error": "failed to save file"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"message": "file uploaded successfully"})
|
||||
fileMeta := types.File{
|
||||
ID: uid + "_" + file.Filename,
|
||||
Path: filePath,
|
||||
Owner: uid,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := cfg.Database.Create(&fileMeta).Error; err != nil {
|
||||
c.JSON(500, gin.H{"error": "failed to save file metadata"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"message": "file uploaded successfully", "file_id": fileMeta.ID})
|
||||
})
|
||||
|
||||
api.GET("/:name", func(c *gin.Context) {
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"stereo.cat/backend/internal/auth/client"
|
||||
|
@ -16,6 +18,13 @@ type StereoConfig struct {
|
|||
ImagePath string
|
||||
Router *gin.Engine
|
||||
Client client.Client
|
||||
Database *gorm.DB
|
||||
JWTSecret string
|
||||
Database *gorm.DB
|
||||
JWTSecret string
|
||||
}
|
||||
|
||||
type File struct {
|
||||
ID string `gorm:"primaryKey"`
|
||||
Path string `gorm:"not null;index"`
|
||||
Owner string `gorm:"not null;index"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue