Esempio n. 1
0
        public static FileStatus IsValid(this IFormFile file, string[] acceptedFileTypes)
        {
            if (file == null)
            {
                return(FileStatus.Null);
            }

            if (file.Length <= 0)
            {
                return(FileStatus.Empty);
            }

            if (string.IsNullOrWhiteSpace(file.Name))
            {
                return(FileStatus.Null);
            }

            if (!FileValidator.IsSupported(file.Name, acceptedFileTypes))
            {
                return(FileStatus.InvalidType);
            }

            return(FileStatus.Valid);
        }
Esempio n. 2
0
        public async Task <IActionResult> Upload(IFormFile file) // I should use other of uploading files instead
        {
            // e.g wwwroot/uploads (will be created if it didn't exist)
            var uploadPath = Path.Combine(host.WebRootPath, "uploads");

            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }

            // Checks
            if (file == null)
            {
                return(BadRequest("Null File"));
            }
            if (file.Length <= 0)
            {
                return(BadRequest("Empty File"));
            }
            if (!FileValidator.IsSupported(appSettings.AcceptedFileTypes, file.FileName))
            {
                return(BadRequest("Invalid File Type"));
            }

            //Generate a new file name in orde r to protect from hackers, e.g
            // var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
            var fileName = Path.GetFileName(file.FileName);
            // Generate an upload file path
            var filePath = Path.Combine(uploadPath, fileName);

            // Store file to file system
            // TODO: log Error Cannot save file ...
            using (var stream = new FileStream(filePath, FileMode.Create)) {
                await file.CopyToAsync(stream);
            }

            // Get Data from file
            // TODO: log Error Invalid File or Data Corrupted
            ICollection <BNKSEEKEntity> uploaded = await GetDataAsync <BNKSEEKEntity>(
                filePath,
                // Mapper Configuration
                new MapperConfiguration(cfg => { cfg.AddProfile(new DataTableToBNKSEEKProfile()); }
                                        ));

            if (uploaded.Any())
            {
                // NOTE: Didn't work for me!
                // Update entities with AutoMapper in DB Context REF: https://visualstudiomagazine.com/blogs/tool-tracker/2013/11/updating--entities-with-automapper.aspx
                // var updated = mapper.Map(bnkSeekRecords, context.BNKSEEKRecords);

                var source = await repository.GetAllAsync(true);

                var updated = mapper.Map <ICollection <BNKSEEKEntity>, ICollection <BNKSEEKEntity> >(
                    source: uploaded,
                    destination: source
                    );

                // Trying to Add records to DataBase
                try
                {
                    repository.AddAsync(uploaded);
                    await unitOfWork.CompleteAsync();
                }
                catch (Exception)
                {
                    // Handling exception...
                    // TODO: log DataBase save exception
                    throw;
                }
            }

            // Send Http status code 200
            return(Ok());
        }