Ejemplo n.º 1
0
func (m *Manager) hash(name string) (string, error) {
	f, err := m.Load(name)
	if err != nil {
		return "", err
	}
	h := hashutil.Adler32(f)
	f.Close()
	return h[:6], nil
}
Ejemplo n.º 2
0
// Create a local fallback for the given script, downloading it if
// necessary
func scriptFallback(m *Manager, script *Asset, fallback string) (*Asset, error) {
	fallbackName := script.Name
	if !m.Has(fallbackName) {
		var scriptURL string
		if urlutil.IsURL(fallbackName) {
			scriptURL = fallbackName
			fallbackName = "asset.gen." + hashutil.Adler32(fallbackName) + "." + path.Base(fallbackName)
		} else {
			cdn, _, err := Cdn(fallbackName)
			if err != nil {
				return nil, err
			}
			scriptURL = cdn
		}
		if !m.Has(fallbackName) {
			u, err := url.Parse(scriptURL)
			if err != nil {
				return nil, err
			}
			if u.Scheme == "" {
				u.Scheme = "http"
			}
			log.Debugf("fetching local fallback for %s to %s", u, fallbackName)
			resp, err := http.Get(u.String())
			if err != nil {
				return nil, err
			}
			defer resp.Body.Close()
			w, err := m.Create(fallbackName, true)
			if err != nil {
				return nil, err
			}
			defer w.Close()
			if _, err := io.Copy(w, resp.Body); err != nil {
				return nil, err
			}
		}
	}
	return &Asset{
		Name:     fallbackName,
		Position: Bottom,
		Type:     TypeOther,
		HTML:     fmt.Sprintf("<script>%s || document.write('<scr'+'ipt src=\"%s\"><\\/scr'+'ipt>')</script>", fallback, m.URL(fallbackName)),
	}, nil
}