【新增】 下载文件,将学生端下载文件动作移动至后台

This commit is contained in:
dlaren
2025-07-14 14:14:35 +08:00
parent 3783a98b92
commit 8543afd2c7

View File

@@ -3,6 +3,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.IO.Compression;
using System.Net; using System.Net;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Security.Cryptography; using System.Security.Cryptography;
@@ -51,10 +52,59 @@ class Program
string openType = context.Request.QueryString["type"]; string openType = context.Request.QueryString["type"];
string exePath = context.Request.QueryString["exepath"]; string exePath = context.Request.QueryString["exepath"];
string workingDirectory = context.Request.QueryString["workingdirectory"]; 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 ip = context.Request.QueryString["ip"];
string action = context.Request.Url.AbsolutePath.ToLower(); string action = context.Request.Url.AbsolutePath.ToLower();
string responseMessage = ""; 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") if (action == "/openjudgement")
{ {
string relativePath = ""; string relativePath = "";
@@ -196,13 +246,7 @@ class Program
{ {
Console.WriteLine($"❌ 出错了:{ex.Message}"); Console.WriteLine($"❌ 出错了:{ex.Message}");
} }
} }
try try
{ {
string? resolvedPath = ResolveAppPath(path); string? resolvedPath = ResolveAppPath(path);
@@ -542,6 +586,53 @@ class Program
return false; // 端口可用 return false; // 端口可用
} }
static async Task<string> 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 // 获取当前登录用户 SID
static string GetCurrentUserSid() static string GetCurrentUserSid()
{ {