/
attachments.go
51 lines (42 loc) · 1.24 KB
/
attachments.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"./desk"
"./zendesk"
"fmt"
log "github.com/cihub/seelog"
)
func (migrator *Migrator) migrateAttachments(c *desk.Case, requester *zendesk.Customer) ([]zendesk.Comment, error) {
comments := make([]zendesk.Comment, 0)
for _, attachment := range c.Attachments {
err := migrator.deskClient.DownloadFile(&attachment, fmt.Sprintf("%s/attachments/", migrator.path))
if err != nil {
log.Errorf("Can't download attachment: %v. Ignoring.", err)
continue
}
token, err := migrator.migrateAttachment(&attachment, fmt.Sprintf("%s/attachments/", migrator.path))
if err != nil {
log.Errorf("Can't migrate attachment: %v. Ignoring", err)
continue
}
if token != "" {
comments = append(comments, zendesk.Comment{
AuthorId: requester.Id,
Value: attachment.FileName,
Uploads: []string{token},
})
}
}
return comments, nil
}
func (migrator *Migrator) migrateAttachment(attachment *desk.Attachment, directory string) (string, error) {
att := zendesk.Attachment{
FileName: attachment.FileName,
ContentType: attachment.ContentType,
Size: attachment.Size,
}
token, err := migrator.zenDeskClient.CreateAttachment(&att, directory)
if err != nil {
return "", err
}
return token, nil
}