Skip to content

Commit

Permalink
eml_unpack_attachments: add filtering by ContentType
Browse files Browse the repository at this point in the history
  • Loading branch information
dezeroku committed Oct 2, 2024
1 parent fe4d297 commit 74c93d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ 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)
- ALLOWED_CONTENT_TYPES_REGEX (optional, defaults to `.*`)

After the `.eml` file is unpacked, it is removed as it's not needed anymore.

Expand Down
27 changes: 22 additions & 5 deletions eml_unpack_attachments/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"log"
"os"
"path"
"regexp"
"strings"

"github.com/fsnotify/fsnotify"
"github.com/mnako/letters"
)

var (
inputDirectoryEnvVar = "INPUT_DIRECTORY"
outputDirectoryEnvVar = "OUTPUT_DIRECTORY"
inputDirectoryEnvVar = "INPUT_DIRECTORY"
outputDirectoryEnvVar = "OUTPUT_DIRECTORY"
allowedContentTypesRegEnvVar = "ALLOWED_CONTENT_TYPES_REGEX"
)

func requireEnvVariable(name string) string {
Expand All @@ -24,7 +26,7 @@ func requireEnvVariable(name string) string {
return value
}

func processNewFile(filename string, outputDirectory string) error {
func processNewFile(filename string, outputDirectory string, allowedContentTypesReg *regexp.Regexp) error {
log.Print("Processing file: ", filename)

if !strings.HasSuffix(filename, ".eml") {
Expand Down Expand Up @@ -59,6 +61,13 @@ func processNewFile(filename string, outputDirectory string) error {
for _, a := range email.AttachedFiles {
attachmentName := a.ContentType.Params["name"]
log.Print(attachmentName)
contentType := a.ContentType.ContentType

if !allowedContentTypesReg.MatchString(contentType) {
log.Print("Not supported contentType, skipping: ", contentType)
continue
}

err := os.WriteFile(path.Join(outputDirectory, attachmentName), a.Data, 0644)
if err != nil {
return err
Expand All @@ -83,6 +92,14 @@ func main() {

os.MkdirAll(outputDirectory, os.ModePerm)

allowedContentTypesRegVar, present := os.LookupEnv(allowedContentTypesRegEnvVar)
if !present {
allowedContentTypesRegVar = ".*"
log.Printf("%s env variable not provided, defaulting to: %s\n", allowedContentTypesRegEnvVar, allowedContentTypesRegVar)
}

allowedContentTypesReg := regexp.MustCompile(allowedContentTypesRegVar)

watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
Expand All @@ -99,7 +116,7 @@ func main() {
if event.Has(fsnotify.Create) {
log.Print("New file: ", event.Name)

err := processNewFile(event.Name, outputDirectory)
err := processNewFile(event.Name, outputDirectory, allowedContentTypesReg)
if err != nil {
log.Print(err)
}
Expand Down Expand Up @@ -128,7 +145,7 @@ func main() {
}

for _, file := range files {
err := processNewFile(path.Join(inputDirectory, file.Name()), outputDirectory)
err := processNewFile(path.Join(inputDirectory, file.Name()), outputDirectory, allowedContentTypesReg)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 74c93d0

Please sign in to comment.