예제 #1
0
        private async Task SaveProductMedias(ProductForm model, Product product)
        {
            if (model.ThumbnailImage != null)
            {
                var fileName = await SaveFile(model.ThumbnailImage);

                product.ThumbnailImage = new Media {
                    FileName = fileName
                };
            }

            // Currently model binder cannot map the collection of file productImages[0], productImages[1]
            foreach (var file in Request.Form.Files)
            {
                if (file.ContentDisposition.Contains("productImages"))
                {
                    model.ProductImages.Add(file);
                }
                else if (file.ContentDisposition.Contains("productDocuments"))
                {
                    model.ProductDocuments.Add(file);
                }
            }

            foreach (var file in model.ProductImages)
            {
                var fileName = await SaveFile(file);

                var productMedia = new ProductMedia
                {
                    Product = product,
                    Media   = new Media {
                        FileName = fileName, MediaType = MediaType.Image
                    }
                };
                product.AddMedia(productMedia);
            }

            foreach (var file in model.ProductDocuments)
            {
                var fileName = await SaveFile(file);

                var productMedia = new ProductMedia
                {
                    Product = product,
                    Media   = new Media {
                        FileName = fileName, MediaType = MediaType.File, Caption = file.FileName
                    }
                };
                product.AddMedia(productMedia);
            }

            foreach (var productMediaId in model.Product.DeletedMediaIds)
            {
                var productMedia = product.Medias.First(x => x.Id == productMediaId);
                await _mediaService.DeleteMediaAsync(productMedia.Media);

                product.RemoveMedia(productMedia);
            }
        }
예제 #2
0
        public ActionResult Edit(Product prod, int[] ListMedia, decimal priceEditor)
        {
            try
            {
                Product prd = dc.Products.Find(prod.ProductID);

                prd.Prices.FirstOrDefault().PriceValue = priceEditor;


                prd.ProductMedias.Clear();

                dc.Entry <Product>(prd).CurrentValues.SetValues(prod);

                foreach (int id in ListMedia)
                {
                    ProductMedia pm   = new ProductMedia();
                    Medium       medi = dc.Media.Find(id);
                    pm.Medium    = medi;
                    pm.MediaID   = medi.MediaID;
                    pm.ProductID = prd.ProductID;
                    pm.Product   = prd;

                    prd.ProductMedias.Add(pm);
                }
                dc.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                throw e;
            }
            return(RedirectToAction("Index", "Home"));
        }
예제 #3
0
        public static string ConvertFileToBase64String(ProductMedia productMedia, MediaType mediaType)
        {
            var fullFileName = String.Join(".", productMedia.FileName, productMedia.FileExtension);

            Byte[] bytes = System.IO.File.ReadAllBytes(Path.Combine(GetTruePath(mediaType), fullFileName));
            return(Convert.ToBase64String(bytes));
        }
        public async Task <IActionResult> UploadFile([FromForm] ProductMediaModel productMediaModel)
        {
            var requestForm = Request.Form;
            var file        = requestForm.Files[0];
            var result      = new Result <string>();
            var fileName    = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
            var newFileName = $@"{Int32.Parse(productMediaModel.ProdId)}-{fileName}";

            try
            {
                // add image
                bool isStoreSuccess = await StoreImage("ProductImages", newFileName, file);

                if (!isStoreSuccess)
                {
                    throw new Exception("Store image locally failed.");
                }

                //add image name to db
                ProductMedia productMedia = new ProductMedia {
                    ProdId = Int32.Parse(productMediaModel.ProdId), Url = $@"Images/ProductImages/{newFileName}"
                };
                await _context.ProductMedia.AddAsync(productMedia);

                await _context.SaveChangesAsync();

                result.Data = $@"{fileName} successfully uploaded";
            }
            catch (Exception e)
            {
                result.ErrorMessage = e.Message;
                return(BadRequest(result));
            }
            return(Ok(result));
        }
예제 #5
0
        private async Task AddMediaInfo(CreateProductCommand request, Product product)
        {
            if (request.ThumbnailImageUrl != null)
            {
                var thumbnailImageFileName = await SaveFile(request.ThumbnailImage);

                if (product.ThumbnailImage != null)
                {
                    product.ThumbnailImage.FileName = thumbnailImageFileName;
                }
                else
                {
                    product.ThumbnailImage = new Media {
                        FileName = thumbnailImageFileName
                    };
                }
            }

            foreach (var image in request.NewImages)
            {
                var fileName = await SaveFile(image);

                var productMedia = new ProductMedia
                {
                    Product = product,
                    Media   = new Media {
                        FileName = fileName, MediaType = MediaType.Image
                    }
                };

                product.AddMedia(productMedia);
            }
        }
예제 #6
0
        public Error InsertOrUpdateProductMedia(ProductMediaModel prodMedia, UserModel user, string lockGuid)
        {
            Error error = validateModel(prodMedia);

            if (!error.IsError)
            {
                // Check that the lock is still current
                if (!db.IsLockStillValid(typeof(Product).ToString(), prodMedia.Id, lockGuid))
                {
                    error.SetError(EvolutionResources.errRecordChangedByAnotherUser, "");
                }
                else
                {
                    // Save the product media
                    ProductMedia temp = null;
                    if (prodMedia.Id != 0)
                    {
                        temp = db.FindProductMedia(prodMedia.Id);
                    }
                    if (temp == null)
                    {
                        temp = new ProductMedia();
                    }

                    Mapper.Map <ProductMediaModel, ProductMedia>(prodMedia, temp);

                    db.InsertOrUpdateProductMedia(temp);
                    prodMedia.Id = temp.Id;
                }
            }
            return(error);
        }
예제 #7
0
        public IHttpActionResult PutProductMedia(int id, ProductMedia productMedia)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != productMedia.Id)
            {
                return(BadRequest());
            }

            db.Entry(productMedia).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductMediaExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
예제 #8
0
        private async Task MapProductVariantImageFromVm(ProductVariationVm variationVm, Product product)
        {
            if (variationVm.ThumbnailImage != null)
            {
                var thumbnailImageFileName = await SaveFile(variationVm.ThumbnailImage);

                if (product.ThumbnailImage != null)
                {
                    product.ThumbnailImage.FileName = thumbnailImageFileName;
                }
                else
                {
                    product.ThumbnailImage = new Media {
                        FileName = thumbnailImageFileName
                    };
                }
            }

            foreach (var image in variationVm.NewImages)
            {
                var fileName = await SaveFile(image);

                var productMedia = new ProductMedia
                {
                    Product = product,
                    Media   = new Media {
                        FileName = fileName, MediaType = MediaType.Image
                    }
                };

                product.AddMedia(productMedia);
            }
        }
예제 #9
0
        public async Task <ActionResult> UploadProductMedium(Guid productId, [FromQuery] ProductMediaParameter parameter, List <IFormFile> files)
        {
            if (files is null || parameter is null)
            {
                return(BadRequest("File is NULL or parameter is NULL"));
            }

            long size = files.Sum(x => x.Length);

            foreach (var file in files)
            {
                if (file.Length > 0)
                {
                    var fileName       = DateTime.UtcNow.ToLocalTime().ToString("yyyyMMddHHmmssffff");
                    var fileExtension  = Path.GetExtension(file.FileName).Substring(1);
                    var fileNameToSave = String.Join(".", fileName, fileExtension);

                    var mediaInformationToDB = new ProductMedia()
                    {
                        FileName      = fileName,
                        FileExtension = fileExtension,
                        ProductId     = productId,
                        Type          = parameter.Type
                    };

                    _productMediaService.CreateProductMedia(mediaInformationToDB);

                    await file.SaveFileAsync(parameter.Type, fileNameToSave);
                }
            }

            await _productMediaService.SaveAsync();

            return(Ok(new { count = files.Count, size }));
        }
예제 #10
0
        private void SaveProductMedias(ProductForm model, Product product)
        {
            if (model.ThumbnailImage != null)
            {
                var fileName = SaveFile(model.ThumbnailImage);
                if (product.ThumbnailImage != null)
                {
                    product.ThumbnailImage.FileName = fileName;
                }
                else
                {
                    product.ThumbnailImage = new Media {
                        FileName = fileName
                    };
                }
            }

            // Currently model binder cannot map the collection of file productImages[0], productImages[1]
            foreach (var file in Request.Form.Files)
            {
                if (file.ContentDisposition.Contains("productImages"))
                {
                    model.ProductImages.Add(file);
                }
                else if (file.ContentDisposition.Contains("productDocuments"))
                {
                    model.ProductDocuments.Add(file);
                }
            }

            foreach (var file in model.ProductImages)
            {
                var fileName     = SaveFile(file);
                var productMedia = new ProductMedia
                {
                    Product = product,
                    Media   = new Media {
                        FileName = fileName, MediaType = MediaType.Image
                    }
                };
                product.AddMedia(productMedia);
            }

            foreach (var file in model.ProductDocuments)
            {
                var fileName     = SaveFile(file);
                var productMedia = new ProductMedia
                {
                    Product = product,
                    Media   = new Media {
                        FileName = fileName, MediaType = MediaType.File, Caption = file.FileName
                    }
                };
                product.AddMedia(productMedia);
            }
        }
        public async Task <ActionResult> DeleteProductMedia(int productId, int mediaId)
        {
            ProductMedia productMedia = await unitOfWork.ProductMedia.Get(x => x.ProductId == productId && x.MediaId == mediaId);

            unitOfWork.ProductMedia.Remove(productMedia);

            await unitOfWork.Save();

            return(Ok());
        }
예제 #12
0
        public OperationResponse Insert(ProductMedia input)
        {
            var result = new OperationResponse <ProductMediaInsertCommandOutputDTO>();

            var dbLocator = AmbientDbContextLocator.Get <RiverdaleDBContext>();

            dbLocator.Add(input);

            return(result);
        }
예제 #13
0
        public IHttpActionResult GetProductMedia(int id)
        {
            ProductMedia productMedia = db.ProductMedias.Find(id);

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

            return(Ok(productMedia));
        }
        public async Task <ActionResult> UpdateProductMedia(UpdatedProperty updatedProductMedia)
        {
            ProductMedia productMedia = await unitOfWork.ProductMedia.Get(updatedProductMedia.ItemId);

            productMedia.MediaId = updatedProductMedia.PropertyId;

            // Update and save
            unitOfWork.ProductMedia.Update(productMedia);
            await unitOfWork.Save();

            return(Ok());
        }
 protected virtual AllProductImages CreateAllProductImages(ProductMedia productMedia, MediaSettings mediaSettings, GetAllProductImagesParam param, string variantId)
 {
     return(new AllProductImages
     {
         ImageUrl = productMedia != null?GetSizedImageUrl(productMedia, mediaSettings, param.ImageSize) : "",
                        ThumbnailUrl = productMedia != null?GetSizedImageUrl(productMedia, mediaSettings, param.ThumbnailImageSize) : "",
                                           ProductZoomImageUrl = productMedia != null?GetSizedImageUrl(productMedia, mediaSettings, param.ProductZoomImageSize) : "",
                                                                     ProductId = param.ProductId,
                                                                     VariantId = variantId,
                                                                     SequenceNumber = productMedia?.Position ?? 0,
                                                                     FallbackImageUrl = GetFallbackImageUrl(),
                                                                     Alt = productMedia?.Title,
     });
 }
예제 #16
0
        public IHttpActionResult PostProductMedia(ProductMedia productMedia)
        {
            productMedia.CreateDate   = DateTime.Today;
            productMedia.ModifiedDate = DateTime.Today;
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.ProductMedias.Add(productMedia);
            db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = productMedia.Id }, productMedia));
        }
예제 #17
0
        public IHttpActionResult DeleteProductMedia(int id)
        {
            ProductMedia productMedia = db.ProductMedias.Find(id);

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

            db.ProductMedias.Remove(productMedia);
            db.SaveChanges();

            return(Ok(productMedia));
        }
        protected virtual string GetSizedImageUrl(ProductMedia productMedia, MediaSettings mediaSettings, string size)
        {
            if (productMedia.ResizedInstances != null && productMedia.ResizedInstances.Length > 0 && !string.IsNullOrEmpty(size))
            {
                var resizedImage = productMedia.ResizedInstances.FirstOrDefault(resizedImg => resizedImg.Size == size);

                if (resizedImage != null)
                {
                    return(GetImageUrl(resizedImage.Url, mediaSettings));
                }
            }

            return(GetImageUrl(productMedia.Url, mediaSettings));
        }
        public async Task <ActionResult> AddProductMedia(UpdatedProperty productMedia)
        {
            ProductMedia newProductMedia = new ProductMedia
            {
                ProductId = productMedia.ItemId,
                MediaId   = productMedia.PropertyId
            };

            unitOfWork.ProductMedia.Add(newProductMedia);

            await unitOfWork.Save();

            return(Ok(newProductMedia.Id));
        }
예제 #20
0
        protected void ProcessImageFile(ZipProcessor.ZippedFile zipFile, IUnitOfWork unit, string filePath)
        {
            var productID = Path.GetFileNameWithoutExtension(zipFile.FileName).Trim();

            var product = unit.Scope.Repository <VendorAssortment>().GetSingle(va => va.CustomItemNumber == productID && va.VendorID == VendorID && va.IsActive == true);

            if (product == null)
            {
                //log.WarnFormat("Cannot process image for product with VSN number: {0} because it doesn't exist in Concentrator database", productID);
                return;
            }

            string destFileName = String.Format("{0}_{1}_{2}{3}", product.ProductID, product.CustomItemNumber, product.VendorID, Path.GetExtension(zipFile.FileName));
            //string destinationPath = Path.Combine(GetConfiguration().AppSettings.Settings["FTPImageDirectory"].Value, "Products");
            string destinationPath = Path.Combine(filePath, "Products");

            string finalDestination = Path.Combine(destinationPath, destFileName);
            var    mediaRepo        = unit.Scope.Repository <ProductMedia>();
            var    productImage     = mediaRepo.GetSingle(c => c.ProductID == product.ProductID && c.VendorID == VendorID);

            if (productImage == null)
            {
                productImage = new ProductMedia
                {
                    ProductID = product.ProductID,
                    VendorID  = VendorID,
                    MediaUrl  = String.Empty,
                    TypeID    = 1
                };
                mediaRepo.Add(productImage);
            }

            if (File.Exists(finalDestination))
            {
                //log.WarnFormat("Skipping image for product with VSN number: {0} because it already exists", productID);
            }
            else
            {
                if (!Directory.Exists(destinationPath))
                {
                    Directory.CreateDirectory(destinationPath);
                }

                using (var file = File.Create(finalDestination))
                {
                    zipFile.Data.WriteTo(file);
                }
            }
            productImage.MediaPath = Path.Combine("Products", destFileName);
        }
예제 #21
0
        // GET: api/ProductMedia/5
        public IHttpActionResult Get(int id)
        {
            var               result            = new Result <ProductMediaModel>();
            ProductMedia      productMedia      = db.ProductMedias.Find(id);
            ProductMediaModel productMediaModel = new ProductMediaModel();

            if (productMedia == null)
            {
                return(Json(DataNotFound(result)));
            }

            Mapper.Map(productMedia, productMediaModel);
            result.Data = productMediaModel;
            return(Json(result));
        }
예제 #22
0
        public BaseEntity BuildObject(Dictionary <string, object> row)
        {
            var productMedia = new ProductMedia
            {
                Id     = GetIntValue(row, DB_COL_ID),
                Image1 = GetStringValue(row, DB_COL_IMAGE_1),
                Image2 = GetStringValue(row, DB_COL_IMAGE_2),
                Image3 = GetStringValue(row, DB_COL_IMAGE_3),
                Image4 = GetStringValue(row, DB_COL_IMAGE_4),
                Image5 = GetStringValue(row, DB_COL_IMAGE_5),
                Video  = GetStringValue(row, DB_COL_VIDEO),
                Active = GetBooleanValue(row, DB_COL_ACTIVE)
            };

            return(productMedia);
        }
예제 #23
0
        private async Task SaveProductMedias(ProductForm model, Product product)
        {
            if (model.ThumbnailImage != null)
            {
                var fileName = await SaveFile(model.ThumbnailImage);

                if (product.ThumbnailImage != null)
                {
                    product.ThumbnailImage.FileName = fileName;
                }
                else
                {
                    product.ThumbnailImage = new Media {
                        FileName = fileName
                    };
                }
            }

            foreach (var file in model.ProductImages)
            {
                var fileName = await SaveFile(file);

                var productMedia = new ProductMedia
                {
                    Product = product,
                    Media   = new Media {
                        FileName = fileName, MediaType = MediaType.Image
                    }
                };
                product.AddMedia(productMedia);
            }

            foreach (var file in model.ProductDocuments)
            {
                var fileName = await SaveFile(file);

                var productMedia = new ProductMedia
                {
                    Product = product,
                    Media   = new Media {
                        FileName = fileName, MediaType = MediaType.File, Caption = file.FileName
                    }
                };
                product.AddMedia(productMedia);
            }
        }
예제 #24
0
        // POST
        public IHttpActionResult Post(ProductMedia productMedia)
        {
            try
            {
                var mng = new ProductMediaManager();
                if (productMedia.Image2 == null)
                {
                    productMedia.Image2 = "";
                }

                if (productMedia.Image3 == null)
                {
                    productMedia.Image3 = "";
                }
                if (productMedia.Image4 == null)
                {
                    productMedia.Image4 = "";
                }

                if (productMedia.Image5 == null)
                {
                    productMedia.Image5 = "";
                }

                if (productMedia.Video == null)
                {
                    productMedia.Video = "";
                }

                productMedia.Active = true;
                mng.Create(productMedia);

                apiResp = new ApiResponse
                {
                    Message = "Action was executed."
                };

                return(Ok(apiResp));
            }
            catch (BussinessException bex)
            {
                return(InternalServerError(new Exception(bex.ExceptionId + "-" + bex.AppMessage.Message)));
            }
        }
예제 #25
0
        // GET api/Proyect/Id
        public IHttpActionResult Get(int id)
        {
            try
            {
                var mng          = new ProductMediaManager();
                var productMedia = new ProductMedia
                {
                    Id = id
                };

                apiResp.Data = mng.RetrieveById(productMedia);

                return(Ok(apiResp));
            }
            catch (BussinessException bex)
            {
                return(InternalServerError(new Exception(bex.ExceptionId + "-" + bex.AppMessage.Message)));
            }
        }
예제 #26
0
        // PUT
        public IHttpActionResult Put(ProductMedia productMedia)
        {
            try
            {
                var mng = new ProductMediaManager();
                mng.Update(productMedia);

                apiResp = new ApiResponse
                {
                    Message = "Action was executed."
                };

                return(Ok(apiResp));
            }
            catch (BussinessException bex)
            {
                return(InternalServerError(new Exception(bex.ExceptionId + "-" + bex.AppMessage.Message)));
            }
        }
예제 #27
0
        public IHttpActionResult Post([FromBody] ProductModel productModel)
        {
            var result = new Result <string>();

            using (var db = new jupiterEntities())
            {
                Product product = new Product();
                productModel.CreateOn = DateTime.Now;
                Mapper.Map(productModel, product);
                try
                {
                    db.Products.Add(product);
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    result.IsSuccess    = false;
                    result.ErrorMessage = e.Message;
                }
                try
                {
                    if (productModel.ProdMedias.Count != 0)
                    {
                        foreach (var pm in productModel.ProdMedias)
                        {
                            ProductMedia prodMedia = new ProductMedia();
                            prodMedia.ProdId = product.ProdId;
                            {
                                prodMedia.Url = pm;
                            };
                            db.ProductMedias.Add(prodMedia);
                        }
                        db.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    result.IsSuccess    = false;
                    result.ErrorMessage = e.Message;
                }
                return(Json(result));
            }
        }
        public ProductMedia RetrieveById(ProductMedia productMedia)
        {
            ProductMedia p = null;

            try
            {
                p = crudProductMedia.Retrieve <ProductMedia>(productMedia);
                if (p == null)
                {
                    throw new BussinessException(4);
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.GetInstance().Process(ex);
            }

            return(p);
        }
예제 #29
0
        public IHttpActionResult Post([FromBody] ProductMediaModel productMediaModel)
        {
            var          result       = new Result <ProductMediaModel>();
            ProductMedia productMedia = new ProductMedia();

            Mapper.Map(productMediaModel, productMedia);
            result.Data = productMediaModel;
            try
            {
                db.ProductMedias.Add(productMedia);
                db.SaveChanges();
            }
            catch (Exception e)
            {
                result.ErrorMessage = e.Message;
                result.IsSuccess    = false;
                return(Json(result));
            }
            return(Json(result));
        }
예제 #30
0
        public Error AddMediaToProduct(ProductModel product,
                                       CompanyModel company,
                                       UserModel user,
                                       string mediaFile,
                                       ProductMediaModel prodMedia,      // output
                                       FileCopyType copyType)
        {
            var error = new Error();

            var media = new MediaModel();

            error = MediaServices.InsertOrUpdateMedia(media,
                                                      company,
                                                      user,
                                                      MediaFolder.Product,
                                                      mediaFile,
                                                      "",
                                                      product.Id,
                                                      -1,
                                                      copyType);
            if (!error.IsError)
            {
                var tempProdMedia = db.FindProductMedias(product.Id)
                                    .Where(pm => pm.MediaId == media.Id)
                                    .FirstOrDefault();
                if (tempProdMedia == null)
                {
                    // Not currently linked, so link it
                    tempProdMedia = new ProductMedia {
                        Id        = 0,
                        ProductId = product.Id,
                        MediaId   = media.Id
                    };
                    db.InsertOrUpdateProductMedia(tempProdMedia);
                }
                Mapper.Map <ProductMedia, ProductMediaModel>(tempProdMedia, prodMedia);
            }

            return(error);
        }
예제 #31
0
 public void RemoveMedia(ProductMedia media)
 {
     media.Product = null;
     Medias.Remove(media);
 }
예제 #32
0
 public void AddMedia(ProductMedia media)
 {
     media.Product = this;
     Medias.Add(media);
 }
예제 #33
0
        private void SaveProductImages(ProductForm model, Product product)
        {
            if (model.ThumbnailImage != null)
            {
                var fileName = SaveFile(model.ThumbnailImage);
                if (product.ThumbnailImage != null)
                {
                    product.ThumbnailImage.FileName = fileName;
                }
                else
                {
                    product.ThumbnailImage = new Media { FileName = fileName };
                }
            }

            // Currently model binder cannot map the collection of file productImages[0], productImages[1]
            foreach (var file in Request.Form.Files)
            {
                if (file.ContentDisposition.Contains("productImages"))
                {
                    model.ProductImages.Add(file);
                }
            }

            foreach (var file in model.ProductImages)
            {
                var fileName = SaveFile(file);
                var productMedia = new ProductMedia
                {
                    Product = product,
                    Media = new Media { FileName = fileName }
                };
                product.AddMedia(productMedia);
            }
        }