From e6907b9e0693cc79abfcf01081ff6fdce6623c1d Mon Sep 17 00:00:00 2001 From: Arnas Udovic Date: Tue, 27 Jun 2023 00:44:32 +0300 Subject: [PATCH] Ignore not markdown files --- go.mod | 1 + go.sum | 2 + testdata/sunny/subcategory/test1.img | 0 testdata/sunny/test0.img | 0 tree.go | 75 ++++++++++++++++++++++++---- tree_test.go | 15 ++---- 6 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 testdata/sunny/subcategory/test1.img create mode 100644 testdata/sunny/test0.img diff --git a/go.mod b/go.mod index f20ab11..8e0f632 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,5 @@ go 1.18 require ( g.arns.lt/zordsdavini/abcex v1.0.0 github.com/otiai10/copy v1.6.0 + golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 ) diff --git a/go.sum b/go.sum index 22f158e..0df3265 100644 --- a/go.sum +++ b/go.sum @@ -7,3 +7,5 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6 github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/otiai10/mint v1.3.2 h1:VYWnrP5fXmz1MXvjuUvcBrXSjGE6xjON+axB/UrpO3E= github.com/otiai10/mint v1.3.2/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= +golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc= +golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= diff --git a/testdata/sunny/subcategory/test1.img b/testdata/sunny/subcategory/test1.img new file mode 100644 index 0000000..e69de29 diff --git a/testdata/sunny/test0.img b/testdata/sunny/test0.img new file mode 100644 index 0000000..e69de29 diff --git a/tree.go b/tree.go index 85faf32..64d779c 100644 --- a/tree.go +++ b/tree.go @@ -2,10 +2,13 @@ package zord_tree import ( "bufio" + "crypto/md5" "errors" "fmt" "g.arns.lt/zordsdavini/abcex" cp "github.com/otiai10/copy" + "golang.org/x/exp/slices" + "io" "io/fs" "io/ioutil" "os" @@ -29,6 +32,10 @@ type Tree struct { Files []File } +var readableFormats = []string{".md"} + +const attachmentDirName = "__a" + func (t Tree) FileById(id string) (File, error) { for _, f := range t.Files { if f.Id == id { @@ -104,7 +111,13 @@ func BuildTree(dirPath string, meta []string) (Tree, error) { } func PopulateTree(sourcePath string, destPath string, meta []string, customMeta map[string]func() string) error { - err := fixFormat(sourcePath) + + attachmentRegistry, err := moveAttachments(sourcePath) + if err != nil { + return err + } + + err = fixFormat(sourcePath, attachmentRegistry) if err != nil { return err } @@ -131,14 +144,51 @@ func PopulateTree(sourcePath string, destPath string, meta []string, customMeta return err } -func fixFormat(dir string) error { - err := filepath.Walk(dir, func(path string, info os.FileInfo, e error) error { +func moveAttachments(dir string) (map[string]string, error) { + attachmentRegistry := make(map[string]string) + + err := filepath.Walk(dir, func(fullPath string, info os.FileInfo, e error) error { if e != nil { return e } - if info.Mode().IsRegular() { - osf, err := os.Open(path) + var clearDir = strings.TrimPrefix(dir, "./") + if info.Mode().IsRegular() && !slices.Contains(readableFormats, path.Ext(fullPath)) && !strings.HasPrefix(fullPath, fmt.Sprintf("%s/%s", clearDir, attachmentDirName)) { + f, err := os.Open(fullPath) + if err != nil { + return err + } + defer f.Close() + + h := md5.New() + if _, err := io.Copy(h, f); err != nil { + return err + } + + _ = os.Mkdir(fmt.Sprintf("%s/%s", dir, attachmentDirName), 0755) + + attachmentRegistry[fullPath] = fmt.Sprintf("%s/%x%s", attachmentDirName, h.Sum(nil), path.Ext(fullPath)) + newPath := fmt.Sprintf("%s/%s/%x%s", dir, attachmentDirName, h.Sum(nil), path.Ext(fullPath)) + err = os.Rename(fullPath, newPath) + if err != nil { + return err + } + } + + return nil + }) + + return attachmentRegistry, err +} + +func fixFormat(dir string, attachmentRegistry map[string]string) error { + err := filepath.Walk(dir, func(fullPath string, info os.FileInfo, e error) error { + if e != nil { + return e + } + + if info.Mode().IsRegular() && slices.Contains(readableFormats, path.Ext(fullPath)) { + osf, err := os.Open(fullPath) if err != nil { return err } @@ -163,17 +213,17 @@ func fixFormat(dir string) error { content = content + "\n" + line } } - err = ioutil.WriteFile(path, []byte(content), 0644) + err = ioutil.WriteFile(fullPath, []byte(content), 0644) // format split line - b, err := ioutil.ReadFile(path) // just pass the file name + b, err := ioutil.ReadFile(fullPath) // just pass the file name if err != nil { return err } str := string(b) str = strings.Replace(str, "\n---\n", "\n\n---\n", 1) - err = ioutil.WriteFile(path, []byte(str), 0644) + err = ioutil.WriteFile(fullPath, []byte(str), 0644) } return nil }) @@ -320,9 +370,10 @@ func getMaxId(dir string) (int64, error) { } func readPath(dirPath string, category []string, meta []string) (Tree, error) { - tree := Tree{} + var clearDir = strings.TrimPrefix(dirPath, "./") - tree.Path = strings.TrimPrefix(dirPath, "./") + tree := Tree{} + tree.Path = clearDir files, err := ioutil.ReadDir(dirPath) if err != nil { @@ -332,6 +383,10 @@ func readPath(dirPath string, category []string, meta []string) (Tree, error) { for _, file := range files { fullPath := path.Join(dirPath, file.Name()) if file.IsDir() { + if strings.HasPrefix(fullPath, fmt.Sprintf("%s/%s", clearDir, attachmentDirName)) { + continue + } + nextDir, err := readPath(fullPath, append(category, file.Name()), meta) if err != nil { return tree, err diff --git a/tree_test.go b/tree_test.go index 02af4a2..9c5d62f 100644 --- a/tree_test.go +++ b/tree_test.go @@ -19,6 +19,11 @@ func prepare(t *testing.T) { if err != nil { t.Fatal("Couldn't prepare data for testing") } + err = PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{}, nil) + if err != nil { + t.Fatal("Population ended in err:", err) + } + } func TestFromNotExistingDirectory(t *testing.T) { @@ -194,11 +199,6 @@ func TestReadingFileContent(t *testing.T) { func TestGetSlice(t *testing.T) { prepare(t) - err := PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{}, nil) - if err != nil { - t.Fatal("Population ended in err:", err) - } - tree, err := BuildTree("./testdata/sunny2", []string{}) if err != nil { t.Errorf("Got error: %v", err) @@ -216,11 +216,6 @@ func TestGetSlice(t *testing.T) { func TestGetFileById(t *testing.T) { prepare(t) - err := PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{}, nil) - if err != nil { - t.Fatal("Population ended in err:", err) - } - tree, err := BuildTree("./testdata/sunny2", []string{}) if err != nil { t.Errorf("Got error: %v", err)