diff options
author | satotake <[email protected]> | 2020-08-04 02:06:18 +0900 |
---|---|---|
committer | GitHub <[email protected]> | 2020-08-03 19:06:18 +0200 |
commit | 12f6a1cdc0aedf4319367af57bda3c94150d6a84 (patch) | |
tree | 1c04900acb91150c8d05c685648ef94937dac3df /deploy/deploy.go | |
parent | 2fa851e6500752c0cea1da5cfdfc6d99e0a81a71 (diff) | |
download | hugo-12f6a1cdc0aedf4319367af57bda3c94150d6a84.tar.gz hugo-12f6a1cdc0aedf4319367af57bda3c94150d6a84.zip |
Respect mediatypes for deploy
Fixes #6861
Diffstat (limited to 'deploy/deploy.go')
-rw-r--r-- | deploy/deploy.go | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/deploy/deploy.go b/deploy/deploy.go index 9a38072a7..f6b5b5785 100644 --- a/deploy/deploy.go +++ b/deploy/deploy.go @@ -33,6 +33,7 @@ import ( "github.com/dustin/go-humanize" "github.com/gobwas/glob" "github.com/gohugoio/hugo/config" + "github.com/gohugoio/hugo/media" "github.com/pkg/errors" "github.com/spf13/afero" jww "github.com/spf13/jwalterweatherman" @@ -51,6 +52,7 @@ type Deployer struct { target *target // the target to deploy to matchers []*matcher // matchers to apply to uploaded files + mediaTypes media.Types // Hugo's MediaType to guess ContentType ordering []*regexp.Regexp // orders uploads quiet bool // true reduces STDOUT confirm bool // true enables confirmation before making changes @@ -96,11 +98,13 @@ func New(cfg config.Provider, localFs afero.Fs) (*Deployer, error) { return nil, fmt.Errorf("deployment target %q not found", targetName) } } + return &Deployer{ localFs: localFs, target: tgt, matchers: dcfg.Matchers, ordering: dcfg.ordering, + mediaTypes: dcfg.mediaTypes, quiet: cfg.GetBool("quiet"), confirm: cfg.GetBool("confirm"), dryRun: cfg.GetBool("dryRun"), @@ -130,7 +134,7 @@ func (d *Deployer) Deploy(ctx context.Context) error { if d.target != nil { include, exclude = d.target.includeGlob, d.target.excludeGlob } - local, err := walkLocal(d.localFs, d.matchers, include, exclude) + local, err := walkLocal(d.localFs, d.matchers, include, exclude, d.mediaTypes) if err != nil { return err } @@ -322,14 +326,15 @@ type localFile struct { // gzipped before upload. UploadSize int64 - fs afero.Fs - matcher *matcher - md5 []byte // cache - gzipped bytes.Buffer // cached of gzipped contents if gzipping + fs afero.Fs + matcher *matcher + md5 []byte // cache + gzipped bytes.Buffer // cached of gzipped contents if gzipping + mediaTypes media.Types } // newLocalFile initializes a *localFile. -func newLocalFile(fs afero.Fs, nativePath, slashpath string, m *matcher) (*localFile, error) { +func newLocalFile(fs afero.Fs, nativePath, slashpath string, m *matcher, mt media.Types) (*localFile, error) { f, err := fs.Open(nativePath) if err != nil { return nil, err @@ -340,6 +345,7 @@ func newLocalFile(fs afero.Fs, nativePath, slashpath string, m *matcher) (*local SlashPath: slashpath, fs: fs, matcher: m, + mediaTypes: mt, } if m != nil && m.Gzip { // We're going to gzip the content. Do it once now, and cache the result @@ -410,10 +416,13 @@ func (lf *localFile) ContentType() string { if lf.matcher != nil && lf.matcher.ContentType != "" { return lf.matcher.ContentType } - // TODO: Hugo has a MediaType and a MediaTypes list and also a concept - // of custom MIME types. - // Use 1) The matcher 2) Hugo's MIME types 3) TypeByExtension. - return mime.TypeByExtension(filepath.Ext(lf.NativePath)) + + ext := filepath.Ext(lf.NativePath) + if mimeType, found := lf.mediaTypes.GetFirstBySuffix(strings.TrimPrefix(ext, ".")); found { + return mimeType.Type() + } + + return mime.TypeByExtension(ext) } // Force returns true if the file should be forced to re-upload based on the @@ -457,7 +466,7 @@ func knownHiddenDirectory(name string) bool { // walkLocal walks the source directory and returns a flat list of files, // using localFile.SlashPath as the map keys. -func walkLocal(fs afero.Fs, matchers []*matcher, include, exclude glob.Glob) (map[string]*localFile, error) { +func walkLocal(fs afero.Fs, matchers []*matcher, include, exclude glob.Glob, mediaTypes media.Types) (map[string]*localFile, error) { retval := map[string]*localFile{} err := afero.Walk(fs, "", func(path string, info os.FileInfo, err error) error { if err != nil { @@ -503,7 +512,7 @@ func walkLocal(fs afero.Fs, matchers []*matcher, include, exclude glob.Glob) (ma break } } - lf, err := newLocalFile(fs, path, slashpath, m) + lf, err := newLocalFile(fs, path, slashpath, m, mediaTypes) if err != nil { return err } |