Fix CSV errors for rendering (#514)

This commit is contained in:
Thomas Miceli
2025-09-29 19:02:33 +02:00
committed by GitHub
parent 92c5569538
commit 5ef5518795
7 changed files with 21 additions and 15 deletions

View File

@@ -431,7 +431,7 @@ func (gist *Gist) Files(revision string, truncate bool) ([]*git.File, error) {
HumanSize: humanize.IBytes(fileCat.Size),
Content: fileCat.Content,
Truncated: fileCat.Truncated,
MimeType: git.DetectMimeType([]byte(shortContent)),
MimeType: git.DetectMimeType([]byte(shortContent), filepath.Ext(fileCat.Name)),
})
}
return files, err
@@ -465,7 +465,7 @@ func (gist *Gist) File(revision string, filename string, truncate bool) (*git.Fi
HumanSize: humanize.IBytes(size),
Content: content,
Truncated: truncated,
MimeType: git.DetectMimeType([]byte(shortContent)),
MimeType: git.DetectMimeType([]byte(shortContent), filepath.Ext(filename)),
}, err
}

View File

@@ -9,6 +9,7 @@ import (
type MimeType struct {
ContentType string
extension string
}
func (mt MimeType) IsText() bool {
@@ -16,7 +17,8 @@ func (mt MimeType) IsText() bool {
}
func (mt MimeType) IsCSV() bool {
return strings.Contains(mt.ContentType, "text/csv")
return strings.Contains(mt.ContentType, "text/csv") &&
(strings.HasSuffix(mt.extension, ".csv"))
}
func (mt MimeType) IsImage() bool {
@@ -84,6 +86,6 @@ func (mt MimeType) RenderType() string {
return "Binary"
}
func DetectMimeType(data []byte) MimeType {
return MimeType{mimetype.Detect(data).String()}
func DetectMimeType(data []byte, extension string) MimeType {
return MimeType{mimetype.Detect(data).String(), extension}
}

View File

@@ -15,8 +15,8 @@ type CSVFile struct {
Rows [][]string `json:"-"`
}
func (r CSVFile) getFile() *git.File {
return r.File
func (r CSVFile) InternalType() string {
return "CSVFile"
}
func renderCsvFile(file *git.File) (*CSVFile, error) {

View File

@@ -21,8 +21,8 @@ type HighlightedFile struct {
HTML string `json:"-"`
}
func (r HighlightedFile) getFile() *git.File {
return r.File
func (r HighlightedFile) InternalType() string {
return "HighlightedFile"
}
type RenderedGist struct {

View File

@@ -9,7 +9,7 @@ import (
)
type RenderedFile interface {
getFile() *git.File
InternalType() string
}
type NonHighlightedFile struct {
@@ -17,8 +17,8 @@ type NonHighlightedFile struct {
Type string `json:"type"`
}
func (r NonHighlightedFile) getFile() *git.File {
return r.File
func (r NonHighlightedFile) InternalType() string {
return "NonHighlightedFile"
}
func RenderFiles(files []*git.File) []RenderedFile {
@@ -54,7 +54,11 @@ func processFile(file *git.File) RenderedFile {
if mt.IsCSV() {
rendered, err := renderCsvFile(file)
if err != nil {
log.Error().Err(err).Msg("Error parsing CSV file for " + file.Filename)
rendered, err := highlightFile(file)
if err != nil {
log.Error().Err(err).Msg("Error rendering gist preview for " + file.Filename)
}
return rendered
}
return rendered
} else if mt.IsText() && filepath.Ext(file.Filename) == ".md" {