Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions uiapi/handlers.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package uiapi

import (
_ "embed"
"fmt"
"log"
"net/http"
"os"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -806,6 +806,9 @@ func (h handler) DownloadByUrl(c echo.Context) error {
return nil
}

//go:embed readme_content.txt
var readmeContent []byte

// @ID DownloadReadme
// @Tags ui_api
// @Description Retorna um README sobre o pacote de dados
Expand All @@ -818,7 +821,7 @@ func (h handler) DownloadByUrl(c echo.Context) error {
// @Failure 500 {string} string "Algo deu errado ao retornar o README"
// @Router /uiapi/v2/readme [get]
func (h handler) DownloadReadme(c echo.Context) error {
readmeFile := "uiapi/readme_content.txt"
originalContent := readmeContent
year := c.QueryParam("ano")
month := c.QueryParam("mes")
agency := c.QueryParam("orgao")
Expand Down Expand Up @@ -859,30 +862,21 @@ func (h handler) DownloadReadme(c echo.Context) error {
newLines = "Não identificamos potenciais falhas na origem destes dados."
}

originalContent, err := os.ReadFile(readmeFile)
if err != nil {
return c.JSON(http.StatusInternalServerError, fmt.Sprintf("erro ao ler o readme original: %q", err))
}
// Adicionar as novas linhas ao conteúdo original
updatedContent := "\n**Observações sobre este conjunto de dados**:\n\n" +
newLines +
" Em sua análise, esteja atento a possíveis valores estranhos.\n\n"

// Organizando para que seja o 2º tópico
newContent := append(originalContent[:1733], append([]byte(updatedContent), originalContent[1734:]...)...)

// Gravar o conteúdo atualizado no arquivo temporário
readmeFile = "readme_atualizado.txt"
err = os.WriteFile(readmeFile, newContent, 0644)
if err != nil {
return c.JSON(http.StatusInternalServerError, fmt.Sprintf("erro ao gravar o arquivo temporário: %q", err))
}
defer os.Remove(readmeFile)
originalContent = newContent
}

c.Response().Header().Set("Content-Disposition", "attachment; filename=README.txt")
c.Response().Header().Set("Content-Type", "text/plain")
if err := c.File(readmeFile); err != nil {
c.Response().WriteHeader(http.StatusOK)
_, err = c.Response().Write(originalContent)
if err != nil {
return c.JSON(http.StatusInternalServerError, fmt.Sprintf("erro tentando fazer download do readme: %q", err))
}
return nil
Expand Down