| | |
| | | using Microsoft.AspNetCore.Http; |
| | | using Microsoft.AspNetCore.Authorization; |
| | | using Microsoft.AspNetCore.Http; |
| | | using Microsoft.AspNetCore.Mvc; |
| | | using System.Text; |
| | | using WIDESEA_Core; |
| | | using WIDESEA_Core.BaseController; |
| | | using WIDESEA_ISystemService; |
| | | using WIDESEA_Model.Models; |
| | |
| | | public Sys_LogController(ISys_LogService service) : base(service) |
| | | { |
| | | } |
| | | [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 }); |
| | | } |
| | | } |
| | | |
| | | FileInfo[] nextFileInfo = folder.GetFiles(); |
| | | List<object> value = new List<object>(); |
| | | for (int j = 0; j < nextFileInfo.Length; j++) |
| | | { |
| | | value.Add(new { label = nextFileInfo[j].Name, id = k, hidden = true, fatherNode = folder.Name }); |
| | | k++; |
| | | } |
| | | data.Add(new { label = folder.Name, children = value, id = 1, 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); |
| | | } |
| | | |
| | | FileInfo[] nextFileInfo = folder.GetFiles(); |
| | | files.AddRange(nextFileInfo); |
| | | 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"); |
| | | 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 > 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" |
| | | }; |
| | | } |
| | | } |
| | | } |