- repository.Resources (insert/list/get/soft-delete scoping owner_id, UNIQUE(parent_id,name) → NAME_CONFLICT) + Devices.Upsert au register
- service.Resources : DTOs {id,name,size,mimeType,folderId,createdAt,updatedAt}, upload → UPLOAD_DIR/<device>/<id>.<ext> (nettoyage si métadonnée échoue)
- handlers : files list/get/delete/upload + folders racines ; routes consolidées dans handlers.RegisterRoutes
- dbtest package : test DB jetée par repo (open+reset+migrate, skip si PG down) ; tests repository + handlers end-to-end (register→token→CRUD, scoping cross-device, FILE_TOO_LARGE, NAME_CONFLICT)
- docs: /devices persiste le device, layout UPLOAD_DIR par device, codes NAME_CONFLICT/SERVICE_UNAVAILABLE
46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"regexp"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/vaultdrop/backend/pkg/api"
|
|
)
|
|
|
|
var deviceIDPattern = regexp.MustCompile(`^[0-9a-f]{32}$`)
|
|
|
|
type DeviceRegisterRequest struct {
|
|
DeviceID string `json:"deviceId"`
|
|
}
|
|
|
|
func DevicesRegister(c *gin.Context) {
|
|
|
|
var req DeviceRegisterRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil || req.DeviceID == "" {
|
|
api.Error(c, 400, "INVALID_REQUEST", "missing deviceId")
|
|
return
|
|
}
|
|
if !deviceIDPattern.MatchString(req.DeviceID) {
|
|
api.Error(c, 400, "INVALID_DEVICE_ID", "deviceId must be 32 lowercase hex chars")
|
|
return
|
|
}
|
|
|
|
if Store == nil || Store.Repository == nil {
|
|
api.Error(c, 503, "SERVICE_UNAVAILABLE", "backend not initialized")
|
|
return
|
|
}
|
|
if err := Store.Repository.Devices.Upsert(req.DeviceID); err != nil {
|
|
api.Error(c, 500, "INTERNAL", "could not persist device")
|
|
return
|
|
}
|
|
|
|
token, err := Auth.Issue(req.DeviceID)
|
|
if err != nil {
|
|
api.Error(c, 500, "TOKEN_ERROR", "could not issue token")
|
|
return
|
|
}
|
|
|
|
api.OK(c, gin.H{"deviceId": req.DeviceID, "token": token})
|
|
|
|
}
|