/* * Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved. * * This file is part of System Informer. * * Authors: * * dmex * */ namespace CustomBuildTool { /// /// https://sourceforge.net/p/forge/documentation/Using%20the%20Release%20API/ /// public static class BuildSourceForge { /// /// Provides a static instance of the HTTP client used for communicating with SourceForge services. /// /// This client is intended for reuse to optimize resource usage and performance when /// making multiple HTTP requests to SourceForge. Avoid disposing of this instance directly. private static readonly HttpClient SourceForgeHttpClient; private static readonly string BaseUrl; private static readonly string BaseToken; /// /// Initializes static resources required for SourceForge API communication. /// /// This static constructor sets up the HTTP client and retrieves environment variables /// necessary for SourceForge integration. It is automatically invoked before any static members are /// accessed. static BuildSourceForge() { SourceForgeHttpClient = BuildHttpClient.CreateHttpClient(); BaseUrl = Win32.GetEnvironmentVariable("SOURCEFORGE_BASE_KEY"); BaseToken = Win32.GetEnvironmentVariable("SOURCEFORGE_BASE_URL"); } /// /// Uploads a release file to SourceForge and returns the upload response. /// /// The upload operation requires valid SourceForge API credentials and configuration. If /// the upload fails due to missing configuration or an exception, the method returns . /// The path to the local file to upload. Cannot be null or empty. /// The name to assign to the file for download on SourceForge. Cannot be null or empty. /// The label to display on the download button for the uploaded file. /// Indicates whether to delay the release by 72 hours before making it available for download. Set to to delay; otherwise, . /// A containing the result of the upload operation, or if the upload fails or required configuration is missing. public static async Task UploadReleaseFile( string FileName, string DownloadFileName, string DownloadButtonName, bool Delay72Hours ) { if (string.IsNullOrWhiteSpace(BaseUrl)) return null; if (string.IsNullOrWhiteSpace(BaseToken)) return null; try { await using FileStream fileStream = File.OpenRead(FileName); await using BufferedStream bufferedStream = new BufferedStream(fileStream); using HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, $"{BaseUrl}/{DownloadFileName}"); requestMessage.Headers.Add("api_key", BaseToken); //requestMessage.Headers.Add("default", "windows&default=mac&default=linux&default=bsd&default=solaris&default=others&download_label=downloadlabel&name=name"); requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); requestMessage.Content = new StreamContent(bufferedStream); requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); var response = await BuildHttpClient.SendMessage(SourceForgeHttpClient, requestMessage, SourceForgeUploadResponseContext.Default.SourceForgeUploadResponse); return response; } catch (Exception ex) { Program.PrintColorMessage($"[BuildSourceForge] {ex}", ConsoleColor.Red); } return null; } } }