예제 #1
0
        public ObjectResult Post()
        {
            var results = new BaseResponse <AdImageDTO>();

            if (httpContextAccessor.HttpContext != null &&
                httpContextAccessor.HttpContext.Request != null &&
                httpContextAccessor.HttpContext.Request.Form != null &&
                httpContextAccessor.HttpContext.Request.Form.Files != null &&
                httpContextAccessor.HttpContext.Request.Form.Files.Any() &&
                httpContextAccessor.HttpContext.Request.Form.ContainsKey("guid"))

            {
                var guid = Guid.Empty;

                if (Guid.TryParse(httpContextAccessor.HttpContext.Request.Form["guid"], out guid))
                {
                    adImagePath.SetGuidProduct(guid);
                    var relative = adImagePath.RelativePath();
                    var path     = adImagePath.PhysicalPath();
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    try
                    {
                        foreach (var file in httpContextAccessor.HttpContext.Request.Form.Files)
                        {
                            var fileInfo = new FileInfo(file.FileName);

                            var guidSplit = Guid.NewGuid().ToString().Split('-');

                            var newFileName = string.Concat(guidSplit[0], fileInfo.Extension);

                            var pathOriginalFile = adImagePath.FullPhysicalPath(fileInfo.Name);

                            var relativePathFile = adImagePath.FullRelativePath(newFileName);

                            using (var fileStream = new FileStream(pathOriginalFile, FileMode.Create))
                            {
                                file.CopyTo(fileStream);
                            }

                            var imageResizeDTO = new ImageResizeDTO(pathOriginalFile, path, newFileName, 600, 600, true);

                            var resizeReturn = imageHelper.Resize(imageResizeDTO);

                            if (resizeReturn.IsOk())
                            {
                                var index = 0;
                                int.TryParse(httpContextAccessor.HttpContext.Request.Form[fileInfo.Name], out index);

                                var dto = new AdImageDTO
                                {
                                    Index       = index,
                                    GuidProduct = guid,
                                    File        = newFileName,
                                    Full        = relativePathFile,
                                    idOwner     = GetOwner()
                                };

                                var request = new BaseRequest <AdImageDTO>(dto);

                                var response = adImageService.Save(request);

                                results.Add(response.Dto);
                            }
                        }

                        base.PrepareResult <BaseResponse <AdImageDTO> >(results);
                    }
                    catch (Exception ex)
                    {
                        base.PrepareBadRequestResult <BaseResponse <AdImageDTO> >(results);
                    }
                }
                else
                {
                    base.PrepareBadRequestResult();
                }
            }
            else
            {
                base.PrepareBadRequestResult();
            }

            return(result);
        }