From 8543afd2c70277c68fe4f1c7f13845150ef8bb13 Mon Sep 17 00:00:00 2001 From: dlaren Date: Mon, 14 Jul 2025 14:14:35 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91=20?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6=EF=BC=8C=E5=B0=86=E5=AD=A6?= =?UTF-8?q?=E7=94=9F=E7=AB=AF=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6=E5=8A=A8?= =?UTF-8?q?=E4=BD=9C=E7=A7=BB=E5=8A=A8=E8=87=B3=E5=90=8E=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Applications/Program.cs | 105 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 98 insertions(+), 7 deletions(-) diff --git a/Applications/Program.cs b/Applications/Program.cs index 2b274e8..aa02c36 100644 --- a/Applications/Program.cs +++ b/Applications/Program.cs @@ -3,6 +3,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.IO.Compression; using System.Net; using System.Net.NetworkInformation; using System.Security.Cryptography; @@ -51,10 +52,59 @@ class Program string openType = context.Request.QueryString["type"]; string exePath = context.Request.QueryString["exepath"]; string workingDirectory = context.Request.QueryString["workingdirectory"]; - + // 生成文件夹 + string folderPath = context.Request.QueryString["folderPath"]; + // 文件URL + string filesurl = context.Request.QueryString["filesurl"]; + // 文件名称 + string desiredFileName = context.Request.QueryString["filename"]; string ip = context.Request.QueryString["ip"]; string action = context.Request.Url.AbsolutePath.ToLower(); string responseMessage = ""; + if (action == "/downloadfiles") + { + try + { + string downloadedFilePath = await DownloadFileAsync(filesurl, folderPath); + Console.WriteLine($"文件已下载到: {downloadedFilePath}"); + + if (IsZipFile(downloadedFilePath)) + { + Console.WriteLine("检测到ZIP文件,开始解压..."); + string extractPath = Path.Combine(folderPath, Path.GetFileNameWithoutExtension(downloadedFilePath)); + ZipFile.ExtractToDirectory(downloadedFilePath, folderPath); + Console.WriteLine($"解压完成,文件保存在: {extractPath}"); + + // 删除原ZIP文件 + File.Delete(downloadedFilePath); + Console.WriteLine("已删除原ZIP文件"); + } + else + { + Console.WriteLine("文件不是ZIP格式,保留原文件"); + // 获取原文件扩展名 + string originalExtension = Path.GetExtension(downloadedFilePath); + + // 构建新文件名(保留原扩展名) + string newFileName = $"{desiredFileName}{originalExtension}"; + string newFilePath = Path.Combine(folderPath, newFileName); + + // 如果目标文件已存在,先删除 + if (File.Exists(newFilePath)) + { + File.Delete(newFilePath); + } + + File.Move(downloadedFilePath, newFilePath); + Console.WriteLine($"文件已重命名为: {newFilePath}"); + + } + } + catch (Exception ex) + { + Console.WriteLine($"操作失败: {ex.Message}"); + } + } if (action == "/openjudgement") { string relativePath = ""; @@ -196,13 +246,7 @@ class Program { Console.WriteLine($"❌ 出错了:{ex.Message}"); } - - - - - } - try { string? resolvedPath = ResolveAppPath(path); @@ -542,6 +586,53 @@ class Program return false; // 端口可用 } + static async Task DownloadFileAsync(string url, string saveDir) + { + using (var httpClient = new HttpClient()) + { + // 确保目录存在 + Directory.CreateDirectory(saveDir); + + // 从URL获取文件名 + Uri uri = new Uri(url); + string fileName = Path.GetFileName(uri.LocalPath); + if (string.IsNullOrEmpty(fileName)) + { + fileName = $"downloaded_{DateTime.Now:yyyyMMddHHmmss}"; + } + + string fullPath = Path.Combine(saveDir, fileName); + + // 下载文件 + var response = await httpClient.GetAsync(url); + response.EnsureSuccessStatusCode(); + using (var fileStream = File.Create(fullPath)) + { + await response.Content.CopyToAsync(fileStream); + } + + return fullPath; + } + } + // 检查文件是否是ZIP格式 + static bool IsZipFile(string filePath) + { + try + { + if (filePath.Contains("zip")) + { + return true; + } else + { + return false; + } + } + catch + { + return false; + } + } + // 获取当前登录用户 SID static string GetCurrentUserSid() {