@@ -183,6 +183,71 @@ def files_sharedPublicURL(options = {})
183183 def files_upload ( options = { } )
184184 post ( 'files.upload' , options )
185185 end
186+
187+ #
188+ # Uploads or creates a file (V2).
189+ #
190+ # @option options [string] :content
191+ # File contents via a POST variable. If omitting this parameter, you must provide a file.
192+ # @option options [file] :file
193+ # File contents via multipart/form-data. If omitting this parameter, you must submit content.
194+ #
195+ # params for files_getUploadURLExternal:
196+ #
197+ # @option options [string] :filename
198+ # Name of the file being uploaded.
199+ # @option options [string] :alt_txt
200+ # Description of image for screen-reader.
201+ # @option options [string] :snippet_type
202+ # Syntax type of the snippet being uploaded.
203+ #
204+ # params for files_completeUploadExternal:
205+ #
206+ # @option options [Object] :channel_id
207+ # Channel ID where the file will be shared. If not specified the file will be private.
208+ # @option options [string] :initial_comment
209+ # The message text introducing the file in specified channels.
210+ # @option options [string] :thread_ts
211+ # Provide another message's ts value to upload this file as a reply. Never use a reply's ts value; use its parent instead.
212+ #
213+ # @see https://api.slack.com/messaging/files#uploading_files
214+ # @see https://github.com/slackapi/node-slack-sdk/blob/96d846a79878f0d33893d14a9942ed9c4f678f2d/packages/web-api/src/WebClient.ts#L495
215+ # @see https://github.com/slackapi/python-slack-sdk/blob/a7223d9852de8a4ef9156552d7c50a92ec92669e/slack_sdk/web/client.py#L3737
216+ def files_upload_v2 ( options = { } )
217+ file = ::Faraday ::UploadIO . new ( StringIO . new ( options [ :content ] ) , 'text/plain' ) if options [ :content ]
218+ file ||= options [ :file ]
219+ if file . nil?
220+ raise (
221+ ArgumentError , 'Either a file or content field is required for valid file upload. You must supply one'
222+ )
223+ end
224+ # step 1: get upload url
225+ response = files_getUploadURLExternal (
226+ length : file . size , **options . slice ( :filename , :length , :alt_txt , :snippet_type )
227+ )
228+ upload_url = response [ :upload_url ]
229+ file_id = response [ :file_id ]
230+
231+ file = ::Faraday ::UploadIO . new ( StringIO . new ( options [ :content ] ) , 'text/plain' ) if options [ :content ]
232+ file ||= options [ :file ]
233+ if file . nil?
234+ raise (
235+ ArgumentError , 'Either a file or content field is required for valid file upload. You must supply one'
236+ )
237+ end
238+
239+ # step 2: upload file
240+ post (
241+ upload_url ,
242+ { file : file }
243+ )
244+
245+ # step 3: complete upload
246+ files_completeUploadExternal (
247+ files : [ { id : file_id , title : options [ :filename ] } ] . to_json ,
248+ **options . slice ( :channel_id , :initial_comment , :thread_ts )
249+ )
250+ end
186251 end
187252 end
188253 end
0 commit comments