public void Validate_CorrectFilePath_ValidateCorrectly()
        {
            var filePath      = "..\\..\\..\\Resources\\data.csv";
            var fileValidator = new FileValidator();

            Assert.IsTrue(fileValidator.Validate(filePath));
        }
Exemplo n.º 2
0
        public FileValidationError ValidateFile()
        {
            FileValidationError error = FileValidator.Validate(this.FileName,
                                                               SupportedWordFileExtension);

            return(error);
        }
Exemplo n.º 3
0
        public TextEditor(FileInfo file, int linesPerPage)
        {
            LinesCountValidator.Validate(linesPerPage);
            FileValidator.Validate(file);

            _file         = file;
            _linesPerPage = linesPerPage;
            _textBlock    = new TextBlock(linesPerPage);
        }
Exemplo n.º 4
0
        public bool Validate(string rootdirectory, out List <string> missingfiles)
        {
            missingfiles = new List <string>();

            bool result = true;

            result &= FileIncludeValidator.Validate(this, rootdirectory, ref missingfiles);

            return(result);
        }
Exemplo n.º 5
0
        public FileValidationError ValidateFile()
        {
            FileValidationError error = FileValidator.Validate(this.FileName,
                                                               new List <string>()
            {
                ".csv"
            });

            return(error);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Validates the files.
        /// </summary>
        /// <param name="newFiles">The new files.</param>
        private List <ValidationResult> ValidateFiles(IEnumerable <RQModel.FileViewModel> newFiles)
        {
            // Validate provided data contract. If validation errors are found
            // then inform user, else continue
            List <ValidationResult> results = new List <ValidationResult>();

            foreach (var newFile in newFiles)
            {
                if (newFile.Video == true)
                {
                    results.Add(fileValidator.Validate(newFile, ruleSet: "default,Video"));
                }
                else
                {
                    results.Add(fileValidator.Validate(newFile, ruleSet: "NonVideo"));
                }
            }

            return(results);
        }
Exemplo n.º 7
0
        public override async Task <string> ReadAsync()
        {
            var(isValid, validationMsg) = FileValidator
                                          .Validate(FileType.Text, FilePath);

            if (!isValid)
            {
                throw new Exception(validationMsg);
            }

            return(await File.ReadAllTextAsync(FilePath));
        }
Exemplo n.º 8
0
        public void Validate_UnexistingFile_ShouldReturnFalseAndValidationMsg()
        {
            // Arrange
            const string filePath = @"C:\Nowhere";
            var          expectedValidationMsg = $"The file '{filePath}' could not be found.";
            var          sut = new FileValidator();

            // Act
            var(isValid, validationMsg) = sut.Validate(FileType.Text, filePath);

            // Assert
            Assert.False(isValid);
            Assert.Equal(expectedValidationMsg, validationMsg);
        }
Exemplo n.º 9
0
        public void Validate_EmptyFilePath_ShouldReturnFalseAndValidationMsg()
        {
            // Arrange
            const string filePath = @"";
            const string expectedValidationMsg = "No file path provided";
            var          sut = new FileValidator();

            // Act
            var(isValid, validationMsg) = sut.Validate(FileType.Text, filePath);

            // Assert
            Assert.False(isValid);
            Assert.Equal(expectedValidationMsg, validationMsg);
        }
Exemplo n.º 10
0
        public void Validate_ValidFile_ShouldReturnTrueAndNoValidationMsg(int fileType, string fileName)
        {
            // Arrange
            var dir      = TestHelper.GetFilesDirectory();
            var filePath = Path.Combine(dir, fileName);
            var type     = (FileType)fileType;
            var sut      = new FileValidator();

            // Act
            var(isValid, validationMsg) = sut.Validate(type, filePath);

            // Assert
            Assert.True(isValid);
            Assert.Null(validationMsg);
        }
Exemplo n.º 11
0
        public void Validate_InvalidFileExtensions_ShouldReturnFalseAndValidationMsg()
        {
            // Arrange
            var dir      = TestHelper.GetFilesDirectory();
            var filePath = Path.Combine(dir, "javascriptfile.js");
            var expectedValidationMsg = $"The file '{filePath}' is not a valid {FileType.Text.ToString()} file.";
            var sut = new FileValidator();

            // Act
            var(isValid, validationMsg) = sut.Validate(FileType.Text, filePath);

            // Assert
            Assert.False(isValid);
            Assert.Equal(expectedValidationMsg, validationMsg);
        }
        public void AddsErrorWhenEmptyData()
        {
            // Arrange
            var file = new FileModel {
                Id = 1, Name = "file.csv"
            };
            var records = new List <YearlyData>();

            // Act
            var result = _target.Validate(file, records);

            // Assert
            Assert.That(result.Errors.Count, Is.EqualTo(1));
            Assert.That(result.Errors[0], Is.EqualTo("No data found in file."));
        }
Exemplo n.º 13
0
        public override async Task <string> ReadAsync()
        {
            var(isValid, validationMsg) = FileValidator
                                          .Validate(FileType.Json, FilePath);

            if (!isValid)
            {
                throw new Exception(validationMsg);
            }

            // todo: the code below is disabled temporary, it causes multi-threading issues in the WPF client.
            // todo: temp solution is to use the File.ReadAllTextAsync().
            //using var reader = new StreamReader(FilePath);
            //return await reader.ReadToEndAsync();

            return(await File.ReadAllTextAsync(FilePath));
        }
Exemplo n.º 14
0
        public override async Task <string> ReadAsync()
        {
            var(isValid, validationMsg) = FileValidator
                                          .Validate(FileType.Xml, FilePath);

            if (!isValid)
            {
                throw new Exception(validationMsg);
            }

            if (Strict)
            {
                // todo: the code below is disabled temporary, it causes multi-threading issues in the WPF client.
                // todo: temp solution is to use the File.ReadAllTextAsync() outside the Strict block.
                //var xmlDoc = new XmlDocument();
                //await Task.Run(() => xmlDoc.Load(FilePath));
                //return xmlDoc.InnerXml;
            }

            // Read XML file with invalid content (e.g. encrypted content)
            return(await File.ReadAllTextAsync(FilePath));
        }
Exemplo n.º 15
0
        /// <summary>
        /// Validate the provided file
        /// </summary>
        /// <param name="file">File</param>
        private void ValidateFile(HttpPostedFileBase file)
        {
            var fileValidator = new FileValidator(Errors);

            fileValidator.Validate(file);
        }
Exemplo n.º 16
0
        public async Task <(bool Success, UploadFileResult Result, string ErrorMessage)> Process(IQueryCollection query, IFormCollection form, IHeaderDictionary headers, Stream fileStream, string extensionName, string localFileName,
                                                                                                 string sectionName, HttpRequest request)
        {
            if (!headers.TryGetValue(ConstConfigs.FileMd5HeaderKey, out var md5))
            {
                return(false, null, $"未找到Header key:{ConstConfigs.FileMd5HeaderKey}.");
            }

            if (string.IsNullOrWhiteSpace(md5))
            {
                return(false, null, "文件MD5值不能为空.");
            }

            if (md5.ToString().Length != 32)
            {
                return(false, null, "不合法的MD5值.");
            }

            var chunk = 0;

            if (headers.TryGetValue(ConstConfigs.ChunkHeaderKey, out var chunkValue))
            {
                if (string.IsNullOrWhiteSpace(chunkValue))
                {
                    return(false, null, "分片索引值不能为空");
                }
                chunk = int.Parse(chunkValue);
            }

            PartUploadRecording PartUploadRecording;

            byte[] signature = null;
            //只验证第一个分片
            if (chunk == 0)
            {
                var subDir = await SubdirectoryGenerator.Generate(query, form, headers, extensionName, request);

                var folder = Path.Combine(Configure.RootDirectory, subDir);

                var fileName = await FileNameGenerator.Generate(query, form, headers, extensionName, request) + extensionName;

                var url = Path.Combine(folder, fileName).Replace("\\", "/");

                var res = Client.InitMultipartUpload(
                    new COSXML.Model.Object.InitMultipartUploadRequest(Configure.Bucket, url));
                if (res.httpCode != 200)
                {
                    return(false, null, res.httpMessage);
                }

                PartUploadRecording = new PartUploadRecording
                {
                    UploadId      = res.initMultipartUpload.uploadId,
                    Key           = url,
                    LocalFileName = localFileName
                };

                MemoryCache.Set(md5, PartUploadRecording, TimeSpan.FromHours(2));
                var(success, errorMsg, fileSignature) = await FileValidator.Validate(localFileName, fileStream);

                if (!success)
                {
                    return(false, null, errorMsg);
                }
                signature = fileSignature;
            }
            else
            {
                if (!MemoryCache.TryGetValue(md5, out PartUploadRecording))
                {
                    return(false, null, "分片记录丢失");
                }
            }



            await using var stream = new MemoryStream();
            if (signature != null && signature.Length > 0)
            {
                stream.Write(signature, 0, signature.Length);
            }

            await fileStream.CopyToAsync(stream, Configure.BufferSize);

            stream.Seek(0, SeekOrigin.Begin);

            var fileBytes = new byte[stream.Length];
            await stream.ReadAsync(fileBytes);

            try
            {
                var resp = Client.UploadPart(new COSXML.Model.Object.UploadPartRequest(Configure.Bucket, PartUploadRecording.Key, chunk + 1, PartUploadRecording.UploadId, fileBytes));
                if (resp.httpCode != 200)
                {
                    return(false, null, resp.httpMessage);
                }

                PartUploadRecording.PartETag[chunk + 1] = resp.eTag;
                return(true, new UploadFileResult {
                    Name = sectionName, Url = ""
                }, "");
            }
            catch (CosServerException e)
            {
                return(false, null, $"ErrorCode:{e.errorCode};ErrorMessage:{e.errorMessage}");
            }
        }
Exemplo n.º 17
0
 public void JpegFileIsValid()
 {
     Assert.That(_validator.Validate(@"images\test.jpg"));
 }
Exemplo n.º 18
0
        public async Task <(bool Success, UploadFileResult Result, string ErrorMessage)> Process(IQueryCollection query, IFormCollection form, IHeaderDictionary headers, Stream fileStream, string extensionName, string localFileName,
                                                                                                 string sectionName, HttpRequest request)
        {
            if (!headers.TryGetValue(ConstConfigs.FileMd5HeaderKey, out var md5))
            {
                return(false, null, $"未找到Header key:{ConstConfigs.FileMd5HeaderKey}.");
            }

            if (string.IsNullOrWhiteSpace(md5))
            {
                return(false, null, "文件MD5值不能为空.");
            }

            if (md5.ToString().Length != 32)
            {
                return(false, null, "不合法的MD5值.");
            }

            var chunk = 0;

            if (headers.TryGetValue(ConstConfigs.ChunkHeaderKey, out var chunkValue))
            {
                if (string.IsNullOrWhiteSpace(chunkValue))
                {
                    return(false, null, "分片索引值不能为空");
                }
                chunk = int.Parse(chunkValue);
            }

            PartUploadRecording PartUploadRecording;

            byte[] signature = null;
            //只验证第一个分片
            if (chunk == 0)
            {
                var subDir = await SubdirectoryGenerator.Generate(query, form, headers, extensionName, request);

                var folder = Path.Combine(Configure.RootDirectory, subDir);

                var fileName = await FileNameGenerator.Generate(query, form, headers, extensionName, request) + extensionName;

                var url = Path.Combine(folder, fileName).Replace("\\", "/");

                if (!Configure.Metadata.TryGetValue(extensionName, out var meta))
                {
                    meta = new ObjectMetadata {
                        ContentType = "application/octet-stream"
                    };
                }

                var res = Client.InitiateMultipartUpload(new InitiateMultipartUploadRequest(Configure.BucketName, url)
                {
                    ObjectMetadata = meta
                });
                if (res.HttpStatusCode != System.Net.HttpStatusCode.OK)
                {
                    return(false, null, res.HttpStatusCode.ToString());
                }

                PartUploadRecording = new PartUploadRecording
                {
                    LocalFileName = localFileName,
                    UploadId      = res.UploadId,
                    Key           = url
                };

                MemoryCache.Set(md5, PartUploadRecording, TimeSpan.FromHours(2));
                var(success, errorMsg, fileSignature) = await FileValidator.Validate(localFileName, fileStream);

                if (!success)
                {
                    return(false, null, errorMsg);
                }
                signature = fileSignature;
            }
            else
            {
                if (!MemoryCache.TryGetValue(md5, out PartUploadRecording))
                {
                    return(false, null, "分片记录丢失");
                }
            }



            await using var stream = new MemoryStream();
            if (signature != null && signature.Length > 0)
            {
                stream.Write(signature, 0, signature.Length);
            }

            await fileStream.CopyToAsync(stream, Configure.BufferSize);

            stream.Seek(0, SeekOrigin.Begin);

            try
            {
                var resp = Client.UploadPart(new UploadPartRequest(Configure.BucketName, PartUploadRecording.Key, PartUploadRecording.UploadId)
                {
                    InputStream = stream,
                    PartSize    = stream.Length,
                    PartNumber  = chunk + 1
                });
                if (resp.HttpStatusCode != System.Net.HttpStatusCode.OK)
                {
                    return(false, null, resp.HttpStatusCode.ToString());
                }

                PartUploadRecording.PartETag.Add(resp.PartETag);
                return(true, new UploadFileResult {
                    Name = sectionName, Url = ""
                }, "");
            }
            catch (Exception e)
            {
                return(false, null, e.Message);
            }
        }
        public void Validate_IncorrectFilePath_ThrowException(string filePath)
        {
            var fileValidator = new FileValidator();

            Assert.ThrowsException <FileNotFoundException>(() => fileValidator.Validate(filePath));
        }