diff --git a/README.md b/README.md index 0a9b507..6bebb79 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ On top of that, there are golang utilities: - `s3_monitor`, monitors remote bucket for changes and downloads files if new ones were added (also removing them from the bucket). Adds `.eml` suffix to the files -- `eml_unpack_attachments`, monitors local directory for new files and unpacks attachments from them (assumes that new files are .eml) +- `eml_unpack_attachments`, monitors local directory for new `.eml` files and unpacks attachments from them ## How does it work? @@ -118,6 +118,8 @@ In both cases you'll have to set few environment variables for the program to wo - INPUT_DIRECTORY (local directory to monitor for `.eml` files) - OUTPUT_DIRECTORY (where to put the unpacked attachments) +After the `.eml` file is unpacked, it is removed as it's not needed anymore. + ## But why? I have recently started using [Paperless-ngx](https://docs.paperless-ngx.com/) that supports documents consumption via e-mail. diff --git a/eml_unpack_attachments/main.go b/eml_unpack_attachments/main.go index ea9ba2c..7462fed 100644 --- a/eml_unpack_attachments/main.go +++ b/eml_unpack_attachments/main.go @@ -6,6 +6,7 @@ import ( "log" "os" "path" + "strings" "github.com/DusanKasan/parsemail" "github.com/fsnotify/fsnotify" @@ -24,9 +25,24 @@ func requireEnvVariable(name string) string { return value } -func processEmail(filename string, outputDirectory string) error { +func processNewFile(filename string, outputDirectory string) error { log.Print("Processing file: ", filename) + if !strings.HasSuffix(filename, ".eml") { + log.Print("File doesn't have .eml extension, skipping: ", filename) + return nil + } + + fileInfo, err := os.Stat(filename) + if err != nil { + return err + } + + if fileInfo.IsDir() { + log.Print("File is a directory, skipping: ", filename) + return nil + } + reader, err := os.Open(filename) if err != nil { return err @@ -85,7 +101,7 @@ func main() { if event.Has(fsnotify.Create) { log.Print("New file: ", event.Name) - err := processEmail(event.Name, outputDirectory) + err := processNewFile(event.Name, outputDirectory) if err != nil { log.Print(err) } @@ -114,7 +130,7 @@ func main() { } for _, file := range files { - err := processEmail(path.Join(inputDirectory, file.Name()), outputDirectory) + err := processNewFile(path.Join(inputDirectory, file.Name()), outputDirectory) if err != nil { log.Fatal(err) }