feat: handle multiple directories

This commit is contained in:
Jan Bader 2025-01-02 23:31:49 +01:00
parent 93f3ccd59d
commit 7d4cd72bb7

202
main.go
View File

@ -74,125 +74,127 @@ func main() {
fmt.Println() fmt.Println()
fmt.Println() fmt.Println()
fmt.Printf("Scanning folder '%s'\n", dsConsumedir) for _, dsConsumedir := range cfg.ImportDirectories {
fmt.Println() fmt.Printf("Scanning folder '%s'\n", dsConsumedir)
fmt.Println() fmt.Println()
fmt.Println()
err = filepath.Walk(dsConsumedir, func(path string, info os.FileInfo, err error) error { err = filepath.Walk(dsConsumedir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
fmt.Printf("Checking '%s'\n", path)
// Check if file exists in Docspell
cmd := exec.Command("dsc", "-f", "json", "file-exists", path)
output, err := cmd.Output()
if err != nil {
fmt.Printf("ERROR %v\n", err)
return nil
}
var fileExistsResponse []map[string]interface{}
if err := json.Unmarshal(output, &fileExistsResponse); err != nil {
fmt.Printf("ERROR parsing response: %v\n", err)
return nil
}
if fileExistsResponse[0]["exists"].(bool) {
// File exists in Docspell
items := fileExistsResponse[0]["items"].([]interface{})
item := items[0].(map[string]interface{})
itemID := item["id"].(string)
itemName := item["name"].(string)
// Get item details
cmd = exec.Command("dsc", "-f", "json", "item", "get", itemID)
output, err = cmd.Output()
if err != nil { if err != nil {
fmt.Printf("ERROR getting item details: %v\n", err) return err
}
if info.IsDir() {
return nil return nil
} }
var itemDetails map[string]interface{} fmt.Printf("Checking '%s'\n", path)
if err := json.Unmarshal(output, &itemDetails); err != nil {
fmt.Printf("ERROR parsing item details: %v\n", err) // Check if file exists in Docspell
cmd := exec.Command("dsc", "-f", "json", "file-exists", path)
output, err := cmd.Output()
if err != nil {
fmt.Printf("ERROR %v\n", err)
return nil return nil
} }
folder := itemDetails["folder"].(map[string]interface{})["name"].(string) var fileExistsResponse []map[string]interface{}
extension := filepath.Ext(path)[1:] if err := json.Unmarshal(output, &fileExistsResponse); err != nil {
fmt.Printf("ERROR parsing response: %v\n", err)
var corr string return nil
if corrOrg, ok := itemDetails["corr_org"].(map[string]interface{}); ok && corrOrg["name"] != nil {
corr = corrOrg["name"].(string)
} else if corrPerson, ok := itemDetails["corr_person"].(map[string]interface{}); ok && corrPerson["name"] != nil {
corr = corrPerson["name"].(string)
} }
fmt.Printf("File already exists: '%s @ %s/app/item/%s'\n", itemName, dsUrl, itemID) if fileExistsResponse[0]["exists"].(bool) {
// File exists in Docspell
items := fileExistsResponse[0]["items"].([]interface{})
item := items[0].(map[string]interface{})
itemID := item["id"].(string)
itemName := item["name"].(string)
state := item["state"].(string) // Get item details
if state == "confirmed" { cmd = exec.Command("dsc", "-f", "json", "item", "get", itemID)
itemDate := item["item_date"] output, err = cmd.Output()
if itemDate == nil { if err != nil {
fmt.Println("... but has no date - not doing anything.") fmt.Printf("ERROR getting item details: %v\n", err)
return nil
}
var itemDetails map[string]interface{}
if err := json.Unmarshal(output, &itemDetails); err != nil {
fmt.Printf("ERROR parsing item details: %v\n", err)
return nil
}
folder := itemDetails["folder"].(map[string]interface{})["name"].(string)
extension := filepath.Ext(path)[1:]
var corr string
if corrOrg, ok := itemDetails["corr_org"].(map[string]interface{}); ok && corrOrg["name"] != nil {
corr = corrOrg["name"].(string)
} else if corrPerson, ok := itemDetails["corr_person"].(map[string]interface{}); ok && corrPerson["name"] != nil {
corr = corrPerson["name"].(string)
}
fmt.Printf("File already exists: '%s @ %s/app/item/%s'\n", itemName, dsUrl, itemID)
state := item["state"].(string)
if state == "confirmed" {
itemDate := item["item_date"]
if itemDate == nil {
fmt.Println("... but has no date - not doing anything.")
} else {
timestamp := int64(itemDate.(float64)) / 1000
date := time.Unix(timestamp, 0)
curDir := filepath.Join(dsArchivedir, folder, date.Format("2006/01"))
if err := os.MkdirAll(curDir, 0755); err != nil {
fmt.Printf("ERROR creating directory: %v\n", err)
return nil
}
newPath := filepath.Join(curDir, fmt.Sprintf("%s %s - %s.%s",
date.Format("20060102"), corr, itemName, extension))
if err := os.Rename(path, newPath); err != nil {
fmt.Printf("ERROR moving file: %v\n", err)
return nil
}
fmt.Printf("... moving to archive by date ('%s')\n", curDir)
}
} else { } else {
timestamp := int64(itemDate.(float64)) / 1000 fmt.Println("... but is not confirmed yet - not doing anything.")
date := time.Unix(timestamp, 0)
curDir := filepath.Join(dsArchivedir, folder, date.Format("2006/01"))
if err := os.MkdirAll(curDir, 0755); err != nil {
fmt.Printf("ERROR creating directory: %v\n", err)
return nil
}
newPath := filepath.Join(curDir, fmt.Sprintf("%s %s - %s.%s",
date.Format("20060102"), corr, itemName, extension))
if err := os.Rename(path, newPath); err != nil {
fmt.Printf("ERROR moving file: %v\n", err)
return nil
}
fmt.Printf("... moving to archive by date ('%s')\n", curDir)
} }
} else { } else {
fmt.Println("... but is not confirmed yet - not doing anything.") fmt.Println("Files does not exist, yet")
} if uploadMissing {
} else { fmt.Print("...uploading file..")
fmt.Println("Files does not exist, yet") cmd = exec.Command("dsc", "-f", "json", "upload", path)
if uploadMissing { output, err := cmd.Output()
fmt.Print("...uploading file..") if err != nil {
cmd = exec.Command("dsc", "-f", "json", "upload", path) fmt.Printf("\nERROR uploading: %v\n", err)
output, err := cmd.Output() return nil
if err != nil { }
fmt.Printf("\nERROR uploading: %v\n", err)
return nil
}
var uploadResult map[string]interface{} var uploadResult map[string]interface{}
if err := json.Unmarshal(output, &uploadResult); err != nil { if err := json.Unmarshal(output, &uploadResult); err != nil {
fmt.Printf("\nERROR parsing upload result: %v\n", err) fmt.Printf("\nERROR parsing upload result: %v\n", err)
return nil return nil
} }
if uploadResult["success"].(bool) { if uploadResult["success"].(bool) {
fmt.Println(". done") fmt.Println(". done")
} else { } else {
fmt.Printf("\nERROR %v\n", uploadResult) fmt.Printf("\nERROR %v\n", uploadResult)
}
} }
} }
return nil
})
if err != nil {
fmt.Printf("Error walking directory: %v\n", err)
os.Exit(1)
} }
return nil
})
if err != nil {
fmt.Printf("Error walking directory: %v\n", err)
os.Exit(1)
} }
fmt.Println("################# DONE #################") fmt.Println("################# DONE #################")