Exemplo n.º 1
0
        public async Task <Resp> ModifyAsync(Models.UpdateTheme updateTheme)
        {
            (bool isValid, string msg) = updateTheme.IsValid();

            if (!isValid)
            {
                return(Resp.Fault(Resp.NONE, msg));
            }

            using var db = new MyForContext();

            DB.Tables.Theme model = await db.Themes.FirstOrDefaultAsync(t => t.Id == Id);

            if (model is null)
            {
                return(Resp.Fault(Resp.NONE, NONE_THEME));
            }
            model.Description = updateTheme.Description;
            if (updateTheme.IsChangeCover)
            {
                Files.File file = new Files.File();
                model.CoverId = await file.SaveImageToDB(updateTheme.Cover);
            }
            int suc = await db.SaveChangesAsync();

            if (suc == 1)
            {
                return(Resp.Success(Resp.NONE, ""));
            }
            return(Resp.Fault(Resp.NONE, "失败"));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 将图片上传, 并设置为内容的图片
        /// 若再执行一次, 也不会重复执行上传替换
        /// </summary>
        public async Task UploadImagesAndSetInContent()
        {
            if (imageSetedInContent)
            {
                return;
            }
            Files.File file = new Files.File();

            foreach (Share.KeyValue <string, IFormFile> img in Images)
            {
                //  若内容中不包含该文件, 则跳过
                if (!source_Content.Contains(img.Key))
                {
                    continue;
                }

                var(path, thumbnail) = await file.SaveImageGetThumbnailAndPath(img.Value);

                //  替换原图和缩略图
                content = content.Replace($"href=\"{img.Key}\"", $"href=\"{path}\"")
                          .Replace($"{img.Key}", $"{thumbnail}");
            }

            imageSetedInContent = true;
        }
Exemplo n.º 3
0
        // Group: Support Functions
        // __________________________________________________________________________


        /* Function: FindOrCreateFileSourceEntryOf
         * Finds or creates the file source entry associated with the passed file.
         */
        protected MenuEntries.FileSource FindOrCreateFileSourceEntryOf(Files.File file)
        {
            var fileSource      = EngineInstance.Files.FileSourceOf(file);
            var fileSourceEntry = FindFileSourceEntry(fileSource);

            if (fileSourceEntry == null)
            {
                fileSourceEntry = CreateFileSourceEntry(fileSource);
            }

            return(fileSourceEntry);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 添加一个账号
        /// </summary>
        /// <param name="newAccount"></param>
        /// <returns></returns>
        public async Task <Resp> AddAccount(Models.NewAccount newAccount)
        {
            (bool isValid, string msg) = newAccount.IsValid();
            if (!isValid)
            {
                return(Resp.Fault(msg));
            }

            using var db = new MyForContext();

            if (await db.Accounts.AnyAsync(a => a.AccountName == newAccount.AccountName))
            {
                return(Resp.Fault("账号名已经存在"));
            }

            //if (await db.Accounts.AnyAsync(a => a.Email == newAccount.Email))
            int avatarId;

            if (newAccount.Avatar is null)
            {
                avatarId = User.GetDefaultAvatarId();
            }
            else
            {
                Files.File file = new Files.File();
                avatarId = await file.SaveFileToDBAsync(newAccount.Avatar);
            }

            DB.Tables.Account model = new DB.Tables.Account
            {
                AccountName = newAccount.AccountName,
                Password    = newAccount.Password,
                AvatarId    = avatarId,
                Email       = newAccount.Email,
                Phone       = newAccount.Phone,
                Gender      = newAccount.Gender,
                CreateById  = newAccount.CreatorId
            };
            db.Accounts.Add(model);
            int suc = await db.SaveChangesAsync();

            if (suc == 1)
            {
                return(Resp.Success(Resp.NONE, "添加成功"));
            }
            return(Resp.Fault(msg: "添加账号失败"));
        }
Exemplo n.º 5
0
        /// <summary>
        /// 发布新帖
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <(bool, string)> PulishNewPostAsync(Models.NewPostInfo model)
        {
            bool   suc = true;
            string msg = "";

            /*
             *  先保存文件
             *  会保存在数据库的 Files 表
             *  返回的 fileId 为保存的 ID
             */
            Files.File file = new Files.File();
            if (model.File is null)
            {
                return(false, "需要上传文件");
            }

            int fileId = await file.SaveFileToDBAsync(model.File);

            if (fileId == Files.File.NOT_FILES)
            {
                return(false, "文件上传失败");
            }

            using ATXDbContext db = new ATXDbContext();

            DB.Tables.Post newPost = new DB.Tables.Post
            {
                Author      = model.Author,
                Description = model.Description,
                FileId      = fileId
            };
            db.Posts.Add(newPost);

            //  添加今天的一条发布记录
            await Records.Records.TodayPulishRecordIncreaseAsync();

            if (await db.SaveChangesAsync() == 1)
            {
                return(suc, msg);
            }
            return(false, "发布失败");
        }
Exemplo n.º 6
0
        /// <summary>
        /// 新增主题
        /// </summary>
        /// <returns></returns>
        public async Task <Resp> AddThemeAsync(Models.NewTheme newTheme)
        {
            (bool isValid, string msg) = newTheme.IsValid();
            if (!isValid)
            {
                return(Resp.Fault(Resp.NONE, msg));
            }

            using var db = new MyForContext();

            bool exist = await db.Themes.AnyAsync(t => t.Name == newTheme.Name);

            if (exist)
            {
                return(Resp.Fault(Resp.NONE, "已存在的主题"));
            }

            Files.File file = new Files.File();

            DB.Tables.Theme model = new DB.Tables.Theme
            {
                Name        = newTheme.Name,
                Description = newTheme.Description,
                //CoverId = await file.SaveImageToDB(newTheme.Cover),
                MasterId   = newTheme.CreateById,
                CreateById = newTheme.CreateById,
                State      = (int)Theme.ThemeStates.ToAudit
            };
            db.Themes.Add(model);
            int suc = await db.SaveChangesAsync();

            if (suc == 1)
            {
                return(Resp.Success(Resp.NONE, ""));
            }
            return(Resp.Fault(Resp.NONE, "失败"));
        }
Exemplo n.º 7
0
        /// <summary>
        /// 修改头像
        /// </summary>
        /// <param name="avatar"></param>
        /// <returns></returns>
        public async Task <Resp> ChangeAvatar(IFormFile avatar)
        {
            if (avatar is null)
            {
                return(Resp.Fault(Resp.NONE, "头像不能为空"));
            }

            using var db = new MyForContext();

            DB.Tables.Account account = await db.Accounts.FirstOrDefaultAsync(a => a.Id == Id);

            if (account is null)
            {
                return(Resp.Fault(Resp.NONE, "管理员不存在"));
            }
            int oldAvatarId = account.AvatarId;

            Files.File file      = new Files.File();
            int        newFileId = await file.SaveImageToDB(avatar);

            if (newFileId == Files.File.NOT_FILES)
            {
                return(Resp.Fault(Resp.NONE, "修改失败"));
            }

            account.AvatarId = newFileId;
            int suc = await db.SaveChangesAsync();

            if (suc != 1)
            {
                return(Resp.Fault(Resp.NONE, "修改失败"));
            }
            //  删除旧头像文件
            file.Delete(oldAvatarId);
            return(Resp.Success(Resp.NONE, ""));
        }
Exemplo n.º 8
0
        /* Function: AddFile
         * Adds a file to the menu tree.
         */
        public void AddFile(Files.File file)
        {
                        #if DEBUG
            if (isCondensed)
            {
                throw new Exception("Cannot add a file to the menu once it's been condensed.");
            }
                        #endif


            // Find which file source owns this file and generate a relative path to it.

            MenuEntries.FileSource fileSourceEntry = FindOrCreateFileSourceEntryOf(file);
            Path relativePath = fileSourceEntry.WrappedFileSource.MakeRelative(file.FileName);


            // Split off the file name and split the rest into individual folder names.

            string        prefix;
            List <string> pathSegments;
            relativePath.Split(out prefix, out pathSegments);

            string fileName = pathSegments[pathSegments.Count - 1];
            pathSegments.RemoveAt(pathSegments.Count - 1);


            // Create the file entry and find out where it goes.  Create new folder levels as necessary.

            MenuEntries.File      fileEntry = new MenuEntries.File(file);
            MenuEntries.Container container = fileSourceEntry;

            foreach (string pathSegment in pathSegments)
            {
                Path pathFromFileSource;

                if (container == fileSourceEntry)
                {
                    pathFromFileSource = pathSegment;
                }
                else
                {
                    pathFromFileSource = (container as MenuEntries.Folder).PathFromFileSource + '/' + pathSegment;
                }

                MenuEntries.Folder folderEntry = null;

                foreach (var member in container.Members)
                {
                    if (member is MenuEntries.Folder &&
                        (member as MenuEntries.Folder).PathFromFileSource == pathFromFileSource)
                    {
                        folderEntry = (MenuEntries.Folder)member;
                        break;
                    }
                }

                if (folderEntry == null)
                {
                    folderEntry        = new MenuEntries.Folder(pathFromFileSource);
                    folderEntry.Parent = container;
                    container.Members.Add(folderEntry);
                }

                container = folderEntry;
            }

            fileEntry.Parent = container;
            container.Members.Add(fileEntry);
        }
Exemplo n.º 9
0
 internal PlainWriter(
     Files.File file,
     IOutputStream stream
     ) : base(file, stream)
 {
 }
Exemplo n.º 10
0
        /// <summary>
        /// Generate a new, random world.
        /// </summary>
        public void CreateWorld(TerminalGame game, int companies)
        {
            DateTime beginWorld = DateTime.Now;

            Game = game;
            GameClock.Initialize();
            CurrentGameTime = GameClock.GameTime;
            Player          = new Player();
            Player.CreateNewPlayer("testPlayer", "abc123");
            Computers   = new List <Computer>();
            People      = new List <Person>();
            CompanyList = new List <Company>();

            Files.File testFile   = new Files.File("testFile", "this is a test", FileType.Text);
            Files.File testSubDir = new Files.File("test2");
            testSubDir.AddFile(testFile);
            Files.File testDir = new Files.File("test");
            testDir.AddFile(testSubDir);
            Files.File testRoot = new Files.File("");
            testRoot.AddFile(testDir);
            Files.File testFile2 = new Files.File("testFile2", "test 2", FileType.Text);
            testRoot.AddFile(testFile2);
            FileSystem pfs = new FileSystem(testRoot);
            var        bin = new Files.File("bin");

            bin.AddFile(new Files.File("terminal", "terminal executable", FileType.Binary, 48374));
            bin.AddFile(new Files.File("notepad", "notepad executable", FileType.Binary, 7522));
            bin.AddFile(new Files.File("remoteview", "remoteview executable", FileType.Binary, 61325));
            bin.AddFile(new Files.File("netmap", "netmap executable", FileType.Binary, 879820));
            pfs.RootDir.AddFile(bin);

            Company  mCorp = new Company("Mission Corp");
            Computer mComp = new Computer("Mission Hub", new int[] { 80 }, ComputerType.Server, mCorp, "123.123.123.123", "swordfish", FileSystemGenerator.GenerateDefaultFilesystem());

            mCorp.GetComputers.Add(mComp);
            CompanyList.Add(mCorp);

            Company pc = new Company("Unknown", new Person("Unknown", DateTime.Parse("1970-01-01"), (Gender)(-1), (EducationLevel)(-1)), new Person("Unknown", DateTime.Parse("1970-01-01"), (Gender)(-1), (EducationLevel)(-1)), 0, 0);;

            Computer PlayerComp = new Computer("localhost", new int[] { 69, 1337 }, ComputerType.Workstation, pc, "127.0.0.1", Player.Password, pfs)
            {
                Game = game
            };

            Computers.Add(PlayerComp);

            DateTime beginCompanies = DateTime.Now;

            for (int i = 0; i < companies; i++)
            {
                Console.Write($"Creating company {i}/{companies}...");
                Company comp   = Company.GetRandomCompany(game);
                int     whoops = 0;
                while (CheckIfNameExists(comp.Name))
                {
                    comp.Name = Companies.Generator.CompanyGenerator.GenerateName();
                    whoops++;
                }
                comp.GenerateComputers();
                Console.WriteLine($"Done: {comp.Name}, whoopses: {whoops}.");
                CompanyList.Add(comp);
            }
            Console.WriteLine("Generated {0} companies in {1} seconds.", CompanyList.Count, (DateTime.Now.Subtract(beginCompanies).TotalSeconds).ToString("N4"));

            List <string> names = new List <string>();

            foreach (Company c in CompanyList)
            {
                Computers.AddRange(c.GetComputers);
                People.AddRange(c.GetPeople);
                names.Add(c.Name);
            }

            List <string> test = SortByLength(names.AsEnumerable()).ToList();

            foreach (string s in test)
            {
                Console.WriteLine(s);
            }

            foreach (Computer c in Computers)
            {
                c.Init(Game);
                if (c.Owner == CompanyList[35])
                {
                    c.IsMissionObjective = true;
                }
            }

            PlayerComp.PlayerHasRoot = true;
            PlayerComp.AccessLevel   = AccessLevel.Root;

            Player.PlayerComp = PlayerComp;

            Console.WriteLine("Generated world in {0} seconds.", (DateTime.Now.Subtract(beginWorld).TotalSeconds).ToString("N4"));
        }
Exemplo n.º 11
0
 internal Reader(IInputStream stream, Files.File file)
 {
     this.parser = new FileParser(stream, file);
 }
Exemplo n.º 12
0
 internal FileParser(IInputStream stream, Files.File file)
     : base(stream)
 {
     this.file = file;
 }
Exemplo n.º 13
0
 internal Reader(IInputStream stream, Files.File file, string password = null, System.IO.Stream keyStoreInputStream = null)
 {
     this.parser = new FileParser(stream, file, password, keyStoreInputStream);
 }
Exemplo n.º 14
0
 internal CompressedWriter(Files.File file, IOutputStream stream) : base(file, stream)
 {
 }