From ce1f8c5b0e8cd5d4050e79c3e02433aafce81b24 Mon Sep 17 00:00:00 2001
From: huangxiaoqiang <huangxiaoqiang@hnkhzn.com>
Date: 星期二, 09 十二月 2025 16:16:22 +0800
Subject: [PATCH] 1
---
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 171 insertions(+), 0 deletions(-)
diff --git a/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs b/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs
new file mode 100644
index 0000000..633a59c
--- /dev/null
+++ b/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs
@@ -0,0 +1,171 @@
+锘縰sing Microsoft.AspNetCore.Mvc;
+using System.Text;
+
+namespace WIDESEA_WMSServer.Controllers.System
+{
+ [Route("api/[controller]")]
+ [ApiController]
+ public class Sys_LogController : Controller
+ {
+ [HttpPost, Route("GetLogName"), AllowAnonymous]
+ public WebResponseContent GetLogName()
+ {
+ WebResponseContent content = new WebResponseContent();
+ try
+ {
+ List<object> data = new List<object>();
+ DirectoryInfo folder = new DirectoryInfo(AppContext.BaseDirectory + "\\Logs\\");
+ DirectoryInfo[] firstDirectoryInfos = folder.GetDirectories().OrderByDescending(x => x.CreationTime).ToArray();
+ int k = 2020;
+ for (int i = 0; i < firstDirectoryInfos.Length; i++)
+ {
+ if (firstDirectoryInfos[i].Name != "Info")
+ {
+ FileInfo[] nextFileInfos = firstDirectoryInfos[i].GetFiles();
+ List<object> values = new List<object>();
+ for (int j = 0; j < nextFileInfos.Length; j++)
+ {
+ values.Add(new { label = nextFileInfos[j].Name, id = k, hidden = true, fatherNode = firstDirectoryInfos[i].Name });
+ k++;
+ }
+ data.Add(new { label = firstDirectoryInfos[i].Name, children = values, id = i, hidden = false });
+ }
+ }
+
+ return WebResponseContent.Instance.OK(data: data);
+ }
+ catch (Exception ex)
+ {
+ return WebResponseContent.Instance.Error(ex.Message);
+ }
+ }
+
+ [HttpPost, Route("GetLog"), AllowAnonymous]
+ public WebResponseContent GetLog(string fileName)
+ {
+ WebResponseContent content = new WebResponseContent();
+ try
+ {
+ List<FileInfo> files = new List<FileInfo>();
+ DirectoryInfo folder = new DirectoryInfo(AppContext.BaseDirectory + "\\Logs\\");
+ DirectoryInfo[] firstDirectoryInfos = folder.GetDirectories();
+ for (int i = 0; i < firstDirectoryInfos.Length; i++)
+ {
+ FileInfo[] nextFileInfos = firstDirectoryInfos[i].GetFiles();
+ files.AddRange(nextFileInfos);
+ }
+ if (files.Count > 0)
+ {
+ FileInfo file = files.Where(x => x.Name == fileName).FirstOrDefault();
+ using StreamReader stream = new StreamReader(file.FullName);
+ StringBuilder text = new StringBuilder();
+ List<string> lines = new List<string>();
+ int i = 0;
+ while (stream.Peek() >= 0)
+ {
+ var line = stream.ReadLine();
+ lines.Add(line);
+ }
+
+ content = WebResponseContent.Instance.OK(data: lines);
+ }
+ else
+ {
+ content = WebResponseContent.Instance.Error($"鏈壘鍒版棩蹇楁枃浠�,銆恵fileName}銆�");
+ }
+ }
+ catch (Exception ex)
+ {
+ content = WebResponseContent.Instance.Error($"鎵撳紑鏃ュ織鏂囦欢閿欒,{ex.Message}");
+ }
+ return content;
+ }
+ [HttpPost, HttpGet, Route("DownLoadLog"), AllowAnonymous]
+ public virtual ActionResult DownLoadLog(string fileName)
+ {
+ try
+ {
+ // 1. 鍙傛暟楠岃瘉
+ if (string.IsNullOrWhiteSpace(fileName))
+ {
+ return BadRequest("鏂囦欢鍚嶄笉鑳戒负绌�");
+ }
+
+ string logDirectory = Path.Combine(AppContext.BaseDirectory, "Logs");
+
+ if (!Directory.Exists(logDirectory))
+ {
+ Directory.CreateDirectory(logDirectory);
+ }
+
+ string filePath = Path.Combine(logDirectory, fileName);
+
+ if (Directory.Exists(filePath))
+ {
+ return NotFound($"鏂囦欢 {fileName} 涓嶅瓨鍦�");
+ }
+
+ string extension = Path.GetExtension(fileName).ToLowerInvariant();
+ if (!IsAllowedFileType(extension))
+ {
+ return BadRequest($"涓嶆敮鎸佺殑鏂囦欢绫诲瀷: {extension}");
+ }
+
+ FileInfo fileInfo = new FileInfo(filePath);
+
+ if (fileInfo.Length > 50 * 1024 * 1024) // 50MB闄愬埗
+ {
+ return BadRequest("鏂囦欢杩囧ぇ锛屾棤娉曚笅杞�");
+ }
+
+ byte[] fileBytes;
+ using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
+ {
+ using (MemoryStream memoryStream = new MemoryStream())
+ {
+ fileStream.CopyTo(memoryStream);
+ fileBytes = memoryStream.ToArray();
+ }
+ }
+
+ string contentType = GetContentType(extension);
+
+ return File(fileBytes, contentType, fileName);
+ }
+ catch (UnauthorizedAccessException)
+ {
+ return StatusCode(403, "娌℃湁璁块棶璇ユ枃浠剁殑鏉冮檺");
+ }
+ catch (PathTooLongException)
+ {
+ return BadRequest("鏂囦欢璺緞杩囬暱");
+ }
+ catch (IOException ex)
+ {
+ return StatusCode(500, "鏂囦欢璇诲彇澶辫触锛屽彲鑳芥鍦ㄨ鍏朵粬杩涚▼浣跨敤");
+ }
+ catch (Exception ex)
+ {
+ return StatusCode(500, $"鏈嶅姟鍣ㄥ唴閮ㄩ敊璇�: {ex.Message}");
+ }
+ }
+ private bool IsAllowedFileType(string extension)
+ {
+ var allowedTypes = new[] { ".txt", ".log", ".csv", ".json", ".xml" };
+ return allowedTypes.Contains(extension);
+ }
+
+ private string GetContentType(string extension)
+ {
+ return extension.ToLowerInvariant() switch
+ {
+ ".txt" => "text/plain; charset=utf-8",
+ ".log" => "text/plain; charset=utf-8",
+ ".csv" => "text/csv; charset=utf-8",
+ ".json" => "application/json; charset=utf-8",
+ ".xml" => "application/xml; charset=utf-8",
+ _ => "application/octet-stream"
+ };
+ }
+ }
+}
--
Gitblit v1.9.3