2014-10-16 20:20:09 -04:00
|
|
|
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Licensed under the Simple Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://opensource.org/licenses/Simple-2.0
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
2013-12-15 09:49:23 -05:00
|
|
|
"bytes"
|
2013-09-05 01:28:59 -04:00
|
|
|
"io"
|
2013-12-15 09:49:23 -05:00
|
|
|
"io/ioutil"
|
2013-09-05 01:28:59 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2013-11-28 09:27:09 -05:00
|
|
|
"strings"
|
2015-02-17 16:21:37 -05:00
|
|
|
|
|
|
|
"github.com/spf13/hugo/helpers"
|
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2013-09-05 01:28:59 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Input interface {
|
|
|
|
Files() []*File
|
|
|
|
}
|
|
|
|
|
|
|
|
type Filesystem struct {
|
|
|
|
files []*File
|
|
|
|
Base string
|
|
|
|
AvoidPaths []string
|
|
|
|
}
|
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
func (f *Filesystem) FilesByExts(exts ...string) []*File {
|
|
|
|
var newFiles []*File
|
|
|
|
|
|
|
|
if len(exts) == 0 {
|
|
|
|
return f.Files()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, x := range f.Files() {
|
|
|
|
for _, e := range exts {
|
|
|
|
if x.Ext() == strings.TrimPrefix(e, ".") {
|
|
|
|
newFiles = append(newFiles, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newFiles
|
|
|
|
}
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
func (f *Filesystem) Files() []*File {
|
2013-10-01 17:27:09 -04:00
|
|
|
if len(f.files) < 1 {
|
|
|
|
f.captureFiles()
|
|
|
|
}
|
2013-09-05 01:28:59 -04:00
|
|
|
return f.files
|
|
|
|
}
|
|
|
|
|
2013-09-20 20:03:43 -04:00
|
|
|
func (f *Filesystem) add(name string, reader io.Reader) (err error) {
|
2014-10-16 20:20:09 -04:00
|
|
|
var file *File
|
2013-09-20 20:03:43 -04:00
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
//if f.Base == "" {
|
|
|
|
//file = NewFileWithContents(name, reader)
|
|
|
|
//} else {
|
|
|
|
file, err = NewFileFromAbs(f.Base, name, reader)
|
|
|
|
//}
|
2013-11-28 09:27:09 -05:00
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
if err == nil {
|
|
|
|
f.files = append(f.files, file)
|
2013-09-20 20:03:43 -04:00
|
|
|
}
|
2014-10-16 20:20:09 -04:00
|
|
|
return err
|
2013-09-20 20:03:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filesystem) getRelativePath(name string) (final string, err error) {
|
2014-10-16 20:20:09 -04:00
|
|
|
return helpers.GetRelativePath(name, f.Base)
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filesystem) captureFiles() {
|
|
|
|
|
2013-09-20 20:03:43 -04:00
|
|
|
walker := func(filePath string, fi os.FileInfo, err error) error {
|
2013-09-05 01:28:59 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-10 10:48:51 -05:00
|
|
|
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
|
2015-02-17 16:21:37 -05:00
|
|
|
link, err := filepath.EvalSymlinks(filePath)
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", filePath, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
linkfi, err := os.Stat(link)
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !linkfi.Mode().IsRegular() {
|
|
|
|
jww.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", filePath)
|
|
|
|
}
|
2014-12-10 10:48:51 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
if fi.IsDir() {
|
2014-04-14 17:25:54 -04:00
|
|
|
if f.avoid(filePath) || isNonProcessablePath(filePath) {
|
2013-09-05 01:28:59 -04:00
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} else {
|
2014-04-14 17:25:54 -04:00
|
|
|
if isNonProcessablePath(filePath) {
|
2013-09-05 01:28:59 -04:00
|
|
|
return nil
|
|
|
|
}
|
2013-12-15 09:49:23 -05:00
|
|
|
data, err := ioutil.ReadFile(filePath)
|
2013-09-05 01:28:59 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-12-15 09:49:23 -05:00
|
|
|
f.add(filePath, bytes.NewBuffer(data))
|
2013-09-05 01:28:59 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filepath.Walk(f.Base, walker)
|
|
|
|
}
|
|
|
|
|
2013-09-20 20:03:43 -04:00
|
|
|
func (f *Filesystem) avoid(filePath string) bool {
|
2013-09-05 01:28:59 -04:00
|
|
|
for _, avoid := range f.AvoidPaths {
|
2013-09-20 20:03:43 -04:00
|
|
|
if avoid == filePath {
|
2013-09-05 01:28:59 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-04-14 17:25:54 -04:00
|
|
|
func isNonProcessablePath(filePath string) bool {
|
2014-02-28 00:26:17 -05:00
|
|
|
base := filepath.Base(filePath)
|
|
|
|
if base[0] == '.' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-03-03 16:55:28 -05:00
|
|
|
if base[0] == '#' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-02-28 00:26:17 -05:00
|
|
|
if base[len(base)-1] == '~' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|