15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
+ using System ;
18
19
using System . IO ;
20
+ using RestSharp ;
21
+ using RestSharp . Extensions ;
19
22
20
23
namespace SymphonyOSS . RestApiClient . Api . AgentApi
21
24
{
@@ -56,11 +59,14 @@ public AttachmentsApi(IAuthTokens authTokens, Configuration configuration, IApiE
56
59
/// be used on a message in that stream.
57
60
/// </summary>
58
61
/// <param name="sid">Stream ID.</param>
59
- /// <param name="file">The file to upload as an attachment.</param>
62
+ /// <param name="file">The path of the file to upload as an attachment.</param>
60
63
/// <returns>Attachment info.</returns>
61
- public AttachmentInfo UploadAttachment ( string sid , Stream file )
64
+ public AttachmentInfo UploadAttachment ( string sid , string file )
62
65
{
63
- return _apiExecutor . Execute ( _attachmentsApi . V1StreamSidAttachmentCreatePost , sid , _authTokens . SessionToken , _authTokens . KeyManagerToken , file ) ;
66
+ using ( var stream = File . OpenRead ( file ) )
67
+ {
68
+ return _apiExecutor . Execute ( UploadAttachment , sid , Path . GetFileName ( file ) , stream ) ;
69
+ }
64
70
}
65
71
66
72
/// <summary>
@@ -74,5 +80,17 @@ public byte[] DownloadAttachment(string sid, string messageId, string fileId)
74
80
{
75
81
return _apiExecutor . Execute ( _attachmentsApi . V1StreamSidAttachmentGet , sid , fileId , messageId , _authTokens . SessionToken , _authTokens . KeyManagerToken ) ;
76
82
}
83
+
84
+ private AttachmentInfo UploadAttachment ( string sid , string filename , Stream file )
85
+ {
86
+ var request = new RestRequest ( "v1/stream/" + sid + "/attachment/create" , Method . POST ) ;
87
+ request . AddHeader ( "sessionToken" , _authTokens . SessionToken ) ;
88
+ request . AddHeader ( "keyManagerToken" , _authTokens . KeyManagerToken ) ;
89
+ request . AddFile ( "file" , file . ReadAsBytes ( ) , filename , "application/octet-stream" ) ;
90
+
91
+ var apiClient = _attachmentsApi . Configuration . ApiClient ;
92
+ var response = apiClient . RestClient . Execute ( request ) ;
93
+ return ( AttachmentInfo ) apiClient . Deserialize ( response , typeof ( AttachmentInfo ) ) ;
94
+ }
77
95
}
78
96
}
0 commit comments