2016-03-21 23:28:42 +00:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2014-12-27 03:40:10 +00:00
|
|
|
//
|
2015-11-24 03:16:36 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-12-27 03:40:10 +00:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-24 03:16:36 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-12-27 03:40:10 +00:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-03-13 22:55:02 +00:00
|
|
|
package data
|
2014-12-27 03:40:10 +00:00
|
|
|
|
|
|
|
import (
|
2017-05-02 03:41:08 +00:00
|
|
|
"fmt"
|
2014-12-27 03:40:10 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-06-26 10:23:37 +00:00
|
|
|
"path/filepath"
|
2014-12-27 03:40:10 +00:00
|
|
|
"sync"
|
2015-05-28 05:36:06 +00:00
|
|
|
"time"
|
2014-12-27 03:40:10 +00:00
|
|
|
|
2017-06-13 16:42:45 +00:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2017-06-13 17:07:35 +00:00
|
|
|
"github.com/spf13/afero"
|
2014-12-27 03:40:10 +00:00
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
|
|
|
)
|
|
|
|
|
2015-05-28 05:36:06 +00:00
|
|
|
var (
|
|
|
|
remoteURLLock = &remoteLock{m: make(map[string]*sync.Mutex)}
|
|
|
|
resSleep = time.Second * 2 // if JSON decoding failed sleep for n seconds before retrying
|
|
|
|
resRetries = 1 // number of retries to load the JSON from URL or local file system
|
|
|
|
)
|
2014-12-27 03:40:10 +00:00
|
|
|
|
|
|
|
type remoteLock struct {
|
|
|
|
sync.RWMutex
|
|
|
|
m map[string]*sync.Mutex
|
|
|
|
}
|
|
|
|
|
2015-03-18 05:16:54 +00:00
|
|
|
// URLLock locks an URL during download
|
2015-03-11 17:34:57 +00:00
|
|
|
func (l *remoteLock) URLLock(url string) {
|
2017-03-31 08:40:33 +00:00
|
|
|
var (
|
|
|
|
lock *sync.Mutex
|
|
|
|
ok bool
|
|
|
|
)
|
2014-12-27 03:40:10 +00:00
|
|
|
l.Lock()
|
2017-03-31 08:40:33 +00:00
|
|
|
if lock, ok = l.m[url]; !ok {
|
|
|
|
lock = &sync.Mutex{}
|
|
|
|
l.m[url] = lock
|
2014-12-27 03:40:10 +00:00
|
|
|
}
|
2017-02-17 13:24:56 +00:00
|
|
|
l.Unlock()
|
2017-03-31 08:40:33 +00:00
|
|
|
lock.Lock()
|
2014-12-27 03:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 05:16:54 +00:00
|
|
|
// URLUnlock unlocks an URL when the download has been finished. Use only in defer calls.
|
2015-03-11 17:34:57 +00:00
|
|
|
func (l *remoteLock) URLUnlock(url string) {
|
2014-12-27 03:40:10 +00:00
|
|
|
l.RLock()
|
|
|
|
defer l.RUnlock()
|
|
|
|
if um, ok := l.m[url]; ok {
|
|
|
|
um.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:04:21 +00:00
|
|
|
// getRemote loads the content of a remote file. This method is thread safe.
|
2017-05-02 03:41:08 +00:00
|
|
|
func getRemote(req *http.Request, fs afero.Fs, cfg config.Provider, hc *http.Client) ([]byte, error) {
|
|
|
|
url := req.URL.String()
|
|
|
|
|
2017-05-01 19:04:21 +00:00
|
|
|
c, err := getCache(url, fs, cfg, cfg.GetBool("ignoreCache"))
|
2014-12-27 03:40:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-02 03:41:08 +00:00
|
|
|
if c != nil {
|
|
|
|
return c, nil
|
|
|
|
}
|
2014-12-27 03:40:10 +00:00
|
|
|
|
|
|
|
// avoid race condition with locks, block other goroutines if the current url is processing
|
2015-03-11 17:34:57 +00:00
|
|
|
remoteURLLock.URLLock(url)
|
|
|
|
defer func() { remoteURLLock.URLUnlock(url) }()
|
2014-12-27 03:40:10 +00:00
|
|
|
|
2017-05-01 19:04:21 +00:00
|
|
|
// avoid multiple locks due to calling getCache twice
|
|
|
|
c, err = getCache(url, fs, cfg, cfg.GetBool("ignoreCache"))
|
2014-12-27 03:40:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-02 03:41:08 +00:00
|
|
|
if c != nil {
|
|
|
|
return c, nil
|
|
|
|
}
|
2014-12-27 03:40:10 +00:00
|
|
|
|
|
|
|
jww.INFO.Printf("Downloading: %s ...", url)
|
2017-05-02 03:41:08 +00:00
|
|
|
res, err := hc.Do(req)
|
2014-12-27 03:40:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-02 03:41:08 +00:00
|
|
|
|
|
|
|
if res.StatusCode < 200 || res.StatusCode > 299 {
|
|
|
|
return nil, fmt.Errorf("Failed to retrieve remote file: %s", http.StatusText(res.StatusCode))
|
|
|
|
}
|
|
|
|
|
2014-12-27 03:40:10 +00:00
|
|
|
c, err = ioutil.ReadAll(res.Body)
|
|
|
|
res.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-02 03:41:08 +00:00
|
|
|
|
2017-05-01 19:04:21 +00:00
|
|
|
err = writeCache(url, c, fs, cfg, cfg.GetBool("ignoreCache"))
|
2014-12-27 03:40:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-02 03:41:08 +00:00
|
|
|
|
2017-02-05 03:20:06 +00:00
|
|
|
jww.INFO.Printf("... and cached to: %s", getCacheFileID(cfg, url))
|
2014-12-27 03:40:10 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:04:21 +00:00
|
|
|
// getLocal loads the content of a local file
|
|
|
|
func getLocal(url string, fs afero.Fs, cfg config.Provider) ([]byte, error) {
|
2017-02-05 03:20:06 +00:00
|
|
|
filename := filepath.Join(cfg.GetString("workingDir"), url)
|
2016-02-07 01:07:58 +00:00
|
|
|
if e, err := helpers.Exists(filename, fs); !e {
|
2014-12-27 03:40:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-07-30 13:37:03 +00:00
|
|
|
return afero.ReadFile(fs, filename)
|
|
|
|
|
2014-12-27 03:40:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 19:04:21 +00:00
|
|
|
// getResource loads the content of a local or remote file
|
2017-05-02 03:41:08 +00:00
|
|
|
func (ns *Namespace) getResource(req *http.Request) ([]byte, error) {
|
|
|
|
switch req.URL.Scheme {
|
|
|
|
case "":
|
|
|
|
return getLocal(req.URL.String(), ns.deps.Fs.Source, ns.deps.Cfg)
|
|
|
|
default:
|
|
|
|
return getRemote(req, ns.deps.Fs.Source, ns.deps.Cfg, ns.client)
|
2014-12-27 03:40:10 +00:00
|
|
|
}
|
|
|
|
}
|