mycorrhiza/internal/hyphae/files.go
Timur Ismagilov 41733c50bd
New templates #117 (#236)
Didn't have the chance to migrate //all// templates just yet. We'll get there.

* Implement yet another template system

* Move orphans to the new system and fix a bug in it

* Link orphans in the admin panel

* Move the backlink handlers to the web package

* Move auth routing to web

* Move /user-list to the new system

* Move change password and translate it

* Move stuff

* Move admin-related stuff to the web

* Move a lot of files into internal dir

Outside of it are web and stuff that needs further refactoring

* Fix static not loading and de-qtpl tree

* Move tree to internal

* Keep the globe on the same line #230

* Revert "Keep the globe on the same line #230"

This reverts commit ae78e5e459.

* Migrate templates from hypview: delete, edit, start empty and existing WIP

The delete media view was removed, I didn't even know it still existed as a GET. A rudiment.

* Make views multi-file and break compilation

* Megarefactoring of hypha views

* Auth-related stuffs

* Fix some of those weird imports

* Migrate cat views

* Fix cat js

* Lower standards

* Internalize trauma
2024-09-07 21:22:41 +03:00

94 lines
2.5 KiB
Go

package hyphae
import (
"github.com/bouncepaw/mycorrhiza/internal/mimetype"
"log"
"log/slog"
"os"
"path/filepath"
)
// Index finds all hypha files in the full `path` and saves them to the hypha storage.
func Index(path string) {
byNames = make(map[string]ExistingHypha)
ch := make(chan ExistingHypha, 5)
go func(ch chan ExistingHypha) {
indexHelper(path, 0, ch)
close(ch)
}(ch)
for foundHypha := range ch {
switch storedHypha := ByName(foundHypha.CanonicalName()).(type) {
case *EmptyHypha:
Insert(foundHypha)
case *TextualHypha:
switch foundHypha := foundHypha.(type) {
case *TextualHypha: // conflict! overwrite
storedHypha.mycoFilePath = foundHypha.mycoFilePath
slog.Info("File collision",
"hypha", foundHypha.CanonicalName(),
"usingFile", foundHypha.TextFilePath(),
"insteadOf", storedHypha.TextFilePath(),
)
case *MediaHypha: // no conflict
Insert(ExtendTextualToMedia(storedHypha, foundHypha.mediaFilePath))
}
case *MediaHypha:
switch foundHypha := foundHypha.(type) {
case *TextualHypha: // no conflict
storedHypha.mycoFilePath = foundHypha.mycoFilePath
case *MediaHypha: // conflict! overwrite
storedHypha.mediaFilePath = foundHypha.mediaFilePath
slog.Info("File collision",
"hypha", foundHypha.CanonicalName(),
"usingFile", foundHypha.MediaFilePath(),
"insteadOf", storedHypha.MediaFilePath(),
)
}
}
}
log.Println("Indexed", Count(), "hyphae")
}
// indexHelper finds all hypha files in the full `path` and sends them to the
// channel. Handling of duplicate entries and media and counting them is
// up to the caller.
func indexHelper(path string, nestLevel uint, ch chan ExistingHypha) {
nodes, err := os.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 folders for it has an administrative importance!
if node.IsDir() && IsValidName(node.Name()) && node.Name() != ".git" {
indexHelper(filepath.Join(path, node.Name()), nestLevel+1, ch)
continue
}
var (
hyphaPartPath = filepath.ToSlash(filepath.Join(path, node.Name()))
hyphaName, isText, skip = mimetype.DataFromFilename(hyphaPartPath)
)
if !skip {
if isText {
ch <- &TextualHypha{
canonicalName: hyphaName,
mycoFilePath: hyphaPartPath,
}
} else {
ch <- &MediaHypha{
canonicalName: hyphaName,
mycoFilePath: "",
mediaFilePath: hyphaPartPath,
}
}
}
}
}