Example #1
0
        public async Task <IActionResult> Edit(string id, ProductEditViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var product = await _db.ProductRepository.GetAsync(id);

                if (product != null)
                {
                    string url = product.PhotoUrl;
                    if (viewModel.File.IsImage())
                    {
                        PersianCalendar pc           = new PersianCalendar();
                        var             uploadResult = await _uploadService.UploadFile(viewModel.File, string.Format("{0}://{1}{2}", Request.Scheme, Request.Host.Value, Request.PathBase.Value), "Products\\" + pc.GetYear(DateTime.Now) + "\\" + pc.GetMonth(DateTime.Now) + "\\" + pc.GetDayOfMonth(DateTime.Now));

                        if (uploadResult.Status)
                        {
                            try
                            {
                                _uploadService.RemoveFileFromLocal(_utilities.FindLocalPathFromUrl(url));
                            }
                            catch { }

                            url = uploadResult.Url;
                        }
                        else
                        {
                            ModelState.AddModelError("File", "خطا در آپلود تصویر محصول");
                            var categories = await _db.CategoryRepository.GetAsync();

                            ViewData["CategoryId"] = new SelectList(categories, "Id", "Name", viewModel.CategoryId);
                            return(View(viewModel));
                        }
                    }

                    _mapper.Map(viewModel, product);
                    product.PhotoUrl = url;
                    _db.ProductRepository.Update(product);
                    await _db.SaveAsync();

                    return(Redirect("/Admin/Product"));
                }
                else
                {
                    return(NotFound());
                }
            }
            else
            {
                var categories = await _db.CategoryRepository.GetAsync();

                ViewData["CategoryId"] = new SelectList(categories, "Id", "Name", viewModel.CategoryId);

                return(View(viewModel));
            }
        }
Example #2
0
        public async Task <IActionResult> EditProfile(ProfileEditViewModel viewModel)
        {
            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            viewModel.Email = user.Email;
            if (ModelState.IsValid)
            {
                string url = user.PhotoUrl;
                if (viewModel.File.IsImage())
                {
                    var uploadResult = await _uploadService.UploadFile(viewModel.File, string.Format("{0}://{1}{2}", Request.Scheme, Request.Host.Value, Request.PathBase.Value), "Users\\" + user.Id + "\\Profile");

                    if (uploadResult.Status)
                    {
                        if (url != "/images/site/default.png")
                        {
                            try
                            {
                                _uploadService.RemoveFileFromLocal(_utilities.FindLocalPathFromUrl(url));
                            }
                            catch { }
                        }

                        url = uploadResult.Url;
                    }
                    else
                    {
                        ModelState.AddModelError("File", "خطا در آپلود تصویر پروفایل");
                        return(View(viewModel));
                    }
                }

                user.FullName = viewModel.FullName;
                user.PhotoUrl = url;
                _db.UserRepository.Update(user);
                await _db.SaveAsync();

                return(Redirect("/Panel"));
            }
            else
            {
                return(View(viewModel));
            }
        }
        public async Task <IActionResult> DeleteBlogImage(BlogImgDeletingDto blogImgDeletingDto)
        {
            var deleteRes = _uploadService.RemoveFileFromLocal(
                Path.GetFileName(blogImgDeletingDto.ImgUrl),
                _env.WebRootPath,
                _utilities.FindLocalPathFromUrl(blogImgDeletingDto.ImgUrl).Replace("wwwroot\\", ""));

            if (deleteRes.Status)
            {
                return(Ok());
            }
            else
            {
                return(BadRequest(new CkEditorUploadImgReaturnMesssage
                {
                    uploaded = false
                }));
            }
        }
Example #4
0
        public async Task FindLocalPathFromUrl_Success()
        {
            //Arrange------------------------------------------------------------------------------------------------------------------------------
            var str  = _utilities.FindLocalPathFromUrl("www.ali.com/ali/sdf/asd/asd.png");
            var str1 = _utilities.FindLocalPathFromUrl("ali.com/ali/sdf/asd/asd.png");
            var str2 = _utilities.FindLocalPathFromUrl("https://www.ali.com/ali/sdf/asd/asd.png");
            var str3 = _utilities.FindLocalPathFromUrl("https://ali.com/ali/sdf/asd/asd.png");
            var str4 = _utilities.FindLocalPathFromUrl("http://www.ali.com/ali/sdf/asd/asd.png");
            var str5 = _utilities.FindLocalPathFromUrl("http://ali.com/ali/sdf/asd/asd.png");

            //Act----------------------------------------------------------------------------------------------------------------------------------
            var result1 = _utilities.FindLocalPathFromUrl(str1);
            var result2 = _utilities.FindLocalPathFromUrl(str2);
            var result3 = _utilities.FindLocalPathFromUrl(str3);
            var result4 = _utilities.FindLocalPathFromUrl(str4);
            var result5 = _utilities.FindLocalPathFromUrl(str5);

            //Assert-------------------------------------------------------------------------------------------------------------------------------
            Assert.NotNull(result1);
            Assert.NotNull(result2);
            Assert.NotNull(result3);
            Assert.NotNull(result4);
            Assert.NotNull(result5);

            Assert.Equal("ali.com/ali/sdf/asd", result1);
            Assert.Equal("ali.com/ali/sdf/asd", result2);
            Assert.Equal("ali.com/ali/sdf/asd", result3);
            Assert.Equal("ali.com/ali/sdf/asd", result4);
            Assert.Equal("ali.com/ali/sdf/asd", result5);
        }
        public async Task <IActionResult> ChangeUserPhoto(string userId, [FromForm] PhotoForProfileDto photoForProfileDto)
        {
            //var userFromRepo = await _db.UserRepository.GetByIdAsync(userId);

            // var uplaodRes = _uploadService.UploadToCloudinary(photoForProfileDto.File);

            var uplaodRes = await _uploadService.UploadFile(
                photoForProfileDto.File,
                userId,
                _env.WebRootPath,
                $"{Request.Scheme ?? ""}://{Request.Host.Value ?? ""}{Request.PathBase.Value ?? ""}",
                "Files\\Pic\\Profile\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day
                );

            if (uplaodRes.Status)
            {
                photoForProfileDto.Url = uplaodRes.Url;
                if (uplaodRes.LocalUploaded)
                {
                    photoForProfileDto.PublicId = "1";
                }
                else
                {
                    photoForProfileDto.PublicId = uplaodRes.PublicId;
                }


                var oldphoto = await _db.PhotoRepository.GetAsync(p => p.UserId == userId && p.IsMain);

                if (oldphoto.PublicId != "2")
                {
                    if (oldphoto.PublicId != null && oldphoto.PublicId != "0" && oldphoto.PublicId != "1")
                    {
                        _uploadService.RemoveFileFromCloudinary(oldphoto.PublicId);
                    }
                    if (oldphoto.PublicId == photoForProfileDto.PublicId && photoForProfileDto.Url.Split('/').Last() != oldphoto.Url.Split('/').Last())
                    {
                        _uploadService.RemoveFileFromLocal(
                            oldphoto.Url.Split('/').Last(),
                            _env.WebRootPath,
                            _utilities.FindLocalPathFromUrl(oldphoto.Url).Replace("wwwroot\\", ""));
                    }
                    if (oldphoto.PublicId == "1" && photoForProfileDto.PublicId != "1")
                    {
                        _uploadService.RemoveFileFromLocal(
                            oldphoto.Url.Split('/').Last(),
                            _env.WebRootPath,
                            _utilities.FindLocalPathFromUrl(oldphoto.Url).Replace("wwwroot\\", ""));
                    }
                }

                _mapper.Map(photoForProfileDto, oldphoto);

                _db.PhotoRepository.Update(oldphoto);

                if (await _db.SaveAsync())
                {
                    var photoForReturn = _mapper.Map <PhotoForReturnProfileDto>(oldphoto);
                    return(CreatedAtRoute("GetPhoto", routeValues: new { v = HttpContext.GetRequestedApiVersion().ToString(), id = oldphoto.Id }, value: photoForReturn));
                }
                else
                {
                    return(BadRequest("خطایی در اپلود دوباره امتحان کنید"));
                }
            }
            else
            {
                return(BadRequest(uplaodRes.Message));
            }
        }