Skip to content

Commit

Permalink
Ensure notes with the same title do not overwrite each other
Browse files Browse the repository at this point in the history
  • Loading branch information
wormi4ok committed Jul 5, 2020
1 parent 811dba8 commit 5fe0673
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
15 changes: 7 additions & 8 deletions file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package file
import (
"io"
"os"
"path/filepath"
"regexp"
"strings"
)
Expand All @@ -17,21 +18,19 @@ var (
dashes = regexp.MustCompile(`[\-_]{2,}`)
)

// Save a new file in a given dir with the following content
// If directory doesn't exist it will create it
// Save a new file in a given dir with the following content.
// Creates a directory if necessary.
func Save(dir, name string, content io.Reader) error {
if len(name) == 0 {
return nil
}

if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.Mkdir(dir, os.ModePerm)
if err != nil {
return err
}
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}

output, err := os.Create(dir + "/" + name)
output, err := os.Create(filepath.FromSlash(dir + "/" + name))
if err != nil {
return err
}
Expand Down
28 changes: 20 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -49,6 +48,11 @@ func main() {
run(input, outputDir)
}

const progressBarTmpl = `Notes: {{counters .}} {{bar . "[" "=" ">" "_" "]" }} {{percent .}} {{etime .}}`

// A map to keep track of what notes are already created
var notes = map[string]int{}

func run(input, output string) {
i, err := os.Open(input)
failWhen(err)
Expand All @@ -63,29 +67,37 @@ func run(input, output string) {
failWhen(err)

progress := pb.StartNew(len(export.Notes))
progress.SetTemplateString(`Notes: {{counters .}} {{bar . "[" "=" ">" "_" "]" }} {{percent .}} {{etime .}}`)
progress.SetTemplateString(progressBarTmpl)

n := export.Notes
for i := range n {
md, err := internal.Convert(&n[i])
failWhen(err)
mdFile := filepath.FromSlash(output + "/" + file.BaseName(n[i].Title) + ".md")
f, err := os.Create(mdFile)
failWhen(err)
_, err = io.Copy(f, bytes.NewReader(md.Content))
err = file.Save(output, uniqueName(n[i].Title), bytes.NewReader(md.Content))
failWhen(err)
for _, res := range md.Media {
err = file.Save(output+"/"+string(res.Type), res.Name, bytes.NewReader(res.Content))
failWhen(err)
}
err = f.Close()
failWhen(err)
progress.Increment()
}
progress.Finish()
fmt.Println("Done!")
}

// uniqueName returns a unique note name
func uniqueName(title string) string {
name := file.BaseName(title) + ".md"
if k, exist := notes[name]; exist {
notes[name] = k + 1
name = fmt.Sprintf("%s-%d.md", file.BaseName(title), k)
} else {
notes[name] = 1
}

return name
}

func failWhen(err error) {
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 5fe0673

Please sign in to comment.