Allow multiple directories, Rename module

This commit is contained in:
MrLetsplay 2023-09-24 22:13:16 +02:00
parent e4774d0d6a
commit 41504f2ea1
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
3 changed files with 45 additions and 43 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
pdf PDFIndexer
index.json index.json

2
go.mod
View File

@ -1,4 +1,4 @@
module cringe-studios.com/pdf module git.cringe-studios.com/CringeStudios/PDFIndexer
go 1.20 go 1.20

84
main.go
View File

@ -13,55 +13,57 @@ import (
"code.sajari.com/docconv" "code.sajari.com/docconv"
) )
var fileFormats = []string{".pdf", ".jpg", ".png", ".txt", ".docx", ".doc", ".tif"} var fileFormats = []string{".pdf", ".jpg", ".png", ".txt", ".docx", ".doc", ".tif", "tiff"}
func buildIndex(dir string, old map[string]string) (map[string]string, error) { func buildIndex(dirs []string, old map[string]string) (map[string]string, error) {
var index = old var index = old
if index == nil { if index == nil {
index = make(map[string]string) index = make(map[string]string)
} }
err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error { for _, dir := range dirs {
if err != nil { err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
return err if err != nil {
} return err
if info.IsDir() {
return nil
}
supportedType := false
for _, suffix := range fileFormats {
if strings.HasSuffix(strings.ToLower(path), suffix) {
supportedType = true
break
} }
}
if !supportedType { if info.IsDir() {
return nil
}
supportedType := false
for _, suffix := range fileFormats {
if strings.HasSuffix(strings.ToLower(path), suffix) {
supportedType = true
break
}
}
if !supportedType {
return nil
}
_, exists := index[path]
if exists {
return nil
}
log.Println(path)
str, err := docconv.ConvertPath(path)
if err != nil {
log.Println("Fail:", err)
return nil
}
index[path] = str.Body
return nil return nil
} })
_, exists := index[path]
if exists {
return nil
}
log.Println(path)
str, err := docconv.ConvertPath(path)
if err != nil { if err != nil {
log.Println("Fail:", err) return nil, err
return nil
} }
index[path] = str.Body
return nil
})
if err != nil {
return nil, err
} }
return index, nil return index, nil
@ -72,13 +74,13 @@ func main() {
flag.Parse() flag.Parse()
if len(flag.Args()) != 2 { if len(flag.Args()) < 2 {
fmt.Println("Usage:", os.Args[0], "<directory> <destination file>") fmt.Println("Usage:", os.Args[0], "<destination file> <directory> [directories...]")
os.Exit(1) os.Exit(1)
} }
directory := flag.Arg(0) indexFile := flag.Arg(0)
indexFile := flag.Arg(1) directories := flag.Args()[1:]
var oldIndex = make(map[string]string) var oldIndex = make(map[string]string)
if !*force { if !*force {
@ -97,7 +99,7 @@ func main() {
} }
} }
index, err := buildIndex(directory, oldIndex) index, err := buildIndex(directories, oldIndex)
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
} }