예제 #1
0
        public void TestAddJourneyOverview()
        {
            var countries = new List <Country> {
                france
            };
            var file1 = new Mock <IFormFile>();

            file1.Setup(x => x.FileName).Returns("file1.jpg");
            file1.Setup(x => x.Length).Returns(100);

            var file2 = new Mock <IFormFile>();

            file2.Setup(x => x.FileName).Returns("file2.jpg");
            file2.Setup(x => x.Length).Returns(0);
            var model1 = new JourneyOverviewUploadViewModel(file1.Object, DateTime.Now, 0, 0);
            var model2 = new JourneyOverviewUploadViewModel(file2.Object, DateTime.Now, 0, 0);
            var models = new List <JourneyOverviewUploadViewModel> {
                model1, model2
            };

            _repository.AddJourneyOverview("1", "Paris", countries, models);
            var journeys = _repository.GetJourneyOverviews("1", 0);

            Assert.AreEqual(2, journeys.Count);
        }
예제 #2
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"));
        }