Skip to content

Commit b40da85

Browse files
committed
set correct mime type and add uploading content to ImageBlock
1 parent ba4a9e7 commit b40da85

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

Src/Notion.Client/Models/File/FileObject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Notion.Client
66
{
77
[JsonConverter(typeof(JsonSubtypes), "type")]
88
[JsonSubtypes.KnownSubTypeAttribute(typeof(UploadedFile), "file")]
9+
[JsonSubtypes.KnownSubTypeAttribute(typeof(UploadingFile), "file_upload")]
910
[JsonSubtypes.KnownSubTypeAttribute(typeof(ExternalFile), "external")]
1011
public abstract class FileObject : IPageIcon
1112
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
4+
namespace Notion.Client
5+
{
6+
public class UploadingFile : FileObject
7+
{
8+
public override string Type => "file_upload";
9+
10+
[JsonProperty("file_upload")]
11+
public Info FileUpload { get; set; }
12+
13+
public class Info
14+
{
15+
[JsonProperty("id")]
16+
public Guid Id { get; set; }
17+
}
18+
}
19+
}

Src/Notion.Client/RestClient/IRestClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.IO;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using Newtonsoft.Json;
@@ -37,5 +38,6 @@ Task DeleteAsync(
3738
CancellationToken cancellationToken = default);
3839

3940
Task<RestClient.UploadResponse> Upload(string filePath, JsonSerializerSettings serializerSettings = null);
41+
Task<RestClient.UploadResponse> Upload(Stream stream, string filename, JsonSerializerSettings serializerSettings = null);
4042
}
4143
}

Src/Notion.Client/RestClient/RestClient.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,29 @@ public async Task<UploadResponse> Upload(Stream stream, string filename, JsonSer
106106
using (var formData = new MultipartFormDataContent())
107107
{
108108
var fileContent = new StreamContent(stream);
109-
formData.Add(fileContent, "file", filename);
110109

111-
var uploadResponse = await this.SendAsync(response.UploadUrl, HttpMethod.Post, attachContent: message =>
110+
var mimeMapping = new Dictionary<string, string>
111+
{
112+
{".jpg", "image/jpeg"},
113+
{".jpeg", "image/jpeg"},
114+
{".png", "image/png"},
115+
{".tif", "image/tiff"},
116+
{".tiff", "image/tiff"},
117+
{".gif", "image/gif"},
118+
{".svg", "image/svg+xml"}
119+
};
120+
121+
if (mimeMapping.TryGetValue(Path.GetExtension(filename).ToLower(), out var mapping))
112122
{
113-
message.Content = formData;
114-
});
123+
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(mapping);
124+
}
125+
formData.Add(fileContent, "file", filename);
126+
127+
var uploadResponse = await this.SendAsync(response.UploadUrl, HttpMethod.Post,
128+
attachContent: message =>
129+
{
130+
message.Content = formData;
131+
});
115132
return await uploadResponse.ParseStreamAsync<UploadResponse>(serializerSettings);
116133
}
117134
}

Test/Notion.IntegrationTests/PageClientTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public async Task Test_CanUploadAndSetProfilePicture()
198198
new RichTextText {Text = new Text {Content = "Test Page Title"}}
199199
}
200200
})
201-
.AddProperty("Number", new NumberPropertyValue() {Number = 123})
201+
.AddProperty("Number", new NumberPropertyValue {Number = 123})
202202
.AddProperty("Profile picture",
203203
new FilesPropertyValue
204204
{
@@ -208,6 +208,10 @@ public async Task Test_CanUploadAndSetProfilePicture()
208208
}
209209
}
210210
)
211+
.AddPageContent(new ImageBlock
212+
{
213+
Image = new UploadingFile {FileUpload = new UploadingFile.Info {Id = upload.Id}}
214+
})
211215
.Build();
212216

213217
var page = await Client.Pages.CreateAsync(pagesCreateParameters);

0 commit comments

Comments
 (0)