Skip to content

Commit

Permalink
[GH-506] Correctly link or embed attachments (#517)
Browse files Browse the repository at this point in the history
* Recognize embeddable attachments and just link the rest of the attachments

* Fix and add missing mimes and change them to array form

* Add mp4 as embeddable item
  • Loading branch information
larkox authored Apr 24, 2020
1 parent b7404c9 commit 0a6a9f5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
14 changes: 7 additions & 7 deletions server/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type IssueService interface {
GetIssue(key string, options *jira.GetQueryOptions) (*jira.Issue, error)
CreateIssue(issue *jira.Issue) (*jira.Issue, error)

AddAttachment(api plugin.API, issueKey, fileID string, maxSize utils.ByteSize) (mattermostName, jiraName string, err error)
AddAttachment(api plugin.API, issueKey, fileID string, maxSize utils.ByteSize) (mattermostName, jiraName, mime string, err error)
AddComment(issueKey string, comment *jira.Comment) (*jira.Comment, error)
DoTransition(issueKey, transitionID string) error
GetCreateMeta(*jira.GetQueryOptions) (*jira.CreateMetaInfo, error)
Expand Down Expand Up @@ -261,27 +261,27 @@ func (client JiraClient) DoTransition(issueKey, transitionID string) error {

// AddAttachment uploads a file attachment
func (client JiraClient) AddAttachment(api plugin.API, issueKey, fileID string, maxSize utils.ByteSize) (
mattermostName, jiraName string, err error) {
mattermostName, jiraName, mime string, err error) {

fileinfo, appErr := api.GetFileInfo(fileID)
if appErr != nil {
return "", "", appErr
return "", "", "", appErr
}
if utils.ByteSize(fileinfo.Size) > maxSize {
return fileinfo.Name, "",
return fileinfo.Name, "", fileinfo.MimeType,
errors.Errorf("Maximum attachment size %v exceeded, file size %v", maxSize, utils.ByteSize(fileinfo.Size))
}

fileBytes, appErr := api.ReadFile(fileinfo.Path)
if appErr != nil {
return "", "", appErr
return "", "", "", appErr
}
attachment, err := client.RESTPostAttachment(issueKey, fileBytes, fileinfo.Name)
if err != nil {
return fileinfo.Name, "", err
return fileinfo.Name, "", fileinfo.MimeType, err
}

return fileinfo.Name, attachment.Filename, nil
return fileinfo.Name, attachment.Filename, fileinfo.MimeType, nil
}

// GetSelf returns a user associated with this Jira client
Expand Down
10 changes: 7 additions & 3 deletions server/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func httpAPICreateIssue(ji Instance, w http.ResponseWriter, r *http.Request) (in
go func() {
conf := ji.GetPlugin().getConfig()
for _, fileId := range post.FileIds {
mattermostName, _, e := client.AddAttachment(api, created.ID, fileId, conf.maxAttachmentSize)
mattermostName, _, _, e := client.AddAttachment(api, created.ID, fileId, conf.maxAttachmentSize)
if e != nil {
notifyOnFailedAttachment(ji, mattermostUserId, created.Key, e, "file: %s", mattermostName)
}
Expand Down Expand Up @@ -630,12 +630,16 @@ func httpAPIAttachCommentToIssue(ji Instance, w http.ResponseWriter, r *http.Req
conf := ji.GetPlugin().getConfig()
extraText := ""
for _, fileId := range post.FileIds {
mattermostName, jiraName, e := client.AddAttachment(api, attach.IssueKey, fileId, conf.maxAttachmentSize)
mattermostName, jiraName, mime, e := client.AddAttachment(api, attach.IssueKey, fileId, conf.maxAttachmentSize)
if e != nil {
notifyOnFailedAttachment(ji, mattermostUserId, attach.IssueKey, e, "file: %s", mattermostName)
}

extraText += "\n\nAttachment: !" + jiraName + "!"
if isImageMIME(mime) || isEmbbedableMIME(mime) {
extraText += "\n\nAttachment: !" + jiraName + "!"
} else {
extraText += "\n\nAttachment: [^" + jiraName + "]"
}
}
if extraText == "" {
return
Expand Down
35 changes: 35 additions & 0 deletions server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,38 @@ func parseJIRAIssuesFromText(text string, keys []string) []string {

return issues
}

func isImageMIME(mime string) bool {
return strings.HasPrefix(mime, "image")
}

func isEmbbedableMIME(mime string) bool {
validMimes := [...]string{
// .swf
"application/x-shockwave-flash",
// .mov
"video/quicktime",
// .rm
"application/vnd.rn-realmedia",
// .ram
"audio/x-pn-realaudio",
// .mp3
"audio/mpeg3",
"audio/x-mpeg-3",
"video/mpeg",
"video/x-mpeg",
// .mp4
"video/mp4",
// .wmv
"video/x-ms-wmv",
"video/x-ms-asf",
// .wma
"audio/x-ms-wma",
}
for _, validMime := range validMimes {
if mime == validMime {
return true
}
}
return false
}

0 comments on commit 0a6a9f5

Please sign in to comment.