Skip to content

Commit

Permalink
expose a few more apis
Browse files Browse the repository at this point in the history
  • Loading branch information
jhaynie committed Sep 19, 2017
1 parent d0f0058 commit 286ca6f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
31 changes: 20 additions & 11 deletions linguist.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ func CheckPreoptimizationCache(filename string) Result {
}
}
if matched == len(p.Matchers) {
ex, r := IsExcluded(filename, nil)
if ex {
return *r
}
// make a copy so that the result can't be mutated
l := Language(*p.Result.Result.Language)
result := Result{
Expand Down Expand Up @@ -291,7 +295,7 @@ func CheckPreoptimizationCache(filename string) Result {

// GetLanguageDetails returns the linguist results for a given file
func GetLanguageDetails(ctx context.Context, filename string, body []byte, skip ...bool) (Result, error) {
if ex, r := isExcluded(filename, body); ex {
if ex, r := IsExcluded(filename, body); ex {
return *r, nil
}
if len(skip) == 0 || !skip[0] {
Expand Down Expand Up @@ -339,7 +343,7 @@ func GetLanguageDetailsMultiple(ctx context.Context, files []*File, skipCache ..
skip = true
}
for i, file := range files {
if ex, r := isExcluded(file.filename, file.body); ex {
if ex, r := IsExcluded(file.filename, file.body); ex {
results = append(results, *r)
continue
}
Expand Down Expand Up @@ -422,7 +426,8 @@ func attempt(ctx context.Context, jsonbuf string, url string, authtoken string,
return []Result{noResult}, errors.New(result.Message)
}

func isLikelyBinary(body []byte) bool {
// IsLikelyBinary returns true if the body is likely a binary buffer
func IsLikelyBinary(body []byte) bool {
ct := http.DetectContentType(body)
if strings.HasPrefix(ct, "image/") || strings.HasPrefix(ct, "video/") {
return true
Expand All @@ -440,8 +445,9 @@ func isLikelyBinary(body []byte) bool {
// MaxBufferSize is the large size in bytes that a buffer can be before it's considered "large"
const MaxBufferSize = 100000

func isLargeBuffer(body []byte) bool {
return len(body) > MaxBufferSize
// IsLargeBuffer returns true if the size is larger than MaxBufferSize
func IsLargeBuffer(size int) bool {
return size > MaxBufferSize
}

func isFilenameExcluded(name string) bool {
Expand Down Expand Up @@ -605,12 +611,15 @@ func RemoveExcludedRule(match Match) {
}
}

func isExcluded(filename string, body []byte) (bool, *Result) {
if isLikelyBinary(body) {
return true, binaryResult
}
if isLargeBuffer(body) {
return true, largeResult
// IsExcluded returns true if the filename and optional body is excluded. If nil body, will only check for filename
func IsExcluded(filename string, body []byte) (bool, *Result) {
if body != nil {
if IsLikelyBinary(body) {
return true, binaryResult
}
if IsLargeBuffer(len(body)) {
return true, largeResult
}
}
if isFilenameExcluded(filename) {
return true, excludedResult
Expand Down
16 changes: 16 additions & 0 deletions linguist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1367,3 +1367,19 @@ func TestSkipPreoptimizationCache(t *testing.T) {
t.Fatal("expected IsCached to be false")
}
}

func TestPreoptimizationExcluded(t *testing.T) {
r := CheckPreoptimizationCache("package.json")
if r.Success == false {
t.Fatal("expected result.success to be true")
}
if r.Result != nil {
t.Fatal("expected result to be nil")
}
if r.IsCached {
t.Fatal("expected IsCached to be false")
}
if !r.IsExcluded {
t.Fatal("expected IsExcluded to be true")
}
}

0 comments on commit 286ca6f

Please sign in to comment.