From 273c739c3960b0c085a297ae6a296bfbf122040b Mon Sep 17 00:00:00 2001 From: sean t Date: Thu, 14 Nov 2024 10:57:05 +0800 Subject: [PATCH 1/3] Fix issues with creating folders and uploading --- Api/API.csproj | 45 ---------------------- Api/Entities/OneDriveCreateFolder.cs | 5 ++- Api/Entities/OneDriveItem.cs | 2 +- Api/Entities/OneDriveUploadSessionItem.cs | 2 +- Api/KoenZomers.OneDrive.Api.snk | Bin 596 -> 0 bytes Api/OneDrive.csproj | 28 ++++++++++++++ Api/OneDriveApi.cs | 13 ++++++- 7 files changed, 45 insertions(+), 50 deletions(-) delete mode 100644 Api/API.csproj delete mode 100644 Api/KoenZomers.OneDrive.Api.snk create mode 100644 Api/OneDrive.csproj diff --git a/Api/API.csproj b/Api/API.csproj deleted file mode 100644 index c62a305..0000000 --- a/Api/API.csproj +++ /dev/null @@ -1,45 +0,0 @@ - - - - netcoreapp3.1;net48;net6 - KoenZomers.OneDrive.Api - true - KoenZomers.OneDrive.Api.snk - 2.4.0.0 - Koen Zomers - Koen Zomers - API in .NET Framework 4.8.0, and .NET Core 3.1 and .NET 6.0 to communicate with OneDrive Personal and OneDrive for Business - https://github.com/KoenZomers/OneDriveAPI - true - - Recompiled to support .NET Framework 4.8.0, .NET Core 3.1 and .NET 6.0 -- Removed Newtonsoft JSON dependency by switching to System.Text.Json - https://github.com/KoenZomers/OneDriveAPI/blob/master/LICENSE.md - Koen Zomers - KoenZomers.OneDrive.Api - 2.4.0.0 - 2.4.0.0 - - - - - - - - - - - - - - - - - - KoenZomers.OneDrive.Api.xml - - - - KoenZomers.OneDrive.Api.xml - - - diff --git a/Api/Entities/OneDriveCreateFolder.cs b/Api/Entities/OneDriveCreateFolder.cs index e43d5fc..4bf0e63 100644 --- a/Api/Entities/OneDriveCreateFolder.cs +++ b/Api/Entities/OneDriveCreateFolder.cs @@ -2,12 +2,15 @@ namespace KoenZomers.OneDrive.Api.Entities { - internal class OneDriveCreateFolder : OneDriveItemBase + public class OneDriveCreateFolder : OneDriveItemBase { [JsonPropertyName("name")] public string Name { get; set; } [JsonPropertyName("folder")] public object Folder { get; set; } + + [JsonPropertyName("@microsoft.graph.conflictBehavior")] + public string ConflictBehavior { get; set; } = "rename"; } } diff --git a/Api/Entities/OneDriveItem.cs b/Api/Entities/OneDriveItem.cs index 01666e1..e728f45 100644 --- a/Api/Entities/OneDriveItem.cs +++ b/Api/Entities/OneDriveItem.cs @@ -129,7 +129,7 @@ public class OneDriveItem : OneDriveItemBase /// /// The conflict resolution behavior for actions that create a new item. An item will never be returned with this annotation. Write-only. /// - [JsonPropertyName("@name.conflictBehavior")] + [JsonPropertyName("@microsoft.graph.conflictBehavior")] public NameConflictBehavior? NameConflictBehahiorAnnotation { get; set; } /// diff --git a/Api/Entities/OneDriveUploadSessionItem.cs b/Api/Entities/OneDriveUploadSessionItem.cs index b76fc9e..94e9234 100644 --- a/Api/Entities/OneDriveUploadSessionItem.cs +++ b/Api/Entities/OneDriveUploadSessionItem.cs @@ -5,7 +5,7 @@ namespace KoenZomers.OneDrive.Api.Entities { internal class OneDriveUploadSessionItem { - [JsonPropertyName("@name.conflictBehavior")] + [JsonPropertyName("@microsoft.graph.conflictBehavior")] public NameConflictBehavior FilenameConflictBehavior { get; set; } [JsonPropertyName("name")] diff --git a/Api/KoenZomers.OneDrive.Api.snk b/Api/KoenZomers.OneDrive.Api.snk deleted file mode 100644 index 8dca5690596538a666800d1add8625f5eb2c4392..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 596 zcmV-a0;~N80ssI2Bme+XQ$aES1ONa50098Ow#5!YB&Ai|t{pU^uevY1Aw;Dtj`xg{ zsyW-LlI#K2Vt<7?#2go;jm0Ym*t2fmhA-2_n#q5_CSdztpwkrITaOSQKJQjVl!%q6 zGXSbPI#LAYo*72-+KhxoloSx71#{dcbB2;}rfI-4T;M>#+-a)Ih#4c89hvXs{92k7 ziONEJq%AC8Fu5!6Fd!2;{H`bb__jYOzL| z;7oczW!?IeQ1iI`i9poWN@N^Be0}Q=Hb<{6)~pUeAjOrfkQyk@M)Fyf;`xs({=4{H z?o9|NC_Ee_75L7=g+@FSpibOr*{0R0%LrMk*h<2RKLsw6eL25cIu|17nP(_-t(c7C zCw;LGxf))?vs%Lf*0GppfEHxPMhC@4t*W~&Y1wAy3sz)}RRDk%?9;r880F+*X|cMR z|K5F;{UV#Bsv6l=j9i~sG{>g&ax|{yOP+0r7ekWlfWKO{ iJui_63B)4(t4`8wk*dA2K6PF)e=R;Ty21B!O-Tjt7#u?Y diff --git a/Api/OneDrive.csproj b/Api/OneDrive.csproj new file mode 100644 index 0000000..80e2298 --- /dev/null +++ b/Api/OneDrive.csproj @@ -0,0 +1,28 @@ + + + + net9.0 + KoenZomers.OneDrive.Api + 2.5.0.0 + Koen Zomers + Koen Zomers + API in .NET 9.0 to communicate with OneDrive Personal and OneDrive for Business + https://github.com/ispysoftware/OneDriveAPI + true + - Recompiled to support .NET Framework 9.0 + https://github.com/KoenZomers/OneDriveAPI/blob/master/LICENSE.md + Koen Zomers + KoenZomers.OneDrive.Api + 2.5.0.0 + 2.5.0.0 + + + + KoenZomers.OneDrive.Api.xml + + + + KoenZomers.OneDrive.Api.xml + + + diff --git a/Api/OneDriveApi.cs b/Api/OneDriveApi.cs index 54b67a5..ec8849c 100644 --- a/Api/OneDriveApi.cs +++ b/Api/OneDriveApi.cs @@ -1620,7 +1620,8 @@ protected virtual async Task UploadFileViaResumableUploadInternal( // Trigger event UploadProgressChanged?.Invoke(this, new OneDriveUploadProgressChangedEventArgs(totalBytesSent, fileStream.Length)); break; - + case HttpStatusCode.Unauthorized: + throw new ApplicationException("unauthorized"); // All fragments have been received, the file did already exist and has been overwritten case HttpStatusCode.OK: // All fragments have been received, the file has been created @@ -1913,7 +1914,15 @@ protected virtual async Task RenameItemInternal(OneDriveItem oneDriveSourc /// Typed OneDrive entity with the result from the webservice protected virtual async Task SendMessageReturnOneDriveItem(OneDriveItemBase oneDriveItem, HttpMethod httpMethod, string url, HttpStatusCode? expectedHttpStatusCode = null) where T : OneDriveItemBase { - var bodyText = oneDriveItem != null ? JsonSerializer.Serialize(oneDriveItem) : null; + var options = new JsonSerializerOptions + { + WriteIndented = true, + // Add the JsonStringEnumConverter + Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + + var bodyText = oneDriveItem != null ? JsonSerializer.Serialize(oneDriveItem, oneDriveItem.GetType(), options) : null; return await SendMessageReturnOneDriveItem(bodyText, httpMethod, url, expectedHttpStatusCode); } From 61ac1b861e37b86ed1eaef6c9ba4add0c3cd8f26 Mon Sep 17 00:00:00 2001 From: sean t Date: Thu, 14 Nov 2024 14:44:23 +0800 Subject: [PATCH 2/3] fix serializzer --- Api/OneDriveApi.cs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/Api/OneDriveApi.cs b/Api/OneDriveApi.cs index ec8849c..01f5de6 100644 --- a/Api/OneDriveApi.cs +++ b/Api/OneDriveApi.cs @@ -21,6 +21,13 @@ namespace KoenZomers.OneDrive.Api /// public abstract class OneDriveApi { + public static JsonSerializerOptions JSONOptions = new JsonSerializerOptions + { + WriteIndented = true, + Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + #region Properties /// @@ -1914,15 +1921,7 @@ protected virtual async Task RenameItemInternal(OneDriveItem oneDriveSourc /// Typed OneDrive entity with the result from the webservice protected virtual async Task SendMessageReturnOneDriveItem(OneDriveItemBase oneDriveItem, HttpMethod httpMethod, string url, HttpStatusCode? expectedHttpStatusCode = null) where T : OneDriveItemBase { - var options = new JsonSerializerOptions - { - WriteIndented = true, - // Add the JsonStringEnumConverter - Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }, - PropertyNamingPolicy = JsonNamingPolicy.CamelCase - }; - - var bodyText = oneDriveItem != null ? JsonSerializer.Serialize(oneDriveItem, oneDriveItem.GetType(), options) : null; + var bodyText = oneDriveItem != null ? JsonSerializer.Serialize(oneDriveItem, oneDriveItem.GetType(), JSONOptions) : null; return await SendMessageReturnOneDriveItem(bodyText, httpMethod, url, expectedHttpStatusCode); } @@ -1992,11 +1991,7 @@ protected virtual async Task SendMessageReturnString(string bodyText, Ht /// Bool indicating if the HTTP response status from the webservice matched the provided expectedHttpStatusCode protected virtual async Task SendMessageReturnBool(OneDriveItemBase oneDriveItem, HttpMethod httpMethod, string url, HttpStatusCode expectedHttpStatusCode, bool preferRespondAsync = false) { - string bodyText = null; - if (oneDriveItem != null) - { - bodyText = JsonSerializer.Serialize(oneDriveItem); - } + var bodyText = oneDriveItem != null ? JsonSerializer.Serialize(oneDriveItem, oneDriveItem.GetType(), JSONOptions) : null; using (var response = await SendMessageReturnHttpResponse(bodyText, httpMethod, url, preferRespondAsync)) { From 6be3e15fe645f961c5fbfa8566eda3880581aa16 Mon Sep 17 00:00:00 2001 From: sean t Date: Thu, 14 Nov 2024 15:13:35 +0800 Subject: [PATCH 3/3] use enum --- Api/Entities/OneDriveCreateFolder.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Api/Entities/OneDriveCreateFolder.cs b/Api/Entities/OneDriveCreateFolder.cs index 4bf0e63..d445e49 100644 --- a/Api/Entities/OneDriveCreateFolder.cs +++ b/Api/Entities/OneDriveCreateFolder.cs @@ -1,4 +1,5 @@ -using System.Text.Json.Serialization; +using KoenZomers.OneDrive.Api.Enums; +using System.Text.Json.Serialization; namespace KoenZomers.OneDrive.Api.Entities { @@ -11,6 +12,6 @@ public class OneDriveCreateFolder : OneDriveItemBase public object Folder { get; set; } [JsonPropertyName("@microsoft.graph.conflictBehavior")] - public string ConflictBehavior { get; set; } = "rename"; + public NameConflictBehavior? NameConflictBehahiorAnnotation { get; set; } } }