public async Task Read_TextFileWithoutHeaders_ReturnsFileDataWithCustomHeaders()
        {
            var parseOptions = new FileParseOptionsDto()
            {
                FullName   = TAB_TEXTFILE_PATH,
                Delimiter  = "\t",
                HasHeaders = false
            };

            var expectedData = new FileDataDto()
            {
                FullName  = TAB_TEXTFILE_PATH,
                Delimiter = "\t",
                Headers   = new List <string>()
                {
                    "col_0", "col_1", "col_2"
                },
                Rows = new List <string[]>()
                {
                    new string[] { "Name", "Site", "Email" },
                    new string[] { "Betty J. Brown", "OEMJobs.com", "*****@*****.**" },
                    new string[] { "John D. Ginter", "keepclicker.com", "*****@*****.**" },
                    new string[] { "Roger I. Clinton", "tastrons.com", "*****@*****.**" }
                }
            };

            var resultData = await _fileProcessor.Read(parseOptions);

            Assert.IsTrue(IsFileDataDtoEquals(resultData, expectedData));
        }
        public Task <FileDataDto> SaveAsync(FileDataDto value)
        {
            var rightMethodInfo = GetType().GetMethod(nameof(SaveAsync), new Type[] { typeof(string), typeof(byte[]) });
            var wrongMethodInfo = GetType().GetMethod(nameof(SaveAsync), new Type[] { typeof(FileDataDto) });

            throw new NotSupportedException($"Use {rightMethodInfo} instead of {wrongMethodInfo}");
        }
Example #3
0
        public IActionResult UploadVideo(FileDataDto fileDataDto)
        {
            var file = Request.Form.Files["FileData"];

            var id = string.Empty;

            string[] mimeTypes = new[] { "video/mp4", "video/webm" };

            if (mimeTypes.Contains(file.ContentType))
            {
                var fileData = new FileData()
                {
                    ContentType = file.ContentType,
                    Author      = User.Identities.First().Name,
                    TagsArray   = fileDataDto.Tags?.Split(DELIMITER),
                    Hero        = fileDataDto.Hero
                };

                using (var ms = new MemoryStream())
                {
                    file.CopyTo(ms);
                    var fileBytes = ms.ToArray();
                    var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
                    fileData.FileName     = parsedContentDisposition.FileName.ToString().Trim('"');
                    fileData.FileContents = fileBytes;
                }

                id = _videoService.AddAsync(fileData).Result;
            }

            return(Ok(id));
        }
        private bool IsFileDataDtoEquals(FileDataDto first, FileDataDto second)
        {
            if (first == null && second == null)
            {
                return(true);
            }

            if (first == null || second == null)
            {
                return(false);
            }

            if (first.FullName != second.FullName ||
                first.Delimiter != second.Delimiter ||
                !first.Headers.SequenceEqual(second.Headers) ||
                first.Rows.Count != second.Rows.Count)
            {
                return(false);
            }

            for (int index = 0; index < first.Rows.Count; index++)
            {
                if (!first.Rows[index].SequenceEqual(second.Rows[index]))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #5
0
        public virtual bool UploadFileData(FileDataDto vm, out FileDataParamDto fileDataParam)
        {
            bool result = false;

            fileDataParam = null;
            UserInfo user = SessionUtils.UserInfo;

            if (vm != null && user.File.OpenType == FileOpenType.Write && user.File.Stream != null)
            {
                if (vm.Data != null && vm.Data.Length > 0 && vm.Length >= vm.Data.Length)
                {
                    user.File.Stream.Seek(vm.Index, SeekOrigin.Begin);
                    user.File.Stream.Write(vm.Data, 0, vm.Length);
                    user.File.Stream.Flush();
                }

                user.File.Position = user.File.Stream.Position;
                fileDataParam      = new FileDataParamDto()
                {
                    Index = user.File.Position,
                    Size  = ConfigUtils.MAX_FILE_DATA_SIZE
                };
                result = true;
            }

            return(result);
        }
        public async Task UpdateData_InvalidDto_ThrowValidationException(FileDataDto value)
        {
            var fileService = CreateFileService(TEST_ROOT_FOLDER_PATH);

            Func <Task> act = () => fileService.UpdateData(value);

            await Assert.ThrowsExceptionAsync <ValidationException>(act);
        }
        public async Task UpdateData(FileDataDto fileData)
        {
            FileDataDtoValidator.ValidateAndThrow(fileData);

            CheckRootPath(fileData.FullName);
            var fileProcessor = GetFileProcessor(fileData.FullName);

            await fileProcessor.Update(fileData);
        }
        public async Task Update_NonexistentTextFile_ThrowFileNotFoundException()
        {
            var fileData = new FileDataDto()
            {
                FullName  = Guid.NewGuid().ToString(),
                Delimiter = " ",
            };

            Func <Task> act = () => _fileProcessor.Update(fileData);

            await Assert.ThrowsExceptionAsync <FileNotFoundException>(act);
        }
        public async Task Update_TextFile_OverwriteFileAndReturnsTask()
        {
            string tempFile = Path.Combine(Path.GetTempPath(), $"temp_file_{Guid.NewGuid().ToString()}.txt");

            File.WriteAllText(tempFile, string.Empty);

            var firstDataToWrite = new FileDataDto()
            {
                FullName  = tempFile,
                Delimiter = "\t",
                Headers   = new List <string>()
                {
                    "1", "2"
                },
                Rows = new List <string[]>()
                {
                    new string[] { "A", "B" }
                }
            };

            var secondDataToWrite = new FileDataDto()
            {
                FullName  = tempFile,
                Delimiter = "\t",
                Headers   = new List <string>()
                {
                    "COL_1", "COL_2"
                },
                Rows = new List <string[]>()
                {
                    new string[] { "A", "B" },
                    new string[] { "A1", "B1" }
                }
            };

            var parseOptions = new FileParseOptionsDto()
            {
                FullName   = tempFile,
                Delimiter  = "\t",
                HasHeaders = true
            };

            await _fileProcessor.Update(firstDataToWrite);

            await _fileProcessor.Update(secondDataToWrite);

            var resultData = await _fileProcessor.Read(parseOptions);

            File.Delete(tempFile);

            Assert.IsTrue(IsFileDataDtoEquals(resultData, secondDataToWrite));
        }
Example #10
0
        public ActionResult UploadFileData(FileDataDto vm)
        {
            if (vm != null && this.UserInfo.File.OpenType == FileOpenType.Write)
            {
                FileDataParamDto fileDataParam = null;
                var fileService = this.GetService <IFileService>();
                if (fileService.UploadFileData(vm, out fileDataParam))
                {
                    return(Success(fileDataParam));
                }
            }

            return(Error());
        }
        public async Task UpdateData_InvalidRootPath_ThrowBusinessLogicException()
        {
            var fileData = new FileDataDto()
            {
                FullName = @"C:\text.txt",
                Headers  = new List <string>()
                {
                    "test"
                }
            };
            var fileService = CreateFileService(TEST_ROOT_FOLDER_PATH);

            Func <Task> act = () => fileService.UpdateData(fileData);

            await Assert.ThrowsExceptionAsync <BusinessLogicException>(act);
        }
Example #12
0
        public async Task Update(FileDataDto fileData)
        {
            CheckFileExist(fileData.FullName);

            var strBuilder = new StringBuilder();

            strBuilder.AppendLine(string.Join(fileData.Delimiter, fileData.Headers));

            foreach (var row in fileData.Rows)
            {
                strBuilder.AppendLine(string.Join(fileData.Delimiter, row));
            }

            using (StreamWriter writer = new StreamWriter(fileData.FullName, false, Encoding.Default))
            {
                await writer.WriteAsync(strBuilder.ToString());
            }
        }
        public async Task Read_EmptyTextFile_ReturnsEmptyFileData()
        {
            var parseOptions = new FileParseOptionsDto()
            {
                FullName   = EMPTY_TEXTFILE_PATH,
                Delimiter  = " ",
                HasHeaders = false
            };

            var expectedData = new FileDataDto()
            {
                FullName  = EMPTY_TEXTFILE_PATH,
                Delimiter = " "
            };

            var resultData = await _fileProcessor.Read(parseOptions);

            Assert.IsTrue(IsFileDataDtoEquals(resultData, expectedData));
        }
        public async Task UpdateData_ValidFile_UpdateCalledOnFileProcessor()
        {
            var fileData = new FileDataDto()
            {
                FullName = $"{TEST_ROOT_FOLDER_PATH}\\text.txt",
                Headers  = new List <string>()
                {
                    "test"
                }
            };

            var fileProcFactory = new Mock <IFileProcessorFactory>();
            var fileProcessor   = new Mock <IFileProcessor>();

            fileProcFactory.Setup(x => x.Create("txt")).Returns(fileProcessor.Object);
            var fileService = CreateFileService(TEST_ROOT_FOLDER_PATH, fileProcFactory.Object);

            await fileService.UpdateData(fileData);

            fileProcessor.Verify(x => x.Update(fileData), Times.Once());
        }
Example #15
0
        public async Task <FileDataDto> Read(FileParseOptionsDto parseOptions)
        {
            CheckFileExist(parseOptions.FullName);

            var result = new FileDataDto()
            {
                FullName  = parseOptions.FullName,
                Delimiter = parseOptions.Delimiter
            };

            using (var stream = new FileStream(parseOptions.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, DefaultOptions))
                using (var reader = new StreamReader(stream, Encoding.Default))
                {
                    string line;
                    while ((line = await reader.ReadLineAsync()) != null)
                    {
                        result.Rows.Add(line.Split(new string[] { parseOptions.Delimiter }, StringSplitOptions.None));
                    }
                }

            if (result.Rows.Count == 0)
            {
                return(result);
            }

            if (parseOptions.HasHeaders)
            {
                result.Headers.AddRange(result.Rows[0]);
                result.Rows.RemoveAt(0);
            }
            else
            {
                var maxColumnsCount = result.Rows.Max(x => x.Length);
                result.Headers.AddRange(Enumerable.Range(0, maxColumnsCount).Select(x => $"col_{x}"));
            }

            return(result);
        }
Example #16
0
        public virtual FileDataDto GetFileData(FileDataParamDto vm)
        {
            FileDataDto m    = null;
            UserInfo    user = SessionUtils.UserInfo;

            if (user != null && vm != null && user.File.OpenType == FileOpenType.Read && user.File.Stream != null)
            {
                user.File.Stream.Seek(vm.Index, SeekOrigin.Begin);
                m = new FileDataDto()
                {
                    Index = vm.Index
                };
                long len = user.File.Stream.Length - user.File.Position;
                if (vm.Size > len)
                {
                    vm.Size = Convert.ToInt32(len);
                }
                m.Data   = new byte[vm.Size];
                m.Length = user.File.Stream.Read(m.Data, 0, vm.Size);
            }

            return(m);
        }
        public async Task GetData_ValidFile_ReturnsFileData()
        {
            var parseOptions = new FileParseOptionsDto()
            {
                FullName = $"{TEST_ROOT_FOLDER_PATH}\\text.txt"
            };

            var expectedData = new FileDataDto()
            {
                FullName = $"{TEST_ROOT_FOLDER_PATH}\\text.txt"
            };

            var fileProcFactory = new Mock <IFileProcessorFactory>();
            var fileProcessor   = new Mock <IFileProcessor>();

            fileProcessor.Setup(x => x.Read(parseOptions)).Returns(Task.FromResult(expectedData));
            fileProcFactory.Setup(x => x.Create("txt")).Returns(fileProcessor.Object);

            var fileService = CreateFileService(TEST_ROOT_FOLDER_PATH, fileProcFactory.Object);

            var resultFileData = await fileService.GetData(parseOptions);

            Assert.ReferenceEquals(resultFileData, expectedData);
        }
        public virtual bool GetFile(FileInfoDto m, string saveFullDirectory, string name, FilePositionCallback call = null)
        {
            bool result = false;
            bool isopen = false;

            try
            {
                if (m != null && m.Type == FileInfoType.File && !string.IsNullOrEmpty(saveFullDirectory) && this.Client.IsLogin)
                {
                    if (!Directory.Exists(saveFullDirectory))
                    {
                        Directory.CreateDirectory(saveFullDirectory);
                    }
                    if (this.Client.Host == "127.0.0.1" || this.Client.Host == "localhost")
                    {
                        result = this.GetLocalFile(m, saveFullDirectory, call);
                    }
                    else
                    {
                        long position = 0;
                        this.OnFilePositionCallback(call, m.Length, position);
                        var fs = this.CreateFile(m, saveFullDirectory, name, out position);
                        if (m.Length == position && fs == null)
                        {
                            this.OnFilePositionCallback(call, m.Length, position);
                            result = true;
                        }
                        else if (fs != null)
                        {
                            using (fs)
                            {
                                isopen = this.Client.Request <bool, int>(MsgCmd.OpenFile, m.Id);
                                if (isopen)
                                {
                                    FileDataParamDto getDto = new FileDataParamDto();
                                    while (position < m.Length)
                                    {
                                        bool isContinue = this.OnFilePositionCallback(call, m.Length, position);
                                        getDto.Index = position;
                                        getDto.Size  = MAX_FILE_DATA_SIZE;
                                        FileDataDto dataDto = this.Client.Request <FileDataDto, FileDataParamDto>(MsgCmd.GetFileData, getDto);
                                        if (dataDto != null && isContinue)
                                        {
                                            fs.Seek(position, SeekOrigin.Begin);
                                            if (dataDto.Length > 0 && dataDto.Data != null && dataDto.Length >= dataDto.Data.Length)
                                            {
                                                fs.Write(dataDto.Data, 0, dataDto.Length);
                                                fs.Flush();
                                            }
                                            position = position + dataDto.Length;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }

                                    this.Client.Request(MsgCmd.CloseFile);

                                    string saveFile  = Utils.Combine(saveFullDirectory, m.Name);
                                    string tempFile  = saveFile + FILE_TEMP_SUFFIX;
                                    string indexFile = saveFile + FILE_INDEX_SUFFIX;
                                    File.WriteAllText(indexFile, position.ToString() + " " + JsonUtils.Serialize(m), Encoding.UTF8);
                                    fs.Close();
                                    if (position >= m.Length)
                                    {
                                        this.OnFilePositionCallback(call, m.Length, position);
                                        if (File.Exists(saveFile))
                                        {
                                            File.Delete(saveFile);
                                        }
                                        File.Move(tempFile, Utils.Combine(saveFullDirectory, name));
                                        File.Delete(indexFile);
                                        result = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (isopen)
                {
                    this.Client.Request(MsgCmd.CloseFile);
                }
                LogUtils.Error("【FileInfoClient.GetFile】", ex);
            }

            return(result);
        }
        public virtual bool SendFile(string localFullFileName, string remoteDirectory, string remoteName, FilePositionCallback call = null)
        {
            bool result = false;

            try
            {
                if (!string.IsNullOrEmpty(localFullFileName) && !string.IsNullOrEmpty(remoteName) &&
                    File.Exists(localFullFileName) && this.Client.IsLogin)
                {
                    var localinfo = new FileInfo(localFullFileName);
                    this.OnFilePositionCallback(call, localinfo.Length, 0);
                    if (this.Client.Host == "127.0.0.1" || this.Client.Host == "localhost")
                    {
                        result = this.AddFileInfo(localinfo, remoteDirectory, remoteName);
                        this.OnFilePositionCallback(call, localinfo.Length, localinfo.Length);
                    }
                    else
                    {
                        using (var fs = File.OpenRead(localFullFileName))
                        {
                            long fileLength = fs.Length;
                            CreateFileParamDto createDto = new CreateFileParamDto()
                            {
                                Directory     = remoteDirectory,
                                Name          = remoteName,
                                Length        = localinfo.Length,
                                CreationTime  = localinfo.CreationTime,
                                LastWriteTime = localinfo.LastAccessTime
                            };
                            var         getDto  = this.Client.Request <FileDataParamDto, CreateFileParamDto>(MsgCmd.CreateFile, createDto);
                            FileDataDto dataDto = new FileDataDto();
                            if (getDto != null)
                            {
                                while (getDto.Index < fileLength)
                                {
                                    dataDto.Index = getDto.Index;
                                    int  size = getDto.Size;
                                    long temp = fileLength - dataDto.Index;
                                    if (temp < size)
                                    {
                                        size = Convert.ToInt32(temp);
                                    }
                                    byte[] buffer = new byte[size];
                                    fs.Seek(dataDto.Index, SeekOrigin.Begin);
                                    dataDto.Length = fs.Read(buffer, 0, buffer.Length);
                                    dataDto.Data   = buffer;
                                    getDto         = this.Client.Request <FileDataParamDto, FileDataDto>(MsgCmd.UploadFileData, dataDto);
                                    bool isContinue = this.OnFilePositionCallback(call, fileLength, dataDto.Index);
                                    if (getDto == null || !isContinue)
                                    {
                                        break;
                                    }
                                }

                                this.Client.Request(MsgCmd.CreateFileCompleted);

                                if (getDto != null && getDto.Index >= fileLength)
                                {
                                    this.OnFilePositionCallback(call, fileLength, dataDto.Index);
                                    result = true;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtils.Error("【FileInfoClient.SendFile】", ex);
            }

            return(result);
        }
 public async Task UpdateFileData([FromBody] FileDataDto fileData)
 {
     await _fileService.UpdateData(fileData);
 }