Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand only UTF-8 file #430

Merged
merged 4 commits into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion gitlab/blob_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"time"
"unicode/utf8"
)

type blobFetcher struct {
Expand Down Expand Up @@ -52,6 +53,10 @@ func (f *blobFetcher) fetchPath(path string, client *gitlab.Client, isDebugLoggi
fmt.Printf("[DEBUG] blobFetcher (%s): fileBody=%s\n", duration, fileBody)
}

if !utf8.ValidString(fileBody) {
return nil
}

if lineMatched == nil {
selectedFile = fileBody
return nil
Expand Down Expand Up @@ -104,9 +109,14 @@ func (f *blobFetcher) fetchPath(path string, client *gitlab.Client, isDebugLoggi
title = fmt.Sprintf("%s:%s", title, lineRange)
}

description := ""
if selectedFile != "" {
description = fmt.Sprintf("```\n%s\n```", selectedFile)
}

page := &Page{
Title: title,
Description: fmt.Sprintf("```\n%s\n```", selectedFile),
Description: description,
AuthorName: "",
AuthorAvatarURL: "",
AvatarURL: project.AvatarURL,
Expand Down
Binary file added gitlab/testdata/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions gitlab/url_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ func TestGitlabUrlParser_FetchURL(t *testing.T) {
"http://example.com/api/v4/projects/diaspora%2Fdiaspora-project-site/repository/files/gitlabci-templates%2Fcontinuous_bundle_update.yml/raw?ref=master",
httpmock.NewStringResponder(200, testutil.ReadTestData("testdata/gitlabci-templates/continuous_bundle_update.yml")),
)
httpmock.RegisterResponder(
"GET",
"http://example.com/api/v4/projects/diaspora%2Fdiaspora-project-site/repository/files/icon.png/raw?ref=master",
httpmock.NewStringResponder(200, testutil.ReadTestData("testdata/icon.png")),
)
httpmock.RegisterResponder(
"GET",
"http://example.com/api/v4/projects/diaspora%2Fdiaspora-project-site/jobs/8",
Expand Down Expand Up @@ -588,6 +593,24 @@ func TestGitlabUrlParser_FetchURL(t *testing.T) {
Color: "",
},
},
{
name: "Blob URL (non UTF-8 file)",
args: args{
url: "http://example.com/diaspora/diaspora-project-site/blob/master/icon.png",
},
want: &Page{
Title: "icon.png",
Description: "",
AuthorName: "",
AuthorAvatarURL: "",
AvatarURL: "http://example.com/uploads/project/avatar/3/uploads/avatar.png",
CanTruncateDescription: false,
FooterTitle: "diaspora/diaspora-project-site",
FooterURL: "http://example.com/diaspora/diaspora-project-site",
FooterTime: nil,
Color: "",
},
},
{
name: "Job URL",
args: args{
Expand Down