Exemple #1
0
        public IActionResult UploadPhotos([FromForm] string title, [FromForm] IFormFile[] files,
                                          [FromForm] string[] dates, [FromForm] string[] latitudes, [FromForm] string[] longitudes,
                                          [FromForm] string[] countries)
        {
            var maxSize           = _config.GetValue <long>("FileStorage:SizeLimit");
            var allowedExtensions = new List <string>(_config["FileStorage:AllowedExtensions"].Split(','));

            var userId = GetUserId(User);
            int length = files.Length;

            length = Math.Min(length, dates.Length);
            length = Math.Min(length, longitudes.Length);
            length = Math.Min(length, latitudes.Length);
            List <JourneyOverviewUploadViewModel> models = new List <JourneyOverviewUploadViewModel>();

            for (int i = 0; i < length; i++)
            {
                var file = files[i];
                if (file.Length > maxSize)
                {
                    var formattedMaxSize = FileUtils.ConvertBytesToMegaBytes(maxSize);
                    return(StatusCode(413,
                                      $"Single file ({file.FileName}) exceeds the limit ({formattedMaxSize} megabytes)"));
                }

                var extension = Path.GetExtension(file.FileName);
                if (string.IsNullOrEmpty(extension) || !allowedExtensions.Contains(extension))
                {
                    return(StatusCode(422,
                                      $"File extension is not whitelisted for file ({file.FileName})! only .jpg, .jpeg and .png are allowed"));
                }

                if (!FileUtils.IsValidImageSignature(file))
                {
                    return(StatusCode(422,
                                      $"Failed to match file ({file.FileName}) signature to any of known file extensions"));
                }

                try
                {
                    models.Add(new JourneyOverviewUploadViewModel(file, Convert.ToDateTime(dates[i]),
                                                                  double.Parse(latitudes[i]), double.Parse(longitudes[i])));
                }
                catch (Exception)
                {
                    return(StatusCode(422, "Failed to parse data"));
                }
            }

            List <Country> countryList = countries.Select(countryCode => _overviewRepository.GetCountry(countryCode))
                                         .Where(country => country != null).ToList();

            _overviewRepository.AddJourneyOverview(userId, title, countryList, models);
            return(Ok("Photos have been uploaded successfully"));
        }
 public void TestGetCountryNull()
 {
     Assert.Null(_repository.GetCountry("LT"));
 }