4
4
using System . Threading ;
5
5
using System . Threading . Tasks ;
6
6
7
+ #if NETSTANDARD2_0
8
+ using Microsoft . AspNetCore . StaticFiles ;
9
+ #else
10
+ using System . Web ;
11
+ #endif
12
+
7
13
using Todoist . Net . Models ;
8
14
9
15
namespace Todoist . Net . Services
@@ -16,6 +22,10 @@ internal class UploadService : IUploadService
16
22
{
17
23
private readonly IAdvancedTodoistClient _todoistClient ;
18
24
25
+ #if NETSTANDARD2_0
26
+ private static readonly FileExtensionContentTypeProvider MimeProvider = new FileExtensionContentTypeProvider ( ) ;
27
+ #endif
28
+
19
29
internal UploadService ( IAdvancedTodoistClient todoistClient )
20
30
{
21
31
_todoistClient = todoistClient ;
@@ -45,34 +55,17 @@ public Task<FileAttachment> UploadAsync(
45
55
string fileName , byte [ ] fileContent , CancellationToken cancellationToken = default
46
56
)
47
57
{
48
- var data = new MultipartFormDataContent
49
- {
50
- {
51
- new ByteArrayContent ( fileContent ) , "file" , fileName
52
- }
53
- } ;
54
-
55
- return _todoistClient . PostFormAsync < FileAttachment > ( "uploads/add" , data , cancellationToken ) ;
56
- }
58
+ #if NETSTANDARD2_0
59
+ MimeProvider . TryGetContentType ( fileName , out var mimeType ) ;
60
+ #else
61
+ var mimeType = MimeMapping . GetMimeMapping ( fileName ) ;
62
+ #endif
57
63
58
- /// <inheritdoc/>
59
- public Task < FileAttachment > UploadAsync (
60
- string fileName , string mimeType , byte [ ] fileContent , CancellationToken cancellationToken = default
61
- )
62
- {
63
- var mime = mimeType != null ? MediaTypeHeaderValue . Parse ( mimeType ) : null ;
64
- var data = new MultipartFormDataContent
65
- {
66
- {
67
- new ByteArrayContent ( fileContent )
68
- {
69
- Headers = { ContentType = mime }
70
- } ,
71
- "file" , fileName
72
- }
73
- } ;
64
+ var parameters = new Dictionary < string , string > ( ) ;
65
+ var file = new FormFile ( fileContent , fileName , mimeType ) ;
66
+ var files = new [ ] { file } ;
74
67
75
- return _todoistClient . PostFormAsync < FileAttachment > ( "uploads/add" , data , cancellationToken ) ;
68
+ return _todoistClient . PostFormAsync < FileAttachment > ( "uploads/add" , parameters , files , cancellationToken ) ;
76
69
}
77
70
}
78
71
}
0 commit comments