Skip to content

Commit

Permalink
Fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SumiGovindaraju committed Jan 24, 2019
1 parent cbe40b3 commit cc97d29
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion mail_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ func (message *MailMessage) saveAttachments() error {
err = os.MkdirAll(basePath+"/images", 0755)
if err != nil {
goqueryErr = err
return
}
}

Expand All @@ -261,12 +262,13 @@ func (message *MailMessage) saveAttachments() error {
resp, err := http.Get(src)
if err != nil {
goqueryErr = err
return
}
defer resp.Body.Close()

lastIndexOfSlash := strings.LastIndex(src, "/")
var fileName string
if lastIndexOfSlash == -1 {
if lastIndexOfSlash != -1 {
fileName = fmt.Sprintf("%d_%s.jpg", i, src[lastIndexOfSlash:len(src)])
} else {
fileName = fmt.Sprintf("%d.jpg", i)
Expand All @@ -275,11 +277,13 @@ func (message *MailMessage) saveAttachments() error {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
goqueryErr = err
return
}

err = ioutil.WriteFile(fmt.Sprintf("%s/images/%s", basePath, fileName), body, 0644)
if err != nil {
goqueryErr = err
return
}

s.SetAttr("src", fmt.Sprintf("%s/%s/images/%s", config.GetString("attachment_base_url"), message.attachmentDir, fileName))
Expand Down

0 comments on commit cc97d29

Please sign in to comment.