Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
199 lines
6.3 KiB
Go
199 lines
6.3 KiB
Go
package v1
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/service"
|
|
"github.com/gochat/gochat/pkg/response"
|
|
)
|
|
|
|
// UploadHandler handles file upload endpoints (account-level + widget direct + account direct).
|
|
type UploadHandler struct {
|
|
svc *service.UploadService
|
|
}
|
|
|
|
// NewUploadHandler creates a new UploadHandler.
|
|
func NewUploadHandler(svc *service.UploadService) *UploadHandler {
|
|
return &UploadHandler{svc: svc}
|
|
}
|
|
|
|
// Upload handles POST /api/v1/accounts/:id/upload — account-level file upload.
|
|
// Reference: Chatwoot api/v1/accounts/:account_id/upload
|
|
func (h *UploadHandler) Upload(c *gin.Context) {
|
|
accountID := getAccountID(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "account_id is required")
|
|
return
|
|
}
|
|
|
|
var result *service.UploadResponse
|
|
var svcErr error
|
|
if strings.Contains(c.GetHeader("Content-Type"), "application/json") {
|
|
var req struct {
|
|
ExternalURL string `json:"external_url"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil || strings.TrimSpace(req.ExternalURL) == "" {
|
|
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "missing input"})
|
|
return
|
|
}
|
|
result, svcErr = h.svc.AccountUploadFromURL(c.Request.Context(), accountID, strings.TrimSpace(req.ExternalURL))
|
|
} else {
|
|
fileHeader, err := c.FormFile("attachment")
|
|
if err != nil {
|
|
fileHeader, err = c.FormFile("file")
|
|
}
|
|
if err != nil {
|
|
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "missing input"})
|
|
return
|
|
}
|
|
result, svcErr = h.svc.AccountUpload(c.Request.Context(), accountID, service.AccountUploadRequest{
|
|
FileHeader: fileHeader,
|
|
})
|
|
}
|
|
if svcErr != nil {
|
|
status := http.StatusUnprocessableEntity
|
|
if errors.Is(svcErr, gorm.ErrRecordNotFound) {
|
|
status = http.StatusNotFound
|
|
}
|
|
c.JSON(status, gin.H{"error": svcErr.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"file_url": result.FileURL,
|
|
"blob_id": result.UploadUUID,
|
|
"blob_key": result.UploadUUID,
|
|
})
|
|
}
|
|
|
|
// DirectUpload handles POST /api/v1/widget/direct_uploads — widget direct file upload.
|
|
// Reference: Chatwoot POST /widget/direct_uploads
|
|
func (h *UploadHandler) DirectUpload(c *gin.Context) {
|
|
if strings.Contains(c.GetHeader("Content-Type"), "application/json") {
|
|
var req service.ActiveStorageDirectUploadRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "invalid direct upload metadata")
|
|
return
|
|
}
|
|
req.WebsiteToken = c.Query("website_token")
|
|
req.AuthToken = c.GetHeader("X-Auth-Token")
|
|
result, svcErr := h.svc.CreateWidgetDirectUpload(c.Request.Context(), req)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, result)
|
|
return
|
|
}
|
|
|
|
fileHeader, err := c.FormFile("file")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "file is required")
|
|
return
|
|
}
|
|
|
|
result, svcErr := h.svc.WidgetDirectUpload(c.Request.Context(), service.WidgetDirectUploadRequest{
|
|
FileHeader: fileHeader,
|
|
})
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
func (h *UploadHandler) CompleteWidgetDirectUpload(c *gin.Context) {
|
|
result, svcErr := h.svc.CompleteWidgetDirectUpload(c.Request.Context(), c.Param("upload_uuid"), c.Request.Body)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// AccountDirectUpload handles POST /api/v1/accounts/:id/direct_uploads — account-level staged upload.
|
|
// Returns a blob/UUID for later attachment to messages.
|
|
// Reference: Chatwoot POST /api/v1/accounts/:account_id/direct_uploads
|
|
func (h *UploadHandler) AccountDirectUpload(c *gin.Context) {
|
|
accountID := getAccountID(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "account_id is required")
|
|
return
|
|
}
|
|
|
|
fileHeader, err := c.FormFile("file")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "file is required")
|
|
return
|
|
}
|
|
|
|
result, svcErr := h.svc.AccountDirectUpload(c.Request.Context(), accountID, service.AccountDirectUploadRequest{
|
|
FileHeader: fileHeader,
|
|
})
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// ConversationDirectUpload handles Chatwoot's nested conversation direct upload
|
|
// endpoint used by the reused dashboard message composer.
|
|
// Reference: POST /api/v1/accounts/:account_id/conversations/:conversation_id/direct_uploads
|
|
func (h *UploadHandler) ConversationDirectUpload(c *gin.Context) {
|
|
accountID := getAccountID(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "account_id is required")
|
|
return
|
|
}
|
|
conversationID, err := parseUintParam(c, "conversation_id")
|
|
if err != nil || conversationID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
|
|
return
|
|
}
|
|
|
|
if !strings.Contains(c.GetHeader("Content-Type"), "application/json") {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "invalid direct upload metadata")
|
|
return
|
|
}
|
|
|
|
var req service.ActiveStorageDirectUploadRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "invalid direct upload metadata")
|
|
return
|
|
}
|
|
result, svcErr := h.svc.CreateConversationDirectUpload(c.Request.Context(), accountID, conversationID, req)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
func (h *UploadHandler) CompleteConversationDirectUpload(c *gin.Context) {
|
|
accountID := getAccountID(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "account_id is required")
|
|
return
|
|
}
|
|
conversationID, err := parseUintParam(c, "conversation_id")
|
|
if err != nil || conversationID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
|
|
return
|
|
}
|
|
|
|
result, svcErr := h.svc.CompleteConversationDirectUpload(c.Request.Context(), accountID, conversationID, c.Param("upload_uuid"), c.Request.Body)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
response.OK(c, result)
|
|
}
|