From e483ac11616ffc9260d8f491fcc0d66f480b5443 Mon Sep 17 00:00:00 2001
From: huangxiaoqiang <huangxiaoqiang@hnkhzn.com>
Date: 星期四, 16 四月 2026 10:12:52 +0800
Subject: [PATCH] Merge branch 'master' of http://115.159.85.185:8098/r/LongTu/FangGangAGV

---
 Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs |  283 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 280 insertions(+), 3 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
index 0e24549..aabc587 100644
--- 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
@@ -1,23 +1,26 @@
 锘縰sing Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
+using System.Text;
 using WIDESEAWCS_Core;
 using WIDESEAWCS_ISystemServices;
 using WIDESEAWCS_Model.Models;
 
-namespace WIDESEAWCS_Server.Controllers.System
+namespace WIDESEAWMSServer.Controllers
 {
     [Route("api/Sys_Log")]
     [ApiController]
     public class Sys_LogController : ApiBaseController<ISys_LogService, Sys_Log>
     {
+        private const int MAX_FILE_SIZE_MB = 50;
+        private const int MAX_RETRY_COUNT = 3;
+        private const int RETRY_DELAY_MS = 100;
+        private static readonly string[] ALLOWED_FILE_TYPES = { ".txt", ".log", ".csv", ".json", ".xml" };
         private readonly IHttpContextAccessor _httpContextAccessor;
         public Sys_LogController(ISys_LogService service, IHttpContextAccessor httpContextAccessor) : base(service)
         {
             _httpContextAccessor = httpContextAccessor;
         }
-
-
 
         [HttpPost, Route("GetLogList"), AllowAnonymous]
         public WebResponseContent GetLogList()
@@ -30,6 +33,280 @@
         {
             return Service.GetLogData(parm);
         }
+
+        [HttpPost, Route("GetLogName"), AllowAnonymous]
+        public WebResponseContent GetLogName()
+        {
+            return Service.GetLogName();
+        }
+
+        [HttpPost, Route("GetLog"), AllowAnonymous]
+        public WebResponseContent GetLog(string fileName)
+        {
+            return Service.GetLog(fileName);
+        }
+
+        [HttpPost, HttpGet, Route("DownLoadLog"), AllowAnonymous]
+        public virtual async Task<ActionResult> DownLoadLog(string fileName)
+        {
+            try
+            {
+                // 1. 鍙傛暟楠岃瘉
+                if (string.IsNullOrWhiteSpace(fileName))
+                {
+                    return BadRequest("鏂囦欢鍚嶄笉鑳戒负绌�");
+                }
+
+                // 瀹夊叏鎬ф鏌ワ細闃叉璺緞閬嶅巻鏀诲嚮
+                if (fileName.Contains("..") || Path.IsPathRooted(fileName))
+                {
+                    return BadRequest("鏃犳晥鐨勬枃浠跺悕");
+                }
+
+                //string logDirectory = Path.Combine(AppContext.BaseDirectory, "logs");
+                string logDirectory = Path.Combine(AppContext.BaseDirectory);
+
+                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 > MAX_FILE_SIZE_MB * 1024 * 1024)
+                {
+                    return BadRequest($"鏂囦欢杩囧ぇ锛岃秴杩噞MAX_FILE_SIZE_MB}MB闄愬埗");
+                }
+
+                // 鏂规1锛氫娇鐢ㄩ噸璇曟満鍒� + 鍏变韩璇诲彇锛堟帹鑽愶級
+                byte[] fileBytes = await ReadFileWithRetryAsync(filePath);
+
+                if (fileBytes == null)
+                {
+                    return StatusCode(500, "鏂囦欢琚崰鐢紝鏃犳硶涓嬭浇锛岃绋嶅悗閲嶈瘯");
+                }
+
+                string contentType = GetContentType(extension);
+
+                // 璁剧疆涓嬭浇澶�
+                Response.Headers.Add("Content-Disposition", $"attachment; filename=\"{System.Web.HttpUtility.UrlEncode(fileName, Encoding.UTF8)}\"");
+                Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
+                Response.Headers.Add("Pragma", "no-cache");
+                Response.Headers.Add("Expires", "0");
+
+                return File(fileBytes, contentType, fileName);
+            }
+            catch (UnauthorizedAccessException)
+            {
+                return StatusCode(403, "娌℃湁璁块棶璇ユ枃浠剁殑鏉冮檺");
+            }
+            catch (PathTooLongException)
+            {
+                return BadRequest("鏂囦欢璺緞杩囬暱");
+            }
+            catch (IOException ex)
+            {
+                if (IsFileLockedException(ex))
+                {
+                    return StatusCode(500, "鏂囦欢琚攣瀹氾紝鍙兘姝e湪琚郴缁熷啓鍏ワ紝璇风◢鍚庡啀璇�");
+                }
+                return StatusCode(500, $"鏂囦欢璇诲彇澶辫触: {ex.Message}");
+            }
+            catch (Exception ex)
+            {
+                // 璁板綍寮傚父鏃ュ織锛堣繖閲岀畝鍖栦负杩斿洖锛屽疄闄呴」鐩腑搴旇璁板綍鍒版棩蹇楃郴缁燂級
+                return StatusCode(500, $"鏈嶅姟鍣ㄥ唴閮ㄩ敊璇�: {ex.Message}");
+            }
+        }
+        
+        /// <summary>
+        /// 甯﹂噸璇曟満鍒剁殑鏂囦欢璇诲彇鏂规硶
+        /// </summary>
+        private async Task<byte[]> ReadFileWithRetryAsync(string filePath)
+        {
+            for (int attempt = 0; attempt < MAX_RETRY_COUNT; attempt++)
+            {
+                try
+                {
+                    // 浣跨敤 FileShare.ReadWrite 鍏佽鍏朵粬杩涚▼鍚屾椂璇诲彇鍜屽啓鍏�
+                    // 浣跨敤寮傛璇诲彇鎻愰珮鎬ц兘
+                    using (var fileStream = new FileStream(
+                        filePath,
+                        FileMode.Open,
+                        FileAccess.Read,
+                        FileShare.ReadWrite | FileShare.Delete, // 鍏佽鍒犻櫎鎿嶄綔
+                        bufferSize: 4096,
+                        useAsync: true))
+                    {
+                        using (var memoryStream = new MemoryStream())
+                        {
+                            await fileStream.CopyToAsync(memoryStream);
+                            return memoryStream.ToArray();
+                        }
+                    }
+                }
+                catch (IOException) when (attempt < MAX_RETRY_COUNT - 1)
+                {
+                    // 濡傛灉涓嶆槸鏈�鍚庝竴娆¢噸璇曪紝绛夊緟涓�娈垫椂闂�
+                    await Task.Delay(RETRY_DELAY_MS * (attempt + 1));
+                }
+                catch (IOException ex)
+                {
+                    // 鏈�鍚庝竴娆″皾璇曚篃澶辫触浜�
+                    throw;
+                }
+            }
+            return null;
+        }
+
+        /// <summary>
+        /// 鍒ゆ柇鏄惁涓烘枃浠惰閿佸畾鐨勫紓甯�
+        /// </summary>
+        private bool IsFileLockedException(IOException ex)
+        {
+            int errorCode = ex.HResult & 0xFFFF;
+            return errorCode == 32 || errorCode == 33; // ERROR_SHARING_VIOLATION or ERROR_LOCK_VIOLATION
+        }
+
+        /// <summary>
+        /// 妫�鏌ユ枃浠剁被鍨嬫槸鍚﹀厑璁�
+        /// </summary>
+        private bool IsAllowedFileType(string extension)
+        {
+            return ALLOWED_FILE_TYPES.Contains(extension);
+        }
+
+        /// <summary>
+        /// 鑾峰彇Content-Type
+        /// </summary>
+        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"
+            };
+        }
+
+        /// <summary>
+        /// 澶囬�夋柟妗堬細鍒涘缓涓存椂鍓湰涓嬭浇锛堟渶瀹夊叏锛屼絾鎬ц兘绋嶅樊锛�
+        /// </summary>
+        [HttpPost, HttpGet, Route("DownLoadLogCopy"), AllowAnonymous]
+        public virtual async Task<ActionResult> DownLoadLogCopy(string fileName)
+        {
+            try
+            {
+                // 鍙傛暟楠岃瘉锛堝悓涓婏級
+                if (string.IsNullOrWhiteSpace(fileName))
+                {
+                    return BadRequest("鏂囦欢鍚嶄笉鑳戒负绌�");
+                }
+
+                string logDirectory = Path.Combine(AppContext.BaseDirectory, "logs");
+                string filePath = Path.Combine(logDirectory, fileName);
+
+                if (Directory.Exists(filePath))
+                {
+                    return NotFound($"鏂囦欢 {fileName} 涓嶅瓨鍦�");
+                }
+
+                // 鐢熸垚涓存椂鏂囦欢鍚�
+                string tempFileName = $"{Path.GetFileNameWithoutExtension(fileName)}_{Guid.NewGuid():N}{Path.GetExtension(fileName)}";
+                string tempFilePath = Path.Combine(Path.GetTempPath(), tempFileName);
+
+                try
+                {
+                    // 灏濊瘯澶嶅埗鏂囦欢鍒颁复鏃朵綅缃紙浣跨敤閲嶈瘯鏈哄埗锛�
+                    bool copySuccess = false;
+                    for (int attempt = 0; attempt < MAX_RETRY_COUNT; attempt++)
+                    {
+                        try
+                        {
+                            //Directory.GetFiles.Copy(filePath, tempFilePath, false);
+                            copySuccess = true;
+                            break;
+                        }
+                        catch (IOException) when (attempt < MAX_RETRY_COUNT - 1)
+                        {
+                            await Task.Delay(RETRY_DELAY_MS * (attempt + 1));
+                        }
+                    }
+
+                    if (!copySuccess)
+                    {
+                        return StatusCode(500, "鏃犳硶澶嶅埗鏂囦欢锛屽彲鑳借鍏朵粬杩涚▼鍗犵敤");
+                    }
+
+                    // 浠庝复鏃舵枃浠惰鍙�
+                    byte[] fileBytes;
+                    using (FileStream tempStream = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
+                    {
+                        using (MemoryStream memoryStream = new MemoryStream())
+                        {
+                            await tempStream.CopyToAsync(memoryStream);
+                            fileBytes = memoryStream.ToArray();
+                        }
+                    }
+
+                    string extension = Path.GetExtension(fileName).ToLowerInvariant();
+                    string contentType = GetContentType(extension);
+
+                    // 杩斿洖鏂囦欢鍚庢竻鐞嗕复鏃舵枃浠�
+                    var result = File(fileBytes, contentType, fileName);
+
+                    // 寮傛娓呯悊涓存椂鏂囦欢
+                    _ = Task.Run(() =>
+                    {
+                        try
+                        {
+                            Directory.Delete(tempFilePath);
+                        }
+                        catch
+                        {
+                            // 蹇界暐鍒犻櫎澶辫触
+                        }
+                    });
+
+                    return result;
+                }
+                finally
+                {
+                    // 纭繚涓存椂鏂囦欢琚竻鐞�
+                    if (Directory.Exists(tempFilePath))
+                    {
+                        try
+                        {
+                            Directory.Delete(tempFilePath);
+                        }
+                        catch
+                        {
+                            // 蹇界暐鍒犻櫎澶辫触
+                        }
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                return StatusCode(500, $"鏈嶅姟鍣ㄥ唴閮ㄩ敊璇�: {ex.Message}");
+            }
+        }
     }
    
 }

--
Gitblit v1.9.3