Mattermost, Inc.

Add file during MessageWillBePosted

I am attempting to add an attachment before a message is posted. Here is the error I am seeing in the logs.

{"level":"warn","ts":1557951449.9072876,"caller":"app/post.go:298","msg":"Failed to attach file to post","file_id":"oz6m7o4qmibhuxufk14j6uf1cr","post_id":"mjkiu9gtwjf1z83s6adwwfqyye","error":"SqlFileInfoStore.AttachToPost: Unable to attach the file info to the post, post_id=mjkiu9gtwjf1z83s6adwwfqyye, file_id=oz6m7o4qmibhuxufk14j6uf1cr"}

Am I approaching this the wrong way? I’m leaving stuff out but I believe the relevant API calls are

p.API.UploadFile(pdfdata, post.ChannelId, "my-sample.pdf")

post.FileIds = append(post.FileIds, newfileids...)

The file is being saved on the server filesystem successfully fwiw.

Hi @kevin.olson,

I believe the steps that the web app uses are to

  1. Upload the file.
  2. Create the post, including the uploaded file as part of the file_ids field of the post.

Thanks, looks like I needed to add CopyFileInfos to the mix, things seem to be working now.