Fix CSV errors for rendering (#514)
هذا الالتزام موجود في:
@@ -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" {
|
||||||
|
|||||||
2
templates/pages/gist.html
مباع
2
templates/pages/gist.html
مباع
@@ -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>
|
||||||
|
|||||||
المرجع في مشكلة جديدة
حظر مستخدم