Exemplo n.º 1
0
        public SharedResultVM DatabaseExport(string zipName = "db/backup.zip")
        {
            return(SharedResultVM.Try(vm =>
            {
                //是否覆盖备份,默认不覆盖,避免线上重置功能被破坏
                var CoverBack = true;

                if (CoverBack)
                {
                    var edb = new SharedDataKit.TransferVM.ExportDatabase
                    {
                        ZipPath = PathTo.Combine(GlobalTo.ContentRootPath, zipName),
                        ReadConnectionInfo = new SharedDataKit.TransferVM.ConnectionInfo()
                        {
                            ConnectionString = SharedDbContext.FactoryTo.GetConn().Replace("Filename=", "Data Source="),
                            ConnectionType = GlobalTo.TDB
                        }
                    };

                    vm = SharedDataKit.DataKit.ExportDatabase(edb);
                }
                else
                {
                    vm.Set(SharedEnum.RTag.refuse);
                    vm.Msg = "已被限制导出覆盖";
                }

                return vm;
            }));
        }
Exemplo n.º 2
0
        public SharedResultVM StaticResourceUsageAnalysis(string rootPath = @"D:\ROOM\static")
        {
            return(SharedResultVM.Try(vm =>
            {
                vm.LogEvent(le =>
                {
                    Console.WriteLine(le.NewItems[0]);
                });

                var db = ContextBaseFactory.CreateDbContext();
                var uws = db.UserWriting.ToList();
                var urs = db.UserReply.ToList();
                var runs = db.Run.ToList();

                var filesPath = Directory.GetFiles(rootPath, "*", SearchOption.AllDirectories).ToList();
                foreach (var path in filesPath)
                {
                    if (!path.Contains(".git") && !path.Contains($@"{rootPath}\static\"))
                    {
                        var filename = Path.GetFileName(path);

                        if (!uws.Any(x => x.UwContent.Contains(filename)) && !urs.Any(x => x.UrContent != null && x.UrContent.Contains(filename)) && !runs.Any(x => x.RunContent1.Contains(filename) || x.RunContent2.Contains(filename)))
                        {
                            vm.Log.Add(path);
                        }
                    }
                }

                return vm;
            }));
        }
Exemplo n.º 3
0
        public SharedResultVM DatabaseReset(string zipName = "db/backup.zip")
        {
            return(SharedResultVM.Try(vm =>
            {
                if (HttpContext != null && new UserAgentTo(new ClientTo(HttpContext).UserAgent).IsBot)
                {
                    vm.Set(SharedEnum.RTag.refuse);
                    vm.Msg = "are you human?";
                }
                else
                {
                    var idb = new SharedDataKit.TransferVM.ImportDatabase
                    {
                        WriteConnectionInfo = new SharedDataKit.TransferVM.ConnectionInfo
                        {
                            ConnectionType = GlobalTo.TDB,
                            ConnectionString = SharedDbContext.FactoryTo.GetConn().Replace("Filename=", "Data Source=")
                        },
                        ZipPath = PathTo.Combine(GlobalTo.ContentRootPath, zipName),
                        WriteDeleteData = true
                    };

                    vm = SharedDataKit.DataKit.ImportDatabase(idb);
                }

                return vm;
            }));
        }
Exemplo n.º 4
0
        public SharedResultVM ReplyList(string id, int page = 1)
        {
            return(SharedResultVM.Try(vm =>
            {
                var uinfo = Apps.LoginService.Get(HttpContext);

                var pag = new SharedPaginationVM
                {
                    PageNumber = Math.Max(page, 1),
                    PageSize = 10
                };

                var list = Application.CommonService.ReplyOneQuery(Application.EnumService.ReplyType.GuffRecord, id, pag);
                //匿名用户,生成邮箱MD5加密用于请求头像
                foreach (var item in list)
                {
                    if (item.Uid == 0 && !string.IsNullOrWhiteSpace(item.UrAnonymousMail))
                    {
                        item.Spare3 = CalcTo.MD5(item.UrAnonymousMail);
                    }
                }

                var pvm = new SharedPageVM()
                {
                    Rows = list,
                    Pag = pag
                };
                vm.Data = pvm;

                vm.Set(SharedEnum.RTag.success);

                return vm;
            }));
        }
Exemplo n.º 5
0
        public SharedResultVM Detail(string id)
        {
            return(SharedResultVM.Try(vm =>
            {
                if (string.IsNullOrWhiteSpace(id))
                {
                    vm.Set(SharedEnum.RTag.invalid);
                }
                else
                {
                    var ctype = Application.EnumService.ConnectionType.GuffRecord.ToString();

                    var uinfo = Apps.LoginService.Get(HttpContext);

                    var query = from a in db.GuffRecord
                                join b in db.UserInfo on a.Uid equals b.UserId
                                join c in db.UserConnection.Where(x => x.UconnTargetType == ctype && x.UconnAction == 1 && x.Uid == uinfo.UserId) on a.GrId equals c.UconnTargetId into cg
                                from c1 in cg.DefaultIfEmpty()
                                where a.GrId == id
                                select new
                    {
                        a,
                        UconnTargetId = c1 == null ? null : c1.UconnTargetId,
                        b.Nickname
                    };
                    var qm = query.FirstOrDefault();
                    if (qm == null)
                    {
                        vm.Set(SharedEnum.RTag.invalid);
                    }
                    else
                    {
                        if (qm.a.GrOpen == 1 || uinfo.UserId == qm.a.Uid)
                        {
                            // 阅读 +1
                            qm.a.GrReadNum += 1;
                            db.Update(qm.a);
                            db.SaveChanges();

                            qm.a.Spare1 = string.IsNullOrEmpty(qm.UconnTargetId) ? "" : "laud";
                            qm.a.Spare2 = (uinfo.UserId == qm.a.Uid) ? "owner" : "";
                            qm.a.Spare3 = qm.Nickname;

                            vm.Data = qm.a;

                            vm.Set(SharedEnum.RTag.success);
                        }
                        else
                        {
                            vm.Set(SharedEnum.RTag.unauthorized);
                        }
                    }
                }

                return vm;
            }));
        }
Exemplo n.º 6
0
        public SharedResultVM HotTag()
        {
            return(SharedResultVM.Try(vm =>
            {
                var listTags = db.GuffRecord.OrderByDescending(x => x.GrCreateTime).Select(x => x.GrTag).Take(1000).ToList();
                var orderTags = string.Join(",", listTags).Split(',').GroupBy(x => x).OrderByDescending(x => x.Count()).Select(x => x.Key).Take(20).ToList();

                vm.Data = orderTags;
                vm.Set(SharedEnum.RTag.success);

                return vm;
            }));
        }
Exemplo n.º 7
0
        public SharedResultVM Upload(IFormFile file, [FromForm] string subdir = "/static")
        {
            return(SharedResultVM.Try(vm =>
            {
                if (file != null)
                {
                    vm = UploadCheck(file, null, "", subdir);
                }
                else
                {
                    vm.Set(SharedEnum.RTag.lack);
                }

                return vm;
            }));
        }
Exemplo n.º 8
0
        public SharedResultVM Delete(string id)
        {
            return(SharedResultVM.Try(vm =>
            {
                if (string.IsNullOrWhiteSpace(id))
                {
                    vm.Set(SharedEnum.RTag.invalid);
                }
                else
                {
                    var uinfo = Apps.LoginService.Get(HttpContext);
                    if (uinfo.UserId != 0)
                    {
                        var mo = db.GuffRecord.Find(id);

                        if (mo == null)
                        {
                            vm.Set(SharedEnum.RTag.invalid);
                        }
                        else
                        {
                            if (mo.Uid != uinfo.UserId)
                            {
                                vm.Set(SharedEnum.RTag.unauthorized);
                            }
                            else if (mo.GrStatus == -1)
                            {
                                vm.Set(SharedEnum.RTag.refuse);
                            }
                            else
                            {
                                db.Remove(mo);
                                int num = db.SaveChanges();

                                vm.Set(num > 0);
                            }
                        }
                    }
                    else
                    {
                        vm.Set(SharedEnum.RTag.unauthorized);
                    }
                }

                return vm;
            }));
        }
Exemplo n.º 9
0
        public SharedResultVM List(string category, string q, int uid, string nv, string tag, string obj, int page = 1)
        {
            return(SharedResultVM.Try(vm =>
            {
                //所属用户
                var OwnerId = 0;

                if (uid != 0)
                {
                    OwnerId = uid;
                }

                var uinfo = Apps.LoginService.Get(HttpContext);

                if (new List <string> {
                    "me", "melaud", "mereply"
                }.Contains(category))
                {
                    if (uinfo.UserId == 0)
                    {
                        vm.Set(SharedEnum.RTag.unauthorized);
                    }
                    else
                    {
                        if (category == "me")
                        {
                            OwnerId = uinfo.UserId;
                        }

                        var pvm = Application.CommonService.GuffQuery(category, q, nv, tag, obj, OwnerId, uinfo.UserId, page);
                        vm.Data = pvm;

                        vm.Set(SharedEnum.RTag.success);
                    }
                }
                else
                {
                    var pvm = Application.CommonService.GuffQuery(category, q, nv, tag, obj, OwnerId, uinfo.UserId, page);
                    vm.Data = pvm;

                    vm.Set(SharedEnum.RTag.success);
                }

                return vm;
            }));
        }
Exemplo n.º 10
0
        public SharedResultVM DatabaseExport(string zipName = "db/backup.zip")
        {
            return(SharedResultVM.Try(vm =>
            {
                var edb = new SharedDataKit.TransferVM.ExportDatabase
                {
                    ZipPath = PathTo.Combine(GlobalTo.ContentRootPath, zipName),
                    ReadConnectionInfo = new SharedDataKit.TransferVM.ConnectionInfo()
                    {
                        ConnectionString = SharedDbContext.FactoryTo.GetConn().Replace("Filename=", "Data Source="),
                        ConnectionType = GlobalTo.TDB
                    }
                };

                vm = SharedDataKit.DataKit.ExportDatabase(edb);

                return vm;
            }));
        }
Exemplo n.º 11
0
        public SharedResultVM UserInfo()
        {
            return(SharedResultVM.Try(vm =>
            {
                var uinfo = Apps.LoginService.Get(HttpContext);

                if (uinfo.UserId != 0)
                {
                    vm.Data = uinfo;

                    vm.Set(SharedEnum.RTag.success);
                }
                else
                {
                    vm.Set(SharedEnum.RTag.unauthorized);
                }

                return vm;
            }));
        }
Exemplo n.º 12
0
        public SharedResultVM DatabaseImport(string zipName = "db/backup.zip", bool clearTable = false)
        {
            return(SharedResultVM.Try(vm =>
            {
                var idb = new SharedDataKit.TransferVM.ImportDatabase
                {
                    WriteConnectionInfo = new SharedDataKit.TransferVM.ConnectionInfo
                    {
                        ConnectionType = GlobalTo.TDB,
                        ConnectionString = SharedDbContext.FactoryTo.GetConn().Replace("Filename=", "Data Source=")
                    },
                    ZipPath = PathTo.Combine(GlobalTo.ContentRootPath, zipName),
                    WriteDeleteData = clearTable
                };

                vm = SharedDataKit.DataKit.ImportDatabase(idb);

                return vm;
            }));
        }
Exemplo n.º 13
0
        public SharedResultVM Connection(string type, int ac, string id)
        {
            return(SharedResultVM.Try(vm =>
            {
                var uinfo = Apps.LoginService.Get(HttpContext);

                if (uinfo.UserId == 0)
                {
                    vm.Set(SharedEnum.RTag.unauthorized);
                }
                else if (string.IsNullOrWhiteSpace(id))
                {
                    vm.Set(SharedEnum.RTag.invalid);
                }
                else if (!new List <string> {
                    "add", "cancel"
                }.Contains(type))
                {
                    vm.Set(SharedEnum.RTag.invalid);
                }
                else if (!new List <int> {
                    1, 2
                }.Contains(ac))
                {
                    vm.Set(SharedEnum.RTag.invalid);
                }
                else
                {
                    var currMo = db.GuffRecord.Find(id);

                    if (currMo == null)
                    {
                        vm.Set(SharedEnum.RTag.invalid);
                    }
                    else
                    {
                        var ctype = Application.EnumService.ConnectionType.GuffRecord.ToString();
                        switch (type)
                        {
                        case "add":
                            {
                                if (db.UserConnection.Any(x => x.Uid == uinfo.UserId && x.UconnTargetType == ctype && x.UconnTargetId == id && x.UconnAction == ac))
                                {
                                    vm.Set(SharedEnum.RTag.exist);
                                }
                                else
                                {
                                    //关联记录
                                    var ucmo = new Domain.UserConnection()
                                    {
                                        UconnId = UniqueTo.LongId().ToString(),
                                        Uid = uinfo.UserId,
                                        UconnTargetType = Application.EnumService.ConnectionType.GuffRecord.ToString(),
                                        UconnTargetId = id,
                                        UconnAction = ac,
                                        UconnCreateTime = DateTime.Now
                                    };

                                    db.Add(ucmo);

                                    switch (ac)
                                    {
                                    case 1:
                                        currMo.GrLaud += 1;
                                        break;

                                    case 2:
                                        currMo.GrMark += 1;
                                        break;
                                    }
                                    db.Update(currMo);

                                    int num = db.SaveChanges();

                                    vm.Set(num > 0);
                                }
                            }
                            break;

                        case "cancel":
                            {
                                var curruc = db.UserConnection.FirstOrDefault(x => x.Uid == uinfo.UserId && x.UconnTargetType == ctype && x.UconnTargetId == id && x.UconnAction == ac);
                                if (curruc == null)
                                {
                                    vm.Set(SharedEnum.RTag.invalid);
                                }
                                else
                                {
                                    db.Remove(curruc);

                                    switch (ac)
                                    {
                                    case 1:
                                        currMo.GrLaud -= 1;
                                        break;

                                    case 2:
                                        currMo.GrMark -= 1;
                                        break;
                                    }
                                    db.Update(currMo);

                                    int num = db.SaveChanges();

                                    vm.Set(num > 0);
                                }
                            }
                            break;
                        }
                    }
                }

                return vm;
            }));
        }
Exemplo n.º 14
0
        public SharedResultVM DatabaseBackupToGit()
        {
            return(SharedResultVM.Try(vm =>
            {
                if (GlobalTo.GetValue <bool>("ReadOnly"))
                {
                    vm.Set(SharedEnum.RTag.refuse);
                    return vm;
                }

                var now = $"{DateTime.Now:yyyyMMdd_HHmmss}";

                var db = ContextBaseFactory.CreateDbContext();
                var database = db.Database.GetDbConnection().Database;

                var createScript = db.Database.GenerateCreateScript();

                //备份创建脚本
                try
                {
                    var b1 = Convert.ToBase64String(Encoding.UTF8.GetBytes(createScript));
                    var p1 = $"{database}/backup_{now}.sql";

                    vm.Log.Add(PutGitee(b1, p1, now));
                    vm.Log.Add(PutGitHub(b1, p1, now));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    vm.Log.Add(ex.Message);
                }

                Thread.Sleep(1000 * 1);

                //备份数据
                var zipPath = $"db/backup_{now}.zip";
                if (DatabaseExport(zipPath).Code == 200)
                {
                    var ppath = PathTo.Combine(GlobalTo.ContentRootPath, zipPath);

                    try
                    {
                        var b2 = Convert.ToBase64String(System.IO.File.ReadAllBytes(ppath));
                        var p2 = $"{database}/backup_{now}.zip";

                        vm.Log.Add(PutGitee(b2, p2, now));
                        vm.Log.Add(PutGitHub(b2, p2, now));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                        vm.Log.Add(ex.Message);

                        System.IO.File.Delete(ppath);
                    }
                    finally
                    {
                        System.IO.File.Delete(ppath);
                    }
                }

                return vm;
            }));
        }