1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 05:20:26 +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() {
} }
} }
// Index finds all hypha files in the full `path` and saves them to HyphaStorage. This function is recursive. func HyphaToTemporaryWorkaround(h *hyphae.Hypha) *HyphaData {
func Index(path string) { return &HyphaData{
nodes, err := ioutil.ReadDir(path) Name: h.Name,
if err != nil { TextPath: h.TextPath,
log.Fatal(err) BinaryPath: h.BinaryPath,
}
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() && isCanonicalName(node.Name()) && node.Name() != ".git" && node.Name() != "static" {
Index(filepath.Join(path, node.Name()))
continue
}
var (
hyphaPartPath = filepath.Join(path, node.Name())
hyphaName, isText, skip = mimetype.DataFromFilename(hyphaPartPath)
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 {
hyphaData = &HyphaData{}
HyphaStorage[hyphaName] = hyphaData
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
}
}
} }
} }
// 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.
func Index(path string) {
ch := make(chan *hyphae.Hypha, 5)
go func() {
hyphae.Index(path, 0, ch)
close(ch)
}()
for h := range ch {
if oldHypha, ok := HyphaStorage[h.Name]; ok {
oldHypha.MergeIn(h)
} else {
HyphaStorage[h.Name] = HyphaToTemporaryWorkaround(h)
hyphae.IncrementCount()
}
}
}

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
}
}
}