Example #1
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
}
Example #2
0
func (m *Manager) URL(name string) string {
	if urlutil.IsURL(name) {
		return name
	}
	m.mutex.RLock()
	h, ok := m.cache[name]
	m.mutex.RUnlock()
	if !ok {
		h, _ = m.hash(name)
		m.mutex.Lock()
		m.cache[name] = h
		m.mutex.Unlock()
	}
	clean := path.Clean(path.Join(m.prefix, name))
	if h != "" {
		return clean + "?v=" + h
	}
	return clean
}