package handlers import ( "errors" "net/http" "strconv" "strings" "github.com/gin-gonic/gin" "github.com/kazier/backend/pkg/api" "github.com/kazier/backend/service" ) // parsePageParams valide page/pageSize (400 si garbage, cf. FEEDBACK A7/A16) // au lieu du fallback silencieux. Cap pageSize 200 et offset 100000. func parsePageParams(c *gin.Context) (page, pageSize int, ok bool) { page = 1 pageSize = 50 if v := c.Query("page"); v != "" { p, err := strconv.Atoi(v) if err != nil || p < 1 { api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid query param `page`") return 0, 0, false } page = p } if v := c.Query("pageSize"); v != "" { ps, err := strconv.Atoi(v) if err != nil || ps < 1 { api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid query param `pageSize`") return 0, 0, false } pageSize = ps } if pageSize > 200 { pageSize = 200 } if (page-1)*pageSize > 100000 { api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "page offset too large") return 0, 0, false } return page, pageSize, true } func FilesList(c *gin.Context) { if Store == nil { api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized") return } userID := c.GetString(UserIDKey) page, pageSize, ok := parsePageParams(c) if !ok { return } folderID := c.Query("folderId") if folderID != "" && !deviceIDPattern.MatchString(folderID) { api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid query param `folderId`") return } files, total, err := Store.ListFiles(userID, folderID, page, pageSize, c.Query("sort"), c.Query("order")) if err != nil { writeError(c, err) return } api.OKList(c, files, page, pageSize, total) } func FilesGet(c *gin.Context) { if Store == nil { api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized") return } userID := c.GetString(UserIDKey) id := c.Param("id") if !deviceIDPattern.MatchString(id) { api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found") return } file, err := Store.GetFile(userID, id) if err != nil { writeError(c, err) return } api.OK(c, file) } // FilesGetOcr expose l'extrait OCR server-side d'un fichier visible (viewer+, // own ou shared) : GET /files/:id/ocr. Réponse { text } ("" si pas encore // extrait) + updatedAt. Permet à un second device de récupérer le texte déjà // calculé sans re-téléverser les octets (cf. docs/api-v1.md §5). func FilesGetOcr(c *gin.Context) { if Store == nil { api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized") return } userID := c.GetString(UserIDKey) id := c.Param("id") if !deviceIDPattern.MatchString(id) { api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found") return } dto, err := Store.GetFileOcr(userID, id) if err != nil { writeError(c, err) return } api.OK(c, dto) } func FilesDelete(c *gin.Context) { if Store == nil { api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized") return } userID := c.GetString(UserIDKey) id := c.Param("id") if !deviceIDPattern.MatchString(id) { api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found") return } deletedID, err := Store.DeleteFile(userID, id) if err != nil { writeError(c, err) return } api.OK(c, gin.H{"id": deletedID}) } func FilesUpload(c *gin.Context) { if Store == nil { api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized") return } userID := c.GetString(UserIDKey) folderID := c.PostForm("folderId") if folderID != "" && !deviceIDPattern.MatchString(folderID) { api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid `folderId`") return } // resourceId optionnel : cible un fichier déjà connu (metadata-only de // l'outbox) pour lui attacher ses octets physiques (ex. OCR serveur). resourceID := c.PostForm("resourceId") if resourceID != "" && !deviceIDPattern.MatchString(resourceID) { api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found") return } c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, Store.MaxFileSize+1) file, err := c.FormFile("file") if err != nil { var maxBytesErr *http.MaxBytesError if errors.As(err, &maxBytesErr) { api.Error(c, http.StatusRequestEntityTooLarge, "FILE_TOO_LARGE", "file exceeds the maximum allowed size") return } api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "missing multipart field `file`") return } if file.Size > Store.MaxFileSize { api.Error(c, http.StatusRequestEntityTooLarge, "FILE_TOO_LARGE", "file exceeds the maximum allowed size") return } dto, err := Store.Upload(userID, file, folderID, resourceID) if err != nil { // Nom/extension rejetés côté service (TrimSpace, cap 255, // anti-slash/retour-ligne, cf. FEEDBACK A16) → 400, jamais 500. if errors.Is(err, service.ErrInvalidUpload) { api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid file name") return } writeError(c, err) return } api.OK(c, dto) } func FoldersList(c *gin.Context) { if Store == nil { api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized") return } userID := c.GetString(UserIDKey) folders, err := Store.ListRootFolders(userID) if err != nil { writeError(c, err) return } api.OK(c, folders) } func FilesSearch(c *gin.Context) { if Store == nil { api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized") return } userID := c.GetString(UserIDKey) q := strings.TrimSpace(c.Query("q")) if q == "" { api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "missing required query param `q`") return } if len(q) > 200 { api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "query param `q` too long (max 200)") return } page, pageSize, ok := parsePageParams(c) if !ok { return } files, total, err := Store.SearchFiles(userID, q, page, pageSize) if err != nil { writeError(c, err) return } api.OKList(c, files, page, pageSize, total) }