Пример #1
0
        public IActionResult OnPostDelete()
        {
            if (User.IsInRole("Admin"))
            {
                var product = _productRepository.GetProduct(Id);
                product.IsDeleted = true;
                var uploads = _uploadRepository.GetProductUploads(Id);
                foreach (var item in uploads)
                {
                    var path = System.IO.Path.Combine(_env.WebRootPath, "uploads", "products", Id.ToString(), item.FileName);
                    System.IO.File.Delete(path);
                    item.IsDeleted = true;
                    _uploadRepository.EditUpload(item);
                }

                var thumbnail = _uploadRepository.GetProductThumbnail(Id);
                var pathi     = System.IO.Path.Combine(_env.WebRootPath, "uploads", "products", Id.ToString(), thumbnail.FileName);
                System.IO.File.Delete(pathi);
                thumbnail.IsDeleted = true;
                _uploadRepository.EditUpload(thumbnail);
                _uploadRepository.SaveChanges();
                return(RedirectToPage("/Products/Index", new { toastr = "Product deleted successfully." }));
            }

            return(RedirectToPage("/Products/Index"));
        }
Пример #2
0
        public JsonResult CropPicture(int entityID, int uploadID, double x, double y, double width, double height)
        {
            try
            {
                var upload        = uploadRepository.GetById(uploadID);
                var entity        = entityRepository.GetById(entityID);
                var currentEntity = SessionHelper.CurrentEntity;
                var citizen       = SessionHelper.LoggedCitizen;
                var crop          = new CropRectangle((int)x, (int)y, (int)width, (int)height);

                var result = uploadAvatarService.CanCropUpload(upload, entity, currentEntity, citizen, crop);
                if (result.IsError)
                {
                    return(JsonError(result));
                }

                var newUpload = uploadAvatarService.CropUploadAndMoveToAnotherLocation(upload, crop, UploadLocationEnum.Avatars);
                entity.ImgUrl = uploadAvatarService.GetRelativePathForLocation(newUpload);

                uploadRepository.SaveChanges();
                return(JsonData(new
                {
                    msg = "Avatar changed!",
                    img = entity.ImgUrl
                }));
            }
            catch (Exception e)
            {
                return(UndefinedJsonError(e));
            }
        }
Пример #3
0
        public void AddUpload(string imageBytes, int productId, bool isEditing)
        {
            try
            {
                var path = System.IO.Path.Combine(_env.WebRootPath, "uploads", "products", productId.ToString());
                if (isEditing)
                {
                    var currentThumbnail = _uploadRepository.GetProductThumbnail(productId);
                    if (currentThumbnail != null)
                    {
                        currentThumbnail.IsDeleted = true;
                        _uploadRepository.EditUpload(currentThumbnail);
                        _uploadRepository.SaveChanges();
                        System.IO.File.Delete(System.IO.Path.Combine(path, "thumbnail.png"));
                    }
                }

                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }

                imageBytes = imageBytes.Remove(0, 22);
                byte[] image     = Convert.FromBase64String(imageBytes);
                var    imagePath = System.IO.Path.Combine(path, "thumbnail.png");
                System.IO.File.WriteAllBytes(imagePath, image);
                var userId = _userManager.GetUserId(User);
                var upload = new Upload()
                {
                    Path            = "~/uploads/products/" + productId + "/thumbnail.png",
                    FileName        = "thumbnail.png",
                    ProductId       = productId,
                    CreatedOnDate   = DateTime.Now,
                    CreatedByUserId = userId,
                    IsThumbnail     = true,
                    IsCarousel      = false,
                    IsDeleted       = false
                };
                _uploadRepository.AddUpload(upload);
                _uploadRepository.SaveChanges();
            }
            catch (Exception ex)
            {
            }
        }
Пример #4
0
        public IActionResult OnPostAddUpload(string imageBytes)
        {
            try
            {
                var upload = new Upload();
                upload.IsCarousel      = true;
                upload.IsDeleted       = false;
                upload.IsThumbnail     = false;
                upload.CreatedOnDate   = DateTime.Now;
                upload.CreatedByUserId = _userManager.GetUserId(User);
                _uploadRepository.AddUpload(upload);
                _uploadRepository.SaveChanges();

                var path = System.IO.Path.Combine(_env.WebRootPath, "uploads", "carousels");

                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                var fileName = "Banner" + upload.Id + ".png";

                imageBytes = imageBytes.Remove(0, 22);
                byte[] image = Convert.FromBase64String(imageBytes);

                var imagePath = System.IO.Path.Combine(path, fileName);
                System.IO.File.WriteAllBytes(imagePath, image);

                upload.Path     = "~/uploads/carousels/" + fileName;
                upload.FileName = fileName;

                _uploadRepository.EditUpload(upload);
                _uploadRepository.SaveChanges();

                return(RedirectToPage("/Panel/Carousel"));
            }
            catch (Exception ex)
            {
                return(RedirectToPage("/Panel/Carousel"));
            }
        }
Пример #5
0
        public IActionResult DeleteProductUpload(int productId, int uploadId)
        {
            try
            {
                var upload = _uploadRepository.GetUpload(uploadId);
                upload.IsDeleted = true;
                _uploadRepository.EditUpload(upload);
                _uploadRepository.SaveChanges();

                var path = System.IO.Path.Combine(_env.WebRootPath, "uploads", "products", productId.ToString(), upload.FileName);
                System.IO.File.Delete(path);

                return(Ok(true));
            }
            catch (Exception)
            {
                return(Ok(false));
            }
        }
Пример #6
0
        public Upload Upload(HttpPostedFileBase file, UploadLocationEnum location, Citizen citizen)
        {
            var filePath = GetUniqueFilePath(location, ".png");

            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
            file.SaveAs(filePath);

            var fileName = Path.GetFileName(filePath);
            var upload   = new Upload()
            {
                UploadedByCitizenID = citizen.ID,
                Day              = GameHelper.CurrentDay,
                Time             = DateTime.Now,
                UploadLocationID = (int)location,
                Filename         = fileName,
            };

            uploadRepository.Add(upload);
            uploadRepository.SaveChanges();

            return(upload);
        }