From 57f81e5dd716162bb2b0d48115f85ed3b37e7eb2 Mon Sep 17 00:00:00 2001 From: Evan Reichard Date: Tue, 13 May 2025 12:14:57 -0400 Subject: [PATCH] fix(api): ko json content type --- api/api.go | 14 +++++++------- api/ko-routes.go | 25 ++++++++++++++++--------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/api/api.go b/api/api.go index 74335d4..49f3697 100644 --- a/api/api.go +++ b/api/api.go @@ -325,6 +325,13 @@ func (api *API) loadTemplates( return nil } +func (api *API) templateMiddleware(router *gin.Engine) gin.HandlerFunc { + return func(c *gin.Context) { + router.HTMLRender = *api.generateTemplates() + c.Next() + } +} + func loggingMiddleware(c *gin.Context) { // Start timer startTime := time.Now() @@ -360,10 +367,3 @@ func loggingMiddleware(c *gin.Context) { // Log result log.WithFields(logData).Info(fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path)) } - -func (api *API) templateMiddleware(router *gin.Engine) gin.HandlerFunc { - return func(c *gin.Context) { - router.HTMLRender = *api.generateTemplates() - c.Next() - } -} diff --git a/api/ko-routes.go b/api/ko-routes.go index 4cac760..3e3645f 100644 --- a/api/ko-routes.go +++ b/api/ko-routes.go @@ -72,7 +72,7 @@ type requestDocumentID struct { } func (api *API) koAuthorizeUser(c *gin.Context) { - c.JSON(200, gin.H{ + koJSON(c, 200, gin.H{ "authorized": "OK", }) } @@ -121,7 +121,7 @@ func (api *API) koSetProgress(c *gin.Context) { return } - c.JSON(http.StatusOK, gin.H{ + koJSON(c, http.StatusOK, gin.H{ "document": progress.DocumentID, "timestamp": progress.CreatedAt, }) @@ -147,7 +147,7 @@ func (api *API) koGetProgress(c *gin.Context) { if err == sql.ErrNoRows { // Not Found - c.JSON(http.StatusOK, gin.H{}) + koJSON(c, http.StatusOK, gin.H{}) return } else if err != nil { log.Error("GetDocumentProgress DB Error:", err) @@ -155,7 +155,7 @@ func (api *API) koGetProgress(c *gin.Context) { return } - c.JSON(http.StatusOK, gin.H{ + koJSON(c, http.StatusOK, gin.H{ "document": progress.DocumentID, "percentage": progress.Percentage, "progress": progress.Progress, @@ -247,7 +247,7 @@ func (api *API) koAddActivities(c *gin.Context) { return } - c.JSON(http.StatusOK, gin.H{ + koJSON(c, http.StatusOK, gin.H{ "added": len(rActivity.Activity), }) } @@ -298,7 +298,7 @@ func (api *API) koCheckActivitySync(c *gin.Context) { return } - c.JSON(http.StatusOK, gin.H{ + koJSON(c, http.StatusOK, gin.H{ "last_sync": parsedTime.Unix(), }) } @@ -352,7 +352,7 @@ func (api *API) koAddDocuments(c *gin.Context) { return } - c.JSON(http.StatusOK, gin.H{ + koJSON(c, http.StatusOK, gin.H{ "changed": len(rNewDocs.Documents), }) } @@ -447,7 +447,7 @@ func (api *API) koCheckDocumentsSync(c *gin.Context) { rCheckDocSync.Delete = deletedDocIDs } - c.JSON(http.StatusOK, rCheckDocSync) + koJSON(c, http.StatusOK, rCheckDocSync) } func (api *API) koUploadExistingDocument(c *gin.Context) { @@ -534,7 +534,7 @@ func (api *API) koUploadExistingDocument(c *gin.Context) { return } - c.JSON(http.StatusOK, gin.H{ + koJSON(c, http.StatusOK, gin.H{ "status": "ok", }) } @@ -589,3 +589,10 @@ func getFileMD5(filePath string) (*string, error) { return &fileHash, nil } + +// koJSON forces koJSON Content-Type to only return `application/json`. This is addressing +// the following issue: https://github.com/koreader/koreader/issues/13629 +func koJSON(c *gin.Context, code int, obj any) { + c.Header("Content-Type", "application/json") + c.JSON(code, obj) +}