22
schangxiang@126.com
2024-12-18 344680022c0c46a1bb108e7d2e81bf2296d79d15
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
using Admin.NET.Application.CommonHelper;
using Admin.NET.Application.Entity;
using Furion.DatabaseAccessor;
 
namespace Admin.NET.Application;
/// <summary>
/// pda服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.PdaUpdateGroup, Order = 100)]
public class PdaUpdateService : IDynamicApiController, ITransient
{
 
    public PdaUpdateService(
        )
    {
    }
 
    /// <summary>
    /// PDA程序更新文件地址
    /// </summary>
    /// <returns></returns>
    /// string
    [HttpGet]
    [ApiDescriptionSettings(Name = "GetUpdate")]
    [Description("PdaUpdate/GetUpdate")]
    [AllowAnonymous]
    public async Task<string> GetUpdate()
    {
        /** ly-0729*/
        var savePath = Path.Combine(App.HostEnvironment.ContentRootPath + "wwwroot\\", "PDAUpdate");
        if (!Directory.Exists(savePath))
        {
            throw Oops.Oh("文件夹不存在");
        }
        List<FileInfo> lstfiles = await GetFile(savePath, ".wgt");
        //Regex rxNonDigits = new Regex(@"[^\d]+");
        //long i = 0;
        FileInfo fileInfo = null;
 
        foreach (var item in lstfiles)
        {
            //string str = rxNonDigits.Replace(item.Name, "");
            //long j = Convert.ToInt64(rxNonDigits.Replace(item.Name, ""));
            //if (i < j)
            //{
            //    fileInfo = item;
            //    i = j;
            //}
            if (fileInfo == null)
            {
                fileInfo = item;
            }
            else
            {
                //比较两个文件的生成时间,取生成比较晚的
                if (item.CreationTimeUtc.Subtract(fileInfo.CreationTimeUtc).TotalSeconds > 0)
                {
                    fileInfo = item;
                }
            }
        }
        if (fileInfo == null)
        {
            throw Oops.Oh("没有要更新的文件");
        }
        return GetFilePreviewURL() + "PDAUpdate/" + fileInfo.Name;
 
    }
 
    /// <summary>
    /// 获取文件预览地址
    /// </summary>
    /// <returns></returns>
    private static string GetFilePreviewURL()
    {
        try
        {
            //App.Configuration.GetSection("IpRateLimiting")
            return App.Configuration["AppSettings:FilePreviewUrl"].ToString();
        }
        catch
        {
            return string.Empty;
        }
    }
 
    /// <summary>
    /// 获取文件夹下的指定后缀的所有文件
    /// </summary>
    /// <param name="path">文件路径</param>
    /// <param name="ExtName">文件后缀</param>
    /// <returns></returns>
    [NonAction]
    public async Task<List<FileInfo>> GetFile(string path, string ExtName)
    {
 
        try
        {
            List<FileInfo> lst = new List<FileInfo>();
            string[] dir = Directory.GetDirectories(path);// 文件夹列表
            DirectoryInfo directoryInfo = new DirectoryInfo(path);
            FileInfo[] files = directoryInfo.GetFiles();
            if (files.Length != 0 || dir.Length != 0) // 当前目录文件或文件夹不能为空
            {
                foreach (FileInfo f in files)
                {
                    if (ExtName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
                    {
                        lst.Add(f);
                    }
                }
                foreach (string d in dir)
                {
                    await GetFile(d, ExtName);
                }
            }
            return lst;
        }
        catch (Exception ex)
        {
            throw Oops.Oh(ex.Message);
        }
    }
 
 
}