Example #1
0
        public async Task <IActionResult> NewEar([Required] string name, [Required] IFormFile picture)
        {
            Ensure.NotNull(name, "name");
            Ensure.NotNull(picture, "picture");

            string fileExtension = Path.GetExtension(picture.FileName);

            if (picture.Length > storage.MaxLength || !storage.SupportedExtensions.Contains(fileExtension))
            {
                ShowMessage("Nahraný soubor neodpovídá normám.", "danger");
                return(View());
            }

            if (await repository.IsExistingEarAsync(name))
            {
                ShowMessage("Bohužel takto pojmenované ouško již máme.", "danger");
                return(View());
            }

            AccountModel owner = await repository.FindAccountAsync(User.Identity.Name);

            Guid earId = await repository.AddEarAsync(owner, name);

            string fileName = earId.ToString() + fileExtension;
            string filePath = Path.Combine(storage.Path, fileName);

            using (Stream fileContent = new FileStream(filePath, FileMode.OpenOrCreate))
                using (Stream content = picture.OpenReadStream())
                    await content.CopyToAsync(fileContent);

            await repository.SetEarFileNameAsync(earId, fileName);

            await repository.SaveChangesAsync();

            ViewData["Message"]     = "Nové ouško úspěšně nahráno!";
            ViewData["MessageType"] = "success";
            return(View());
        }