Пример #1
0
        public void UploadProductImage()
        {
            IClient commerceToolsClient             = this.productFixture.GetService <IClient>();
            Product product                         = this.productFixture.CreateProduct();
            Stream  imageStream                     = File.OpenRead("Resources/commercetools.png");
            UploadProductImageCommand uploadCommand =
                new UploadProductImageCommand(new Guid(product.Id), imageStream, "image/png");
            Product updatedProduct = commerceToolsClient.ExecuteAsync(uploadCommand).Result;

            this.productFixture.ProductsToDelete.Add(updatedProduct);
            Assert.NotNull(updatedProduct.MasterData.Staged.MasterVariant.Images[0].Url);
        }
        public Task <MsgResult <ProductDTO> > UploadImageAsync(UploadProductImageCommand cmd)
        {
            using (var stream = Request.Form.Files[0].OpenReadStream())
            {
                var asin = Request.Form["PostData[ASIN]"].ToString();
                if (asin.IsNotNull())
                {
                    cmd.asin        = asin;
                    cmd.PictureData = stream.ToBytes();
                }
            }

            return(base.RequestAsync <UploadProductImageCommand, MsgResult <ProductDTO> >(cmd));
        }
Пример #3
0
        // *******************************************************************************************************************************
        #region -  UploadImageAsync  -

        public async Task <MsgResult <ProductDTO> > UploadImageAsync(UploadProductImageCommand cmd)
        {
            var dto = await ProductDAL.GetProductAsync(cmd.asin).ConfigureAwait(false);

            if (dto.IsNotNull())
            {
                var sha1    = cmd.PictureData.ToSha1String();
                var key     = $"p/{ DateTime.Now:yyyy'/'MM'/'dd}/{sha1}.jpg";
                var msgCode = await Storage.SaveAsync(key, cmd.PictureData).ConfigureAwait(false);

                if (!msgCode.IsSuccess())
                {
                    return(new MsgResult <ProductDTO>(MsgCodes.SaveFileToCloudFailed));
                }

                dto.ImageUrl = _imageRoot + key;
                await ProductDAL.UpdateProductImageAsync(dto).ConfigureAwait(false);
            }

            return(new MsgResult <ProductDTO>(dto));
        }
        //[Authorize]
        public async Task <IActionResult> UploadProductImage(int productId, [FromForm] ImageUpload imageUpload)
        {
            if (imageUpload.files.Length == 0)
            {
                throw new ApiException($"Please select proper file.");
            }

            string path = _webHostEnvironment.WebRootPath + "\\ProductImages\\" + productId.ToString() + "\\";

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

            if (System.IO.File.Exists(path + imageUpload.files.FileName))
            {
                throw new ApiException($"Same file already exists.");
            }

            UploadProductImageCommand command = new UploadProductImageCommand
            {
                ProductId = productId,
                ImageName = imageUpload.files.FileName
            };

            var result = await Mediator.Send(command);

            if (result.Data != 0)
            {
                using FileStream fileStream = System.IO.File.Create(path + imageUpload.files.FileName);
                await imageUpload.files.CopyToAsync(fileStream);

                await fileStream.FlushAsync();

                result.Message = "Upload Done.";
            }

            return(Ok(result));
        }