huanghongfeng
2024-12-08 05c2e8aadb518a6323ff9fe23051fdbd56097221
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Model.Models;
 
namespace WIDESEAWCS_ISystemServices
{
    #region 实体类
    public class DirInfo
    {
        /// <summary>
        /// 当前路径
        /// </summary>
        public string dirPath { get; set; }
 
        /// <summary>
        /// 当前名称
        /// </summary>
        public string dirName { get; set; }
 
        /// <summary>
        /// 子文件
        /// </summary>
        public List<FileDataInfo> files { get; set; }
 
        /// <summary>
        /// 子文件夹
        /// </summary>
        public List<DirInfo> dirs { get; set; }
    }
 
    public class FileDataInfo
    {
        public string filePath { get; set; }
 
        public string fileName { get; set; }
 
        public long fileSize { get; set; }
    }
 
    public class GetLogParm
    {
        /// <summary>
        /// 文件地址
        /// </summary>
        public string path;
 
        /// <summary>
        /// 读取进度
        /// </summary>
        public float percent;
 
        /// <summary>
        /// 最大读取大小
        /// </summary>
        public double maxsize_KB = 20;
 
        /// <summary>
        /// 当前起始位置
        /// </summary>
        public long topStartPos = 0;
    }
    #endregion
    public interface ISys_LogService : IService<Sys_Log>
    {
        /// <summary>
        /// 递归获取文件信息
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        private DirInfo GetDirInfo(string dirPath)
        {
            //当前文件夹
            var dirInfo = new DirInfo();
            dirInfo.dirName = Path.GetFileName(dirPath);
 
            //子文件
            List<FileDataInfo> files = new List<FileDataInfo>();
            foreach (var file in Directory.GetFiles(dirPath))
            {
                files.Add(new FileDataInfo()
                {
                    filePath = file,
                    fileName = Path.GetFileName(file)
                });
            }
 
            //子文件夹
            var dirs = Directory.GetDirectories(dirPath);
            dirInfo.dirs = new List<DirInfo>();
            foreach (var dir in dirs)
            {
                dirInfo.dirs.Add(GetDirInfo(dir));
            }
 
            //子文件夹,与子目录合并
            foreach (var file in files)
            {
                dirInfo.dirs.Add(new DirInfo() { dirPath = file.filePath, dirName = file.fileName });
            }
            return dirInfo;
        }
 
        /// <summary>
        /// 获取日志文件列表
        /// </summary>
        /// <returns></returns>
        public WebResponseContent GetLogList()
        {
            WebResponseContent content = new WebResponseContent();
            string path = System.Environment.CurrentDirectory + "/log";
            if (!System.IO.Directory.Exists(path))
            {
                return content.Error("暂无日志文件");
            }
 
            content.Data = GetDirInfo(path);
            content.OK();
            return content;
        }
 
        /// <summary>
        /// 获取文件内容
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public WebResponseContent GetLogData(GetLogParm parm)
        {
            WebResponseContent content = new WebResponseContent();
            string res = "";
            //是否读取到最后
            bool isEnd = false;
            long startIndex = 0;
            //文件大小
            long len = 0;
            try
            {
                if (!System.IO.File.Exists(parm.path))
                {
                    throw new Exception($"文件{parm.path}不存在!");
                }
                using (FileStream fs = new FileStream(parm.path, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    //最大读取大小
                    int maxsize = (int)(1024 * parm.maxsize_KB);
                    len = fs.Length;
                    long startPos = (long)(len * (parm.percent / 100));//起始位置
                    long readLen = len - startPos;//读取长度
 
                    //向前加载
                    if (parm.topStartPos != 0)
                    {
                        startPos = parm.topStartPos - maxsize;
                        if (startPos < 0)
                        {
                            //已读到起始位
                            startPos = 0;
                            readLen = parm.topStartPos;
                        }
                        else
                        {
                            readLen = maxsize;
                        }
                    }
                    else
                    {
                        //读取大小是否超出最大长度
                        if (readLen > maxsize)
                        {
                            readLen = maxsize;
                        }
                        else
                        {
                            isEnd = true;
                        }
                    }
 
                    //加载100%,按最大内容读取
                    if (parm.percent == 100)
                    {
                        if (len < maxsize)
                        {
                            startPos = 0;
                            readLen = len;
                        }
                        else
                        {
                            startPos = len - maxsize;
                            readLen = maxsize;
                        }
                    }
 
                    fs.Seek(startPos, SeekOrigin.Begin);
                    var buffer = new byte[readLen];
                    fs.Read(buffer, 0, (int)readLen);
 
                    startIndex = startPos;
                    if (startPos != 0 && (parm.percent != 0 || parm.topStartPos != 0))
                    {
                        //不是从头加载,删除可能不完整的第一行
                        int skipCount = 0;
                        for (int i = 0; i < buffer.Length; i++)
                        {
                            if (buffer[i] == 10)
                            {
                                skipCount = i;
                                break;
                            }
                        }
                        if (skipCount != 0)
                        {
                            //去掉换行
                            skipCount++;
                            //下次读取前延
                            startIndex += skipCount;
                        }
                        res = Encoding.UTF8.GetString(buffer.Skip(skipCount).ToArray());
                    }
                    else
                    {
                        res = Encoding.UTF8.GetString(buffer);
                    }
                }
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
            return content.OK(data: new { content = res, isEnd, startIndex, len });
        }
 
    }
}