/// <summary>
        /// Записать файл на диск
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private async Task <State> WriteFile(PartialFile file)
        {
            State state = new State();
            //try
            {
                string fileDirectoryPath = $"{DataPath}/{file.FileName}";
                if (Directory.Exists(fileDirectoryPath))
                {
                    Directory.Delete(fileDirectoryPath, true);
                }
                Directory.CreateDirectory(fileDirectoryPath);

                foreach (var block in file.Blocks)
                {
                    State isWritten = await WriteBlock(block);

                    state.IsSuccess = state && isWritten;
                    if (!isWritten)
                    {
                        isWritten.Messages.AddRange(isWritten.Messages);
                        break;
                    }
                }
                return(state);
            }
            //catch (Exception ex)
            //{
            //    state.IsSuccess = false;
            //    state.Messages.Add(ex.Message);
            //    return state;
            //}
        }
예제 #2
0
 public PartialMap(string path, TextureManager textures)
     : base()
 {
     this.textures = textures;
     mapfile       = new PartialFile(path);
     Load(mapfile.LoadBlock(), textures);
     ParseBlocks();
 }
        public async Task Write_File()
        {
            string      fileName        = "testFile";
            int         totalBlockCount = 2;
            PartialFile partilFile      = new PartialFile
            {
                FileName = fileName,
                Blocks   = new List <Block>
                {
                    new Block
                    {
                        Data = new byte[] { 1, 2, 3, 4, 5 },
                        Info = new BlockInfo
                        {
                            FileName        = fileName,
                            Index           = 0,
                            TotalBlockCount = totalBlockCount
                        }
                    },

                    new Block
                    {
                        Data = new byte[] { 6, 7, 8, 9 },
                        Info = new BlockInfo
                        {
                            FileName        = fileName,
                            Index           = 1,
                            TotalBlockCount = totalBlockCount
                        }
                    }
                }
            };

            SavePartialFileRequest request = new SavePartialFileRequest
            {
                PartilFile    = partilFile,
                ForceOwerrite = true
            };

            IActionResult actionResult = await _nodeController.SaveFile(request);

            OkObjectResult ok = actionResult as OkObjectResult;

            Assert.NotNull(ok);
            State state = ok.Value as State;

            Assert.NotNull(state);
            Assert.True(state.IsSuccess);

            Assert.True(_nodeService.FileExist(fileName));
            Assert.True(_nodeService.TryGetBlockInfo(fileName, 0, out var info));
            Assert.True(_nodeService.TryGetBlockInfo(fileName, 1, out var info2));
        }
예제 #4
0
        public void WritesPartialMediaFiles(string path, double duration, bool?isIndependent, bool?hasGap, int?length, int?offset, string expected)
        {
            var mediaFile = new PartialFile
            {
                Path          = path,
                Duration      = duration,
                IsIndependent = isIndependent ?? null,
                HasGap        = hasGap ?? null,
                ByteRange     = length.HasValue ? new ByteRange(length.Value, offset) : (ByteRange?)null
            };

            AssertStreamContentEqual(expected, mediaFile);
        }
        /// <summary>
        /// Добавить или перезаписать файл
        /// </summary>
        /// <param name="file">Файл</param>
        /// <param name="forceOwerrite">Перезаписать существующий</param>
        public async Task <State> TryAddFile(PartialFile file, bool forceOwerrite = false)
        {
            State state = new State();
            //try
            {
                if (file != null)
                {
                    if (Files.ContainsKey(file.FileName))
                    {
                        if (forceOwerrite)
                        {
                            State isDeleted = DeleteFile(file.FileName);
                            state += isDeleted;
                            if (!isDeleted)
                            {
                                return(state);
                            }
                        }
                        else
                        {
                            state.IsSuccess = false;
                            state.Messages.Add($"File with name {file.FileName} already exist");
                            return(state);
                        }
                    }
                    State isWritten = await WriteFile(file);

                    state += isWritten;
                    if (isWritten)
                    {
                        Files.Add(file.FileName, file.Blocks.Select(b => b.Info).ToList());
                    }
                }
                else
                {
                    state.IsSuccess = false;
                    state.Messages.Add("File is null");
                }
                return(state);
            }
            //catch (Exception ex)
            //{
            //    state.IsSuccess = false;
            //    state.Messages.Add(ex.Message);
            //    return state;
            //}
        }
        public async Task Write_Test_File_By_2_Blocks()
        {
            string      fileName        = "testFile";
            int         totalBlockCount = 2;
            PartialFile partilFile      = new PartialFile
            {
                FileName = fileName,
                Blocks   = new List <Block>
                {
                    new Block
                    {
                        Data = new byte[] { 1, 2, 3, 4, 5 },
                        Info = new BlockInfo
                        {
                            FileName        = fileName,
                            Index           = 0,
                            TotalBlockCount = totalBlockCount
                        }
                    },

                    new Block
                    {
                        Data = new byte[] { 6, 7, 8, 9 },
                        Info = new BlockInfo
                        {
                            FileName        = fileName,
                            Index           = 1,
                            TotalBlockCount = totalBlockCount
                        }
                    }
                }
            };

            bool state = await _nodeService.TryAddFile(partilFile);

            Assert.True(state);
            Assert.True(_nodeService.FileExist(fileName));
            Assert.True(_nodeService.TryGetBlockInfo(fileName, 0, out var info));
            Assert.True(_nodeService.TryGetBlockInfo(fileName, 1, out var info2));
        }