github.com/go-enry/go-enry@v1.7.3/utils.go (about)

     1  package enry
     2  
     3  import (
     4  	"bytes"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"gopkg.in/src-d/enry.v1/data"
     9  )
    10  
    11  const binSniffLen = 8000
    12  
    13  var configurationLanguages = map[string]bool{
    14  	"XML": true, "JSON": true, "TOML": true, "YAML": true, "INI": true, "SQL": true,
    15  }
    16  
    17  // IsConfiguration tells if filename is in one of the configuration languages.
    18  func IsConfiguration(path string) bool {
    19  	language, _ := GetLanguageByExtension(path)
    20  	_, is := configurationLanguages[language]
    21  	return is
    22  }
    23  
    24  // IsImage tells if a given file is an image (PNG, JPEG or GIF format).
    25  func IsImage(path string) bool {
    26  	extension := filepath.Ext(path)
    27  	if extension == ".png" || extension == ".jpg" || extension == ".jpeg" || extension == ".gif" {
    28  		return true
    29  	}
    30  
    31  	return false
    32  }
    33  
    34  // GetMIMEType returns a MIME type of a given file based on its languages.
    35  func GetMIMEType(path string, language string) string {
    36  	if mime, ok := data.LanguagesMime[language]; ok {
    37  		return mime
    38  	}
    39  
    40  	if IsImage(path) {
    41  		return "image/" + filepath.Ext(path)[1:]
    42  	}
    43  
    44  	return "text/plain"
    45  }
    46  
    47  // IsDocumentation returns whether or not path is a documentation path.
    48  func IsDocumentation(path string) bool {
    49  	return data.DocumentationMatchers.Match(path)
    50  }
    51  
    52  // IsDotFile returns whether or not path has dot as a prefix.
    53  func IsDotFile(path string) bool {
    54  	base := filepath.Base(filepath.Clean(path))
    55  	return strings.HasPrefix(base, ".") && base != "."
    56  }
    57  
    58  // IsVendor returns whether or not path is a vendor path.
    59  func IsVendor(path string) bool {
    60  	return data.VendorMatchers.Match(path)
    61  }
    62  
    63  // IsBinary detects if data is a binary value based on:
    64  // http://git.kernel.org/cgit/git/git.git/tree/xdiff-interface.c?id=HEAD#n198
    65  func IsBinary(data []byte) bool {
    66  	if len(data) > binSniffLen {
    67  		data = data[:binSniffLen]
    68  	}
    69  
    70  	if bytes.IndexByte(data, byte(0)) == -1 {
    71  		return false
    72  	}
    73  
    74  	return true
    75  }