节点在线、应用在线、配置在线使用令牌查询
大石头 authored at 2021-12-16 19:49:30
4.39 KiB
Stardust
using Microsoft.AspNetCore.Mvc;
using NewLife;
using NewLife.Cube;
using NewLife.Cube.ViewModels;
using NewLife.Log;
using NewLife.Web;
using Stardust.Data.Nodes;
using Stardust.Storages;
using Stardust.Data.Platform;
using XCode.Membership;
using Attachment = NewLife.Cube.Entity.Attachment;

namespace Stardust.Web.Areas.Nodes.Controllers;

/// <summary>节点版本。追踪各组件在节点上的版本信息,管理组件升级包上传与分发</summary>
[Menu(40)]
[NodesArea]
public class NodeVersionController(IFileStorage fileStorage) : EntityController<NodeVersion>
{
    static NodeVersionController()
    {
        LogOnChange = true;

        ListFields.RemoveField("Source", "Size", "FileHash", "Preinstall", "Executor");
        ListFields.RemoveCreateField().RemoveRemarkField();

        {
            //var df = ListFields.GetField("Source") as ListField;
            //df.DisplayName = "下载";
            //df.Url = "{Source}";
            //df.DataVisible = e =>
            //{
            //    var entity = e as NodeVersion;
            //    return !entity.Source.IsNullOrEmpty() && !entity.Source.StartsWithIgnoreCase("http://", "https://");
            //};
        }

        {
            var df = ListFields.GetField("Strategy") as ListField;
            if (df != null)
            {
                // 策略文本较长,列表截断显示,完整内容通过标题悬浮查看,避免把末列挤出屏幕
                df.Title = "{Strategy}";
                df.GetValue = e => e is NodeVersion entity && entity.Strategy.Length > 60
                    ? entity.Strategy[..60] + "..."
                    : (e as NodeVersion)?.Strategy;
            }
        }

        {
            var df = ListFields.AddListField("Log", "CreateUserID");
            df.DisplayName = "审计日志";
            df.Header = "审计日志";
            df.Url = "/Admin/Log?category=节点版本&linkId={ID}";
            df.Target = "_frame";
        }
    }

    /// <summary>高级搜索。按条件分页查询</summary>
    /// <param name="p">分页参数</param>
    /// <returns>实体列表</returns>
    protected override IEnumerable<NodeVersion> Search(Pager p)
    {
        var enable = p["enable"]?.ToBoolean();
        var start = p["dtStart"].ToDateTime();
        var end = p["dtEnd"].ToDateTime();
        var key = p["q"];

        return NodeVersion.Search(start, end, enable, key, p);
    }

    protected override Int32 OnDelete(NodeVersion entity)
    {
        if (!entity.Source.IsNullOrEmpty())
        {
            //取 Id@Attachment 唯一
            var id = Path.GetFileNameWithoutExtension(entity.Source.Replace("/cube/file?id=", String.Empty));
            var att = ProjectScopeRules.FilterVisible(Attachment.FindById(id.ToLong()));
            if (att != null)
            {
                var attPath = att.GetFilePath();
                if (System.IO.File.Exists(attPath))
                {
                    XTrace.Log.Error("Successfully deleted {0}", attPath);
                    System.IO.File.Delete(attPath);
                }
                //删除记录
                att.DeleteAsync();
            }
        }

        var rs = base.OnDelete(entity);
        return rs;
    }

    protected override async Task<Attachment> SaveFile(NodeVersion entity, IFormFile file, String uploadPath, String fileName)
    {
        var att = await base.SaveFile(entity, file, uploadPath, fileName);
        if (att != null)
        {
            entity.FileHash = att.Hash;
            entity.Size = att.Size;
            entity.Source = $"/cube/file?id={att.Id}{att.Extension}";

            entity.Update();
        }

        //// 广播指定附件在当前节点可用
        //await fileStorage.PublishNewFileAsync(att.Id, att.FilePath, HttpContext.RequestAborted);

        // 不给上层拿到附件,避免Url字段被覆盖
        return null;
    }

    //protected override async Task<IList<String>> SaveFiles(NodeVersion entity, String uploadPath = null)
    //{
    //    var rs = await base.SaveFiles(entity, uploadPath);

    //    // 更新文件哈希
    //    if (rs.Count > 0 && !entity.Source.IsNullOrEmpty())
    //    {
    //        var fi = NewLife.Cube.Setting.Current.UploadPath.CombinePath(entity.Source).AsFile();
    //        if (fi.Exists)
    //        {
    //            entity.FileHash = fi.ReadBytes().MD5().ToHex();
    //        }
    //    }

    //    return rs;
    //}

}