Exemplo n.º 1
0
        public async Task <IActionResult> AddPhoto(IFormFile uploadedFile)
        {
            if (uploadedFile != null)
            {
                // путь к папке Files
                string path = "/images/" + uploadedFile.FileName;
                // сохраняем файл в папку Files в каталоге wwwroot
                using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create))
                {
                    await uploadedFile.CopyToAsync(fileStream);
                }
                User user = await profileService.Find(User.Identity.Name);

                ProfileDTO profile = new ProfileDTO()
                {
                    GetUser = user
                };
                await profileService.AddPhoto(path, profile.GetUser.Profile);

                return(RedirectToAction("Profile"));
            }
            return(RedirectToAction("Index", "Home"));
        }
        public async Task <ActionResult> Upload(int?roomId, int?profileId, IFormFile file)  // If I want many files use IFormFileCollection
        {
            //host.IsDevelopment()    // Check if it is devoloment for crate other Directory for test

            if (file == null)
            {
                return(BadRequest("Null File"));
            }
            if (file.Length == 0)
            {
                return(BadRequest("Empty File"));
            }
            if (file.Length > MAX_BYTES)
            {
                return(BadRequest("Max file size exceeded"));
            }
            if (ACCEPTED_FILE_TYPE.Any(a => a == Path.GetExtension(file.FileName).ToLower()))
            {
                return(BadRequest("Invalid file type"));
            }

            if (roomId.HasValue)
            {
                // Code upload photo room
                var room = await roomService.GetRoom(roomId.Value);

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

                var photo = photoService.UploadPhoto(file, host);

                await roomService.AddPhoto(roomId.Value, photo);

                await uow.CompleteAsync();

                var photoResource = mapper.Map <Photo, PhotoResource>(photo);
                return(Ok(photoResource));
            }
            else if (profileId.HasValue)
            {
                // Code upload photo profile
                var profile = await profileService.GetProfile(profileId.Value);

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

                var photo = photoService.UploadPhoto(file, host);
                await profileService.AddPhoto(profileId.Value, photo);

                await uow.CompleteAsync();

                var photoResource = mapper.Map <Photo, PhotoResource>(photo);
                return(Ok(photoResource));
            }
            else
            {
                return(NotFound());
            }
        }