174 lines
5.5 KiB
Go
174 lines
5.5 KiB
Go
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"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
|
|
}
|
|
|
|
fileHeader, err := c.FormFile("file")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "file is required")
|
|
return
|
|
}
|
|
|
|
result, svcErr := h.svc.AccountUpload(c.Request.Context(), accountID, service.AccountUploadRequest{
|
|
FileHeader: fileHeader,
|
|
})
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// 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)
|
|
}
|