1
0

Fix CSV errors for rendering (#514)

هذا الالتزام موجود في:
Thomas Miceli
2025-09-29 19:02:33 +02:00
ملتزم من قبل GitHub
الأصل 92c5569538
التزام 5ef5518795
7 ملفات معدلة مع 21 إضافات و15 حذوفات

عرض الملف

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

عرض الملف

@@ -9,6 +9,7 @@ import (
type MimeType struct { type MimeType struct {
ContentType string ContentType string
extension string
} }
func (mt MimeType) IsText() bool { func (mt MimeType) IsText() bool {
@@ -16,7 +17,8 @@ func (mt MimeType) IsText() bool {
} }
func (mt MimeType) IsCSV() 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 { func (mt MimeType) IsImage() bool {
@@ -84,6 +86,6 @@ func (mt MimeType) RenderType() string {
return "Binary" return "Binary"
} }
func DetectMimeType(data []byte) MimeType { func DetectMimeType(data []byte, extension string) MimeType {
return MimeType{mimetype.Detect(data).String()} return MimeType{mimetype.Detect(data).String(), extension}
} }

عرض الملف

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

عرض الملف

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

عرض الملف

@@ -9,7 +9,7 @@ import (
) )
type RenderedFile interface { type RenderedFile interface {
getFile() *git.File InternalType() string
} }
type NonHighlightedFile struct { type NonHighlightedFile struct {
@@ -17,8 +17,8 @@ type NonHighlightedFile struct {
Type string `json:"type"` Type string `json:"type"`
} }
func (r NonHighlightedFile) getFile() *git.File { func (r NonHighlightedFile) InternalType() string {
return r.File return "NonHighlightedFile"
} }
func RenderFiles(files []*git.File) []RenderedFile { func RenderFiles(files []*git.File) []RenderedFile {
@@ -54,7 +54,11 @@ func processFile(file *git.File) RenderedFile {
if mt.IsCSV() { if mt.IsCSV() {
rendered, err := renderCsvFile(file) rendered, err := renderCsvFile(file)
if err != nil { 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 return rendered
} else if mt.IsText() && filepath.Ext(file.Filename) == ".md" { } else if mt.IsText() && filepath.Ext(file.Filename) == ".md" {

عرض الملف

@@ -55,7 +55,7 @@
</div> </div>
<div class="overflow-auto"> <div class="overflow-auto">
{{ if $file.MimeType.IsText }} {{ if $file.MimeType.IsText }}
{{ if $file.MimeType.IsCSV }} {{ if eq $file.InternalType "CSVFile" }}
<table class="csv-table"> <table class="csv-table">
<thead> <thead>
<tr> <tr>

عرض الملف

@@ -17,7 +17,7 @@
</div> </div>
{{ end }} {{ end }}
{{ if $file.MimeType.IsText }} {{ if $file.MimeType.IsText }}
{{ if $file.MimeType.IsCSV }} {{ if eq $file.InternalType "CSVFile" }}
<table class="csv-table"> <table class="csv-table">
<thead> <thead>
<tr> <tr>