static async Task <bool> UploadAssetFileViaSas(string assetFilePath, WebDocuSettings settings) { var httpClient = new HttpClient(); using (var assetStream = File.OpenRead(assetFilePath)) { var fileName = NormalizeFilename(assetFilePath); var sasRequest = new HttpRequestMessage(HttpMethod.Post, settings.DocuBaseUrl.TrimEnd('/') + $"/API/ProjectAssets/SASUpload?apiKey={HttpUtility.UrlEncode(settings.DocuApiKey)}&version={HttpUtility.UrlEncode(settings.Version)}"); var requestContent = new StringContent(JsonConvert.SerializeObject(new { FileName = fileName, FileSizeInBytes = assetStream.Length }), Encoding.UTF8, "application/json"); sasRequest.Content = requestContent; var sasResponse = await httpClient.SendAsync(sasRequest); if (!sasResponse.IsSuccessStatusCode) { return(false); } var sasResponseJson = JObject.Parse(await sasResponse.Content.ReadAsStringAsync()); var sasLink = sasResponseJson["UploadLink"]?.ToString() ?? sasResponseJson["uploadLink"]?.ToString(); if (string.IsNullOrWhiteSpace(sasLink)) { return(false); } var sasBlobClient = new BlobClient(new Uri(sasLink)); var uploadResponse = await sasBlobClient.UploadAsync(assetStream); return(uploadResponse.GetRawResponse().Status >= 200 && uploadResponse.GetRawResponse().Status <= 299); } }
static async Task UploadAssetFile(string assetFilePath, WebDocuSettings settings) { Logger.Normal($"Uploading asset {assetFilePath}"); if (!await UploadAssetFileViaSas(assetFilePath, settings)) { await UploadAssetFileDirectly(assetFilePath, settings); } else { Logger.Normal("File was uploaded via direct SAS upload to Azure Blob Storage"); } }
static async Task UploadAssetFileDirectly(string assetFilePath, WebDocuSettings settings) { using (var assetStream = File.OpenRead(assetFilePath)) { var fileName = NormalizeFilename(assetFilePath); var request = new HttpRequestMessage(HttpMethod.Post, settings.DocuBaseUrl.TrimEnd('/') + "/API/ProjectAssets/Upload"); var requestContent = new MultipartFormDataContent(); requestContent.Add(new StringContent(settings.DocuApiKey), "ApiKey"); requestContent.Add(new StringContent(settings.Version), "Version"); requestContent.Add(new StreamContent(assetStream), "AssetFile", fileName); request.Content = requestContent; var response = await new HttpClient().SendAsync(request); if (!response.IsSuccessStatusCode) { throw new Exception("Upload failed with status code: " + response.StatusCode + Environment.NewLine + await response.Content.ReadAsStringAsync()); } } }
static async Task UploadToDanglDocu(string zipPackage, WebDocuSettings settings) { using (var docsStream = File.OpenRead(zipPackage)) { var request = new HttpRequestMessage(HttpMethod.Post, settings.DocuBaseUrl.TrimEnd('/') + "/API/Projects/Upload"); var requestContent = new MultipartFormDataContent(); requestContent.Add(new StringContent(settings.DocuApiKey), "ApiKey"); requestContent.Add(new StringContent(settings.Version), "Version"); if (!string.IsNullOrWhiteSpace(settings.MarkdownChangelog)) { requestContent.Add(new StringContent(settings.MarkdownChangelog), "MarkdownChangelog"); } requestContent.Add(new StreamContent(docsStream), "ProjectPackage", "docs.zip"); request.Content = requestContent; var response = await new HttpClient().SendAsync(request); if (!response.IsSuccessStatusCode) { if (response.StatusCode == System.Net.HttpStatusCode.Conflict && settings.SkipForVersionConflicts) { Logger.Normal($"WebDocu return Http status 409 - Conflict. This means that there is likely already an existing" + $" combination of version and project present. The settings are enabled to skip this." + $" Asserts will also not be uploaded."); return; } throw new Exception("Upload failed with status code: " + response.StatusCode + Environment.NewLine + await response.Content.ReadAsStringAsync()); } } if (settings.AssetFilePaths?.Any() == true) { foreach (var assetFilePath in settings.AssetFilePaths) { await UploadAssetFile(assetFilePath, settings); } } }
private static WebDocuSettings CloneAndModify(this WebDocuSettings toolSettings, Action <WebDocuSettings> modifyAction) { toolSettings = toolSettings.NewInstance(); modifyAction(toolSettings); return(toolSettings); }
public static WebDocuSettings SetAssetFilePaths(this WebDocuSettings toolSettings, string[] assetFilePaths) => toolSettings.CloneAndModify(s => s.AssetFilePaths = assetFilePaths);
public static WebDocuSettings SetSkipForVersionConflicts(this WebDocuSettings toolSettings, bool skipForVersionConflicts) => toolSettings.CloneAndModify(s => s.SkipForVersionConflicts = skipForVersionConflicts);
public static WebDocuSettings SetMarkdownChangelog(this WebDocuSettings toolSettings, string markdownChangelog) => toolSettings.CloneAndModify(s => s.MarkdownChangelog = markdownChangelog);
public static WebDocuSettings SetVersion(this WebDocuSettings toolSettings, string version) => toolSettings.CloneAndModify(s => s.Version = version);
public static WebDocuSettings SetDocuBaseUrl(this WebDocuSettings toolSettings, string docuBaseUrl) => toolSettings.CloneAndModify(s => s.DocuBaseUrl = docuBaseUrl);
public static WebDocuSettings SetDocuApiKey(this WebDocuSettings toolSettings, string docuApiKey) => toolSettings.CloneAndModify(s => s.DocuApiKey = docuApiKey);
public static WebDocuSettings SetSourceDirectory(this WebDocuSettings toolSettings, string sourceDirectory) => toolSettings.CloneAndModify(s => s.SourceDirectory = sourceDirectory);