func (md *MdFiles) findNewFiles(newFound chan bool) error {
	u.Logln(u.Debug, "Looking for new files")
	defer md.regenerateLinks()
	paths, err := filepath.Glob(md.Path + "/*")
	if err != nil {
		return err
	}
	for _, p := range paths {
		f, err := os.Open(p)
		defer f.Close()
		if err != nil {
			u.Logln(u.Error, "Failed to open the file - ", err)
			continue
		}
		info, err := f.Stat()
		if err != nil {
			u.Logln(u.Error, "Failed to stat the file - ", err)
			continue
		}
		if !info.IsDir() {
			if !md.isFileInFiles(p) {
				u.Logf(u.Info, "New file found - '%v'", p)
				file, err := NewFile(p)
				if err != nil {
					u.Logln(u.Error, "Failed to load file - ", err)
					continue
				}
				md.Files = append(md.Files, file)
				newFound <- true
			}
		}
	}
	return nil
}
findNewFiles checks for new files in md.Path.
If it finds a new file a reload message is sent to the hub.
package mdserver
import (
	. "gomd/mdserver/file"
	u "gomd/util"
	"os"
	"path/filepath"
	"time"
)
type MdFiles struct {
	Path       string
	Files      []File
	Links      map[string]string
	ShowHidden bool
}
func NewMdFiles(path string, showHidden bool) MdFiles {
	f := loadMdFiles(path)
	return MdFiles{
		Path:       path,
		Files:      f,
		Links:      linksFromFiles(f, showHidden),
		ShowHidden: showHidden,
	}
}
func (md *MdFiles) Watch(changed chan bool, newFound chan bool) {
	for {
		md.checkIfFilesChanged(changed)
		md.findNewFiles(newFound)
		time.Sleep(sleepDuration * time.Millisecond)
	}
}
func linksFromFiles(files []File, showHidden bool) map[string]string {
	links := make(map[string]string)
	for _, f := range files {
		if f.IsHidden() && !showHidden {
			continue
		}
		links[f.Filename] = fileviewEp + f.Path
	}
	return links
}
func (md *MdFiles) regenerateLinks() {
	md.Links = linksFromFiles(md.Files, md.ShowHidden)
}
func (md *MdFiles) isFileInFiles(path string) bool {
	u.Logf(u.Debug, "Checking if `%v` is in files", path)
	for _, f := range md.Files {
		if f.Path == path {
			return true
		}
	}
	return false
}
func loadMdFiles(path string) []File {
	u.Logf(u.Debug, "Loading mdfiles from '%v'", path)
	files := []File{}
	paths, err := filepath.Glob(path + "/*")
	if err != nil {
		return files
	}
	for _, p := range paths {
		f, err := os.Open(p)
		if err != nil {
			continue
		}
		info, err := f.Stat()
		if err != nil {
			continue
		}
		if !info.IsDir() {
			file, err := NewFile(p)
			if err != nil {
				u.Logln(u.Error, "Failed to load file - ", err)
				continue
			}
			files = append(files, file)
		}
	}
	return files
}
func (md *MdFiles) checkIfFilesChanged(changed chan bool) {
	u.Logln(u.Debug, "Checking if mdfiles changed")
	defer md.regenerateLinks()
	for i := 0; i < len(md.Files); i++ {
		f := &md.Files[i]
		if hasChanged, _ := f.HasModTimeChanged(); hasChanged {
			u.Logf(u.Info, "File %v changed", f.Filename)
			err := f.Reload()
			if err != nil {
				u.Logln(u.Warn, "Failed to reload file - ", err)
			}
			changed <- true
		}
	}
}