Exemplo n.º 1
0
        public async Task <IActionResult> Upload(int bookInstId, IFormFile file)
        { // IFormCollection
            var bookInst = await bookInstRepo.Get(bookInstId);

            if (bookInst == null)
            {
                return(NotFound());
            }

            if (file == null)
            {
                return(BadRequest("Null file"));
            }

            if (file.Length == 0)
            {
                return(BadRequest("Empty file"));
            }

            if (file.Length > photoSettings.MaxBytes)
            {
                return(BadRequest("You exceeded allowed maximum size (" + photoSettings.MaxBytes + " bytes)"));
            }

            if (!photoSettings.AcceptedFileTypes.Any(s => s == Path.GetExtension(file.FileName).ToLower()))
            {
                return(BadRequest("Unaccepted file type. Accepted file types are :" + string.Join(" , ", photoSettings.AcceptedFileTypes)));
            }

            var uploadFolderPath = Path.Combine(host.WebRootPath, UploadFolderName);

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

            var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
            var filePath = Path.Combine(uploadFolderPath, fileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            var photo = new BookInstPhoto()
            {
                FileName = fileName, BookInstId = bookInst.Id, BookInst = bookInst
            };

            bookInst.Photos.Add(photo);
            photoRepo.Add(photo);

            await uow.CompleteAsync();

            return(Ok(mapper.Map <BookInstPhoto, PhotoResource>(photo)));
            // System.Drawing
        }
Exemplo n.º 2
0
 public void Add(BookInstPhoto photo)
 {
     this.context.Photos.Add(photo);
 }