Hello everyone, so am having this code that gets a multipart file from the request and sends it to a package in charge of sending it as an email attachment, the problem am facing is when i try to send the file directly as i fetch it from the request it just goes empty while if there is some other code running after, the file sending through email goes well. So am really not getting why this is happening, any help understanding what’s really happening here would be very appreciated
var files []*os.File
uploadedFilesData := req.MultipartForm.File["attachments"]
for _, v := range uploadedFilesData {
mFile, err := v.Open()
if err != nil {
helpers.ClientError(w, err, http.StatusUnprocessableEntity)
return
}
fileData, err := ioutil.ReadAll(mFile)
mFile.Close()
if err != nil {
helpers.ServerError(w, err, ec.appLogger, "Failed to read the multipart file data")
return
}
almostUniqueFileName := strconv.Itoa(int(time.Now().Unix())) + v.Filename
fileName := "/tmp/kb_messaging/" + almostUniqueFileName
newFile, err := os.Create(fileName)
if err != nil {
helpers.ServerError(w, err, ec.appLogger, "Failed to create tmp file")
return
}
_, err = newFile.Write(fileData)
if err != nil {
helpers.ServerError(w, err, ec.appLogger, "Failed to write multipart file data to tmp file")
return
}
files = append(files, newFile)
}
status, messageID, mErr := message.SendEmailWithFileAttachments(files, emailModel.Receiver)
Although this code does run, the attachments are just empty files. But if i separate the logic for storing the files and retrieving them before sending again, the files are now not empty.
uploadedFilesData := req.MultipartForm.File["attachments"]
for _, v := range uploadedFilesData {
mFile, err := v.Open()
if err != nil {
helpers.ClientError(w, err, http.StatusUnprocessableEntity)
return
}
fileData, err := ioutil.ReadAll(mFile)
mFile.Close()
if err != nil {
helpers.ServerError(w, err, ec.appLogger, "Failed to read the multipart file data")
return
}
almostUniqueFileName := strconv.Itoa(int(time.Now().Unix())) + v.Filename
fileName := "/tmp/kb_messaging/" + almostUniqueFileName
newFile, err := os.Create(fileName)
if err != nil {
helpers.ServerError(w, err, ec.appLogger, "Failed to create tmp file")
return
}
_, err = newFile.Write(fileData)
if err != nil {
helpers.ServerError(w, err, ec.appLogger, "Failed to write multipart file data to tmp file")
return
}
}
var files []*os.File
tmpDir, err := ioutil.ReadDir("/tmp/kb_messaging/")
if err != nil {
helpers.ServerError(w, result.Error, ec.appLogger, "Failed to open tmp dir")
return
}
for _, v := range tmpDir {
newFile, err := os.Open("/tmp/kb_messaging/" + v.Name())
if err != nil {
helpers.ServerError(w, result.Error, ec.appLogger, "Failed to read file from tmp dir")
return
}
defer newFile.Close()
files = append(files, newFile)
}
status, messageID, mErr := message.SendEmailWithFileAttachments(files, emailModel.Receiver)
This is really confusing although it works i don’t really understand why the first approach is not working as expected!
PS: am using this package, i developed myself for helping to send emails using mailgun: https://pkg.go.dev/github.com/nizigama/gomailer