1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-13 05:50:27 +00:00

Partially move main.Index to hyphae.Index

This commit is contained in:
bouncepaw 2021-02-08 19:59:00 +05:00
parent 70e427bc23
commit 5d8ee3a997
2 changed files with 82 additions and 39 deletions

View File

@ -284,44 +284,43 @@ func setHeaderLinks() {
} }
} }
func HyphaToTemporaryWorkaround(h *hyphae.Hypha) *HyphaData {
return &HyphaData{
Name: h.Name,
TextPath: h.TextPath,
BinaryPath: h.BinaryPath,
}
}
// MergeIn merges in content file paths from a different hypha object. Prints warnings sometimes.
func (h *HyphaData) MergeIn(oh *hyphae.Hypha) {
if h.TextPath == "" && oh.TextPath != "" {
h.TextPath = oh.TextPath
}
if oh.BinaryPath != "" {
if h.BinaryPath != "" {
log.Println("There is a file collision for binary part of a hypha:", h.BinaryPath, "and", oh.BinaryPath, "-- going on with the latter")
}
h.BinaryPath = oh.BinaryPath
}
}
// Index finds all hypha files in the full `path` and saves them to HyphaStorage. This function is recursive. // Index finds all hypha files in the full `path` and saves them to HyphaStorage. This function is recursive.
func Index(path string) { func Index(path string) {
nodes, err := ioutil.ReadDir(path) ch := make(chan *hyphae.Hypha, 5)
if err != nil {
log.Fatal(err)
}
for _, node := range nodes { go func() {
// If this hypha looks like it can be a hypha path, go deeper. Do not touch the .git and static folders for they have an admnistrative importance! hyphae.Index(path, 0, ch)
if node.IsDir() && isCanonicalName(node.Name()) && node.Name() != ".git" && node.Name() != "static" { close(ch)
Index(filepath.Join(path, node.Name())) }()
continue
}
var ( for h := range ch {
hyphaPartPath = filepath.Join(path, node.Name()) if oldHypha, ok := HyphaStorage[h.Name]; ok {
hyphaName, isText, skip = mimetype.DataFromFilename(hyphaPartPath) oldHypha.MergeIn(h)
hyphaData *HyphaData
)
if !skip {
// Reuse the entry for existing hyphae, create a new one for those that do not exist yet.
if hd, ok := HyphaStorage[hyphaName]; ok {
hyphaData = hd
} else { } else {
hyphaData = &HyphaData{} HyphaStorage[h.Name] = HyphaToTemporaryWorkaround(h)
HyphaStorage[hyphaName] = hyphaData
hyphae.IncrementCount() hyphae.IncrementCount()
} }
if isText {
hyphaData.TextPath = hyphaPartPath
} else {
// Notify the user about binary part collisions. It's a design decision to just use any of them, it's the user's fault that they have screwed up the folder structure, but the engine should at least let them know, right?
if hyphaData.BinaryPath != "" {
log.Println("There is a file collision for binary part of a hypha:", hyphaData.BinaryPath, "and", hyphaPartPath, "-- going on with the latter")
}
hyphaData.BinaryPath = hyphaPartPath
}
} }
} }
}

44
hyphae/files.go Normal file
View File

@ -0,0 +1,44 @@
package hyphae
import (
"io/ioutil"
"log"
"path/filepath"
"github.com/bouncepaw/mycorrhiza/mimetype"
"github.com/bouncepaw/mycorrhiza/util"
)
// Index finds all hypha files in the full `path` and sends them to the channel. Handling of duplicate entries and attachment and counting them is up to the caller.
func Index(path string, nestLevel uint, ch chan *Hypha) {
nodes, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
for _, node := range nodes {
// If this hypha looks like it can be a hypha path, go deeper. Do not touch the .git and static folders for they have an admnistrative importance!
if node.IsDir() &&
util.IsCanonicalName(node.Name()) &&
node.Name() != ".git" &&
!(nestLevel == 0 && node.Name() == "static") {
Index(filepath.Join(path, node.Name()), nestLevel+1, ch)
continue
}
var (
hyphaPartPath = filepath.Join(path, node.Name())
hyphaName, isText, skip = mimetype.DataFromFilename(hyphaPartPath)
hypha = &Hypha{Name: hyphaName}
)
if !skip {
if isText {
hypha.TextPath = hyphaPartPath
} else {
hypha.BinaryPath = hyphaPartPath
}
ch <- hypha
}
}
}