feat: handle multiple directories
This commit is contained in:
parent
93f3ccd59d
commit
7d4cd72bb7
202
main.go
202
main.go
@ -74,125 +74,127 @@ func main() {
|
||||
fmt.Println()
|
||||
fmt.Println()
|
||||
|
||||
fmt.Printf("Scanning folder '%s'\n", dsConsumedir)
|
||||
fmt.Println()
|
||||
fmt.Println()
|
||||
for _, dsConsumedir := range cfg.ImportDirectories {
|
||||
fmt.Printf("Scanning folder '%s'\n", dsConsumedir)
|
||||
fmt.Println()
|
||||
fmt.Println()
|
||||
|
||||
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()
|
||||
err = filepath.Walk(dsConsumedir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
fmt.Printf("ERROR getting item details: %v\n", err)
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
var itemDetails map[string]interface{}
|
||||
if err := json.Unmarshal(output, &itemDetails); err != nil {
|
||||
fmt.Printf("ERROR parsing item details: %v\n", err)
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
var fileExistsResponse []map[string]interface{}
|
||||
if err := json.Unmarshal(output, &fileExistsResponse); err != nil {
|
||||
fmt.Printf("ERROR parsing response: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
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)
|
||||
if state == "confirmed" {
|
||||
itemDate := item["item_date"]
|
||||
if itemDate == nil {
|
||||
fmt.Println("... but has no date - not doing anything.")
|
||||
// Get item details
|
||||
cmd = exec.Command("dsc", "-f", "json", "item", "get", itemID)
|
||||
output, err = cmd.Output()
|
||||
if err != nil {
|
||||
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 {
|
||||
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)
|
||||
fmt.Println("... but is not confirmed yet - not doing anything.")
|
||||
}
|
||||
} else {
|
||||
fmt.Println("... but is not confirmed yet - not doing anything.")
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Files does not exist, yet")
|
||||
if uploadMissing {
|
||||
fmt.Print("...uploading file..")
|
||||
cmd = exec.Command("dsc", "-f", "json", "upload", path)
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
fmt.Printf("\nERROR uploading: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
fmt.Println("Files does not exist, yet")
|
||||
if uploadMissing {
|
||||
fmt.Print("...uploading file..")
|
||||
cmd = exec.Command("dsc", "-f", "json", "upload", path)
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
fmt.Printf("\nERROR uploading: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
var uploadResult map[string]interface{}
|
||||
if err := json.Unmarshal(output, &uploadResult); err != nil {
|
||||
fmt.Printf("\nERROR parsing upload result: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
var uploadResult map[string]interface{}
|
||||
if err := json.Unmarshal(output, &uploadResult); err != nil {
|
||||
fmt.Printf("\nERROR parsing upload result: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
if uploadResult["success"].(bool) {
|
||||
fmt.Println(". done")
|
||||
} else {
|
||||
fmt.Printf("\nERROR %v\n", uploadResult)
|
||||
if uploadResult["success"].(bool) {
|
||||
fmt.Println(". done")
|
||||
} else {
|
||||
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 #################")
|
||||
|
Loading…
x
Reference in New Issue
Block a user