github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/core/corehttp/gateway_indexPage.go (about)

     1  package corehttp
     2  
     3  import (
     4  	"html/template"
     5  	"path"
     6  	"strings"
     7  
     8  	"github.com/ipfs/go-ipfs/assets"
     9  )
    10  
    11  // structs for directory listing
    12  type listingTemplateData struct {
    13  	Listing  []directoryItem
    14  	Path     string
    15  	BackLink string
    16  }
    17  
    18  type directoryItem struct {
    19  	Size string
    20  	Name string
    21  	Path string
    22  }
    23  
    24  var listingTemplate *template.Template
    25  
    26  func init() {
    27  	assetPath := "../vendor/dir-index-html-v1.0.0/"
    28  	knownIconsBytes, err := assets.Asset(assetPath + "knownIcons.txt")
    29  	if err != nil {
    30  		panic(err)
    31  	}
    32  	knownIcons := make(map[string]struct{})
    33  	for _, ext := range strings.Split(strings.TrimSuffix(string(knownIconsBytes), "\n"), "\n") {
    34  		knownIcons[ext] = struct{}{}
    35  	}
    36  
    37  	// helper to guess the type/icon for it by the extension name
    38  	iconFromExt := func(name string) string {
    39  		ext := path.Ext(name)
    40  		_, ok := knownIcons[ext]
    41  		if !ok {
    42  			// default blank icon
    43  			return "ipfs-_blank"
    44  		}
    45  		return "ipfs-" + ext[1:] // slice of the first dot
    46  	}
    47  
    48  	// Directory listing template
    49  	dirIndexBytes, err := assets.Asset(assetPath + "dir-index.html")
    50  	if err != nil {
    51  		panic(err)
    52  	}
    53  
    54  	listingTemplate = template.Must(template.New("dir").Funcs(template.FuncMap{
    55  		"iconFromExt": iconFromExt,
    56  	}).Parse(string(dirIndexBytes)))
    57  }