| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "net/url" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | const ( |
| 13 | DEFAULT_REMOTE_FILES_CHANNEL = "" |
| 14 | DEFAULT_REMOTE_FILES_TS_FROM = 0 |
| 15 | DEFAULT_REMOTE_FILES_TS_TO = -1 |
| 16 | DEFAULT_REMOTE_FILES_COUNT = 100 |
| 17 | ) |
| 18 | |
| 19 | // RemoteFile contains all the information for a remote file |
| 20 | // For more details: |
| 21 | // https://api.slack.com/messaging/files/remote |
| 22 | type RemoteFile struct { |
| 23 | ID string `json:"id"` |
| 24 | Created JSONTime `json:"created"` |
| 25 | Timestamp JSONTime `json:"timestamp"` |
| 26 | Name string `json:"name"` |
| 27 | Title string `json:"title"` |
| 28 | Mimetype string `json:"mimetype"` |
| 29 | Filetype string `json:"filetype"` |
| 30 | PrettyType string `json:"pretty_type"` |
| 31 | User string `json:"user"` |
| 32 | Editable bool `json:"editable"` |
| 33 | Size int `json:"size"` |
| 34 | Mode string `json:"mode"` |
| 35 | IsExternal bool `json:"is_external"` |
| 36 | ExternalType string `json:"external_type"` |
| 37 | IsPublic bool `json:"is_public"` |
| 38 | PublicURLShared bool `json:"public_url_shared"` |
| 39 | DisplayAsBot bool `json:"display_as_bot"` |
| 40 | Username string `json:"username"` |
| 41 | URLPrivate string `json:"url_private"` |
| 42 | Permalink string `json:"permalink"` |
| 43 | CommentsCount int `json:"comments_count"` |
| 44 | IsStarred bool `json:"is_starred"` |
| 45 | Shares Share `json:"shares"` |
| 46 | Channels []string `json:"channels"` |
| 47 | Groups []string `json:"groups"` |
| 48 | IMs []string `json:"ims"` |
| 49 | ExternalID string `json:"external_id"` |
| 50 | ExternalURL string `json:"external_url"` |
| 51 | HasRichPreview bool `json:"has_rich_preview"` |
| 52 | } |
| 53 | |
| 54 | // RemoteFileParameters contains required and optional parameters for a remote file. |
| 55 | // |
| 56 | // ExternalID is a user defined GUID, ExternalURL is where the remote file can be accessed, |
| 57 | // and Title is the name of the file. |
| 58 | // |
| 59 | // For more details: |
| 60 | // https://api.slack.com/methods/files.remote.add |
| 61 | type RemoteFileParameters struct { |
| 62 | ExternalID string // required |
| 63 | ExternalURL string // required |
| 64 | Title string // required |
| 65 | Filetype string |
| 66 | IndexableFileContents string |
| 67 | PreviewImage string |
| 68 | PreviewImageReader io.Reader |
| 69 | } |
| 70 | |
| 71 | // ListRemoteFilesParameters contains arguments for the ListRemoteFiles method. |
| 72 | // For more details: |
| 73 | // https://api.slack.com/methods/files.remote.list |
| 74 | type ListRemoteFilesParameters struct { |
| 75 | Channel string |
| 76 | Cursor string |
| 77 | Limit int |
| 78 | TimestampFrom JSONTime |
| 79 | TimestampTo JSONTime |
| 80 | } |
| 81 | |
| 82 | type remoteFileResponseFull struct { |
| 83 | RemoteFile `json:"file"` |
| 84 | Paging `json:"paging"` |
| 85 | Files []RemoteFile `json:"files"` |
| 86 | SlackResponse |
| 87 | } |
| 88 | |
| 89 | func (api *Client) remoteFileRequest(ctx context.Context, path string, values url.Values) (*remoteFileResponseFull, error) { |
| 90 | response := &remoteFileResponseFull{} |
| 91 | err := api.postMethod(ctx, path, values, response) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | |
| 96 | return response, response.Err() |
| 97 | } |
| 98 | |
| 99 | // AddRemoteFile adds a remote file. Unlike regular files, remote files must be explicitly shared. |
| 100 | // For more details: |
| 101 | // https://api.slack.com/methods/files.remote.add |
| 102 | func (api *Client) AddRemoteFile(params RemoteFileParameters) (*RemoteFile, error) { |
| 103 | return api.AddRemoteFileContext(context.Background(), params) |
| 104 | } |
| 105 | |
| 106 | // AddRemoteFileContext adds a remote file and setting a custom context |
| 107 | // For more details see the AddRemoteFile documentation. |
| 108 | func (api *Client) AddRemoteFileContext(ctx context.Context, params RemoteFileParameters) (remotefile *RemoteFile, err error) { |
| 109 | if params.ExternalID == "" || params.ExternalURL == "" || params.Title == "" { |
| 110 | return nil, ErrParametersMissing |
| 111 | } |
| 112 | response := &remoteFileResponseFull{} |
| 113 | values := url.Values{ |
| 114 | "token": {api.token}, |
| 115 | "external_id": {params.ExternalID}, |
| 116 | "external_url": {params.ExternalURL}, |
| 117 | "title": {params.Title}, |
| 118 | } |
| 119 | if params.Filetype != "" { |
| 120 | values.Add("filetype", params.Filetype) |
| 121 | } |
| 122 | if params.IndexableFileContents != "" { |
| 123 | values.Add("indexable_file_contents", params.IndexableFileContents) |
| 124 | } |
| 125 | if params.PreviewImage != "" { |
| 126 | err = postLocalWithMultipartResponse(ctx, api.httpclient, api.endpoint+"files.remote.add", params.PreviewImage, "preview_image", api.token, values, response, api) |
| 127 | } else if params.PreviewImageReader != nil { |
| 128 | err = postWithMultipartResponse(ctx, api.httpclient, api.endpoint+"files.remote.add", "preview.png", "preview_image", api.token, values, params.PreviewImageReader, response, api) |
| 129 | } else { |
| 130 | response, err = api.remoteFileRequest(ctx, "files.remote.add", values) |
| 131 | } |
| 132 | |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | |
| 137 | return &response.RemoteFile, response.Err() |
| 138 | } |
| 139 | |
| 140 | // ListRemoteFiles retrieves all remote files according to the parameters given. Uses cursor based pagination. |
| 141 | // For more details: |
| 142 | // https://api.slack.com/methods/files.remote.list |
| 143 | func (api *Client) ListRemoteFiles(params ListRemoteFilesParameters) ([]RemoteFile, error) { |
| 144 | return api.ListRemoteFilesContext(context.Background(), params) |
| 145 | } |
| 146 | |
| 147 | // ListRemoteFilesContext retrieves all remote files according to the parameters given with a custom context. Uses cursor based pagination. |
| 148 | // For more details see the ListRemoteFiles documentation. |
| 149 | func (api *Client) ListRemoteFilesContext(ctx context.Context, params ListRemoteFilesParameters) ([]RemoteFile, error) { |
| 150 | values := url.Values{ |
| 151 | "token": {api.token}, |
| 152 | } |
| 153 | if params.Channel != DEFAULT_REMOTE_FILES_CHANNEL { |
| 154 | values.Add("channel", params.Channel) |
| 155 | } |
| 156 | if params.TimestampFrom != DEFAULT_REMOTE_FILES_TS_FROM { |
| 157 | values.Add("ts_from", strconv.FormatInt(int64(params.TimestampFrom), 10)) |
| 158 | } |
| 159 | if params.TimestampTo != DEFAULT_REMOTE_FILES_TS_TO { |
| 160 | values.Add("ts_to", strconv.FormatInt(int64(params.TimestampTo), 10)) |
| 161 | } |
| 162 | if params.Limit != DEFAULT_REMOTE_FILES_COUNT { |
| 163 | values.Add("limit", strconv.Itoa(params.Limit)) |
| 164 | } |
| 165 | if params.Cursor != "" { |
| 166 | values.Add("cursor", params.Cursor) |
| 167 | } |
| 168 | |
| 169 | response, err := api.remoteFileRequest(ctx, "files.remote.list", values) |
| 170 | if err != nil { |
| 171 | return nil, err |
| 172 | } |
| 173 | |
| 174 | params.Cursor = response.SlackResponse.ResponseMetadata.Cursor |
| 175 | |
| 176 | return response.Files, nil |
| 177 | } |
| 178 | |
| 179 | // GetRemoteFileInfo retrieves the complete remote file information. |
| 180 | // For more details: |
| 181 | // https://api.slack.com/methods/files.remote.info |
| 182 | func (api *Client) GetRemoteFileInfo(externalID, fileID string) (remotefile *RemoteFile, err error) { |
| 183 | return api.GetRemoteFileInfoContext(context.Background(), externalID, fileID) |
| 184 | } |
| 185 | |
| 186 | // GetRemoteFileInfoContext retrieves the complete remote file information given with a custom context. |
| 187 | // For more details see the GetRemoteFileInfo documentation. |
| 188 | func (api *Client) GetRemoteFileInfoContext(ctx context.Context, externalID, fileID string) (remotefile *RemoteFile, err error) { |
| 189 | if fileID == "" && externalID == "" { |
| 190 | return nil, fmt.Errorf("either externalID or fileID is required") |
| 191 | } |
| 192 | if fileID != "" && externalID != "" { |
| 193 | return nil, fmt.Errorf("don't provide both externalID and fileID") |
| 194 | } |
| 195 | values := url.Values{ |
| 196 | "token": {api.token}, |
| 197 | } |
| 198 | if fileID != "" { |
| 199 | values.Add("file", fileID) |
| 200 | } |
| 201 | if externalID != "" { |
| 202 | values.Add("external_id", externalID) |
| 203 | } |
| 204 | response, err := api.remoteFileRequest(ctx, "files.remote.info", values) |
| 205 | if err != nil { |
| 206 | return nil, err |
| 207 | } |
| 208 | return &response.RemoteFile, err |
| 209 | } |
| 210 | |
| 211 | // ShareRemoteFile shares a remote file to channels |
| 212 | // For more details: |
| 213 | // https://api.slack.com/methods/files.remote.share |
| 214 | func (api *Client) ShareRemoteFile(channels []string, externalID, fileID string) (file *RemoteFile, err error) { |
| 215 | return api.ShareRemoteFileContext(context.Background(), channels, externalID, fileID) |
| 216 | } |
| 217 | |
| 218 | // ShareRemoteFileContext shares a remote file to channels with a custom context. |
| 219 | // For more details see the ShareRemoteFile documentation. |
| 220 | func (api *Client) ShareRemoteFileContext(ctx context.Context, channels []string, externalID, fileID string) (file *RemoteFile, err error) { |
| 221 | if channels == nil || len(channels) == 0 { |
| 222 | return nil, ErrParametersMissing |
| 223 | } |
| 224 | if fileID == "" && externalID == "" { |
| 225 | return nil, fmt.Errorf("either externalID or fileID is required") |
| 226 | } |
| 227 | values := url.Values{ |
| 228 | "token": {api.token}, |
| 229 | "channels": {strings.Join(channels, ",")}, |
| 230 | } |
| 231 | if fileID != "" { |
| 232 | values.Add("file", fileID) |
| 233 | } |
| 234 | if externalID != "" { |
| 235 | values.Add("external_id", externalID) |
| 236 | } |
| 237 | response, err := api.remoteFileRequest(ctx, "files.remote.share", values) |
| 238 | if err != nil { |
| 239 | return nil, err |
| 240 | } |
| 241 | return &response.RemoteFile, err |
| 242 | } |
| 243 | |
| 244 | // UpdateRemoteFile updates a remote file |
| 245 | // For more details: |
| 246 | // https://api.slack.com/methods/files.remote.update |
| 247 | func (api *Client) UpdateRemoteFile(fileID string, params RemoteFileParameters) (remotefile *RemoteFile, err error) { |
| 248 | return api.UpdateRemoteFileContext(context.Background(), fileID, params) |
| 249 | } |
| 250 | |
| 251 | // UpdateRemoteFileContext updates a remote file with a custom context |
| 252 | // For more details see the UpdateRemoteFile documentation. |
| 253 | func (api *Client) UpdateRemoteFileContext(ctx context.Context, fileID string, params RemoteFileParameters) (remotefile *RemoteFile, err error) { |
| 254 | response := &remoteFileResponseFull{} |
| 255 | values := url.Values{ |
| 256 | "token": {api.token}, |
| 257 | } |
| 258 | if fileID != "" { |
| 259 | values.Add("file", fileID) |
| 260 | } |
| 261 | if params.ExternalID != "" { |
| 262 | values.Add("external_id", params.ExternalID) |
| 263 | } |
| 264 | if params.ExternalURL != "" { |
| 265 | values.Add("external_url", params.ExternalURL) |
| 266 | } |
| 267 | if params.Title != "" { |
| 268 | values.Add("title", params.Title) |
| 269 | } |
| 270 | if params.Filetype != "" { |
| 271 | values.Add("filetype", params.Filetype) |
| 272 | } |
| 273 | if params.IndexableFileContents != "" { |
| 274 | values.Add("indexable_file_contents", params.IndexableFileContents) |
| 275 | } |
| 276 | if params.PreviewImageReader != nil { |
| 277 | err = postWithMultipartResponse(ctx, api.httpclient, api.endpoint+"files.remote.update", "preview.png", "preview_image", api.token, values, params.PreviewImageReader, response, api) |
| 278 | } else { |
| 279 | response, err = api.remoteFileRequest(ctx, "files.remote.update", values) |
| 280 | } |
| 281 | |
| 282 | if err != nil { |
| 283 | return nil, err |
| 284 | } |
| 285 | |
| 286 | return &response.RemoteFile, response.Err() |
| 287 | } |
| 288 | |
| 289 | // RemoveRemoteFile removes a remote file. |
| 290 | // For more details: |
| 291 | // https://api.slack.com/methods/files.remote.remove |
| 292 | func (api *Client) RemoveRemoteFile(externalID, fileID string) (err error) { |
| 293 | return api.RemoveRemoteFileContext(context.Background(), externalID, fileID) |
| 294 | } |
| 295 | |
| 296 | // RemoveRemoteFileContext removes a remote file with a custom context |
| 297 | // For more information see the RemoveRemoteFiles documentation. |
| 298 | func (api *Client) RemoveRemoteFileContext(ctx context.Context, externalID, fileID string) (err error) { |
| 299 | if fileID == "" && externalID == "" { |
| 300 | return fmt.Errorf("either externalID or fileID is required") |
| 301 | } |
| 302 | if fileID != "" && externalID != "" { |
| 303 | return fmt.Errorf("don't provide both externalID and fileID") |
| 304 | } |
| 305 | values := url.Values{ |
| 306 | "token": {api.token}, |
| 307 | } |
| 308 | if fileID != "" { |
| 309 | values.Add("file", fileID) |
| 310 | } |
| 311 | if externalID != "" { |
| 312 | values.Add("external_id", externalID) |
| 313 | } |
| 314 | _, err = api.remoteFileRequest(ctx, "files.remote.remove", values) |
| 315 | return err |
| 316 | } |