Exemplo n.º 1
0
        public ActionResult Edit(CategoryViewModel categoryVM)
        {
            if (categoryVM.PictureSelector.ImageFile == null && categoryVM.PictureSelector.PictureId == null)
            {
                this.ModelState.AddModelError(nameof(categoryVM.PictureSelector), ValidationMessages.Required);
            }

            if (this.ModelState.IsValid == false)
            {
                return(this.View(categoryVM));
            }

            CreatePictureResponse pictureResponse = null;

            if (categoryVM.PictureSelector.ImageFile != null)
            {
                string categoriesImagesPath = WebConfigurationManager.AppSettings[Constants.CategoriesImagesPath_ConfigKey];
                string serverPath           = this.Server.MapPath(categoriesImagesPath);

                var pictureRequest = new CreatePictureRequest
                {
                    ContentType   = categoryVM.PictureSelector.ImageFile.ContentType,
                    FileExtention = Path.GetExtension(categoryVM.PictureSelector.ImageFile.FileName),
                    InputStream   = categoryVM.PictureSelector.ImageFile.InputStream,
                    ServerPath    = serverPath,
                    UrlPath       = categoriesImagesPath,
                };

                pictureResponse = this.pictureService.Create(pictureRequest);

                if (pictureResponse.Result != CreatePictureResult.Success)
                {
                    this.ModelState.AddModelError(nameof(categoryVM.PictureSelector.ImageFile), pictureResponse.Message);
                    return(this.View(categoryVM));
                }
            }

            var updateRequest = new UpdateCategoryRequest()
            {
                Id           = categoryVM.Id,
                Name         = categoryVM.Name,
                DisplayOrder = categoryVM.Order,
                IsPrimary    = categoryVM.IsPrimary,
                Visible      = categoryVM.Visible,
                PictureId    = pictureResponse?.Picture.Id,
            };

            updateRequest.SelectedCategoriesIds = categoryVM.Subcategories.ListItems
                                                  .Where(x => x.Checked)
                                                  .Select(x => int.Parse(x.Id));

            this.categoryService.Update(updateRequest);
            return(this.RedirectToAction("Index"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create(IFormFile picture, [FromForm] CreatePictureRequest createPictureRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var userId = HttpContext?.User.Claims.First(x => x.Type == ClaimTypes.NameIdentifier).Value;

            var result = await _pictureService.CreateAsync(picture, userId, createPictureRequest);

            return(Ok(result));
        }
        /// <summary>
        /// 如果您需要在 [云端混流转码](https://cloud.tencent.com/document/product/647/16827) 时频繁新增自定义背景图或水印,可通过此接口上传新的图片素材。无需频繁新增图片的场景,建议直接在 [控制台 > 应用管理 > 素材管理](https://cloud.tencent.com/document/product/647/50769) 中操作。
        /// </summary>
        /// <param name="req"><see cref="CreatePictureRequest"/></param>
        /// <returns><see cref="CreatePictureResponse"/></returns>
        public CreatePictureResponse CreatePictureSync(CreatePictureRequest req)
        {
            JsonResponseModel <CreatePictureResponse> rsp = null;

            try
            {
                var strResp = this.InternalRequestSync(req, "CreatePicture");
                rsp = JsonConvert.DeserializeObject <JsonResponseModel <CreatePictureResponse> >(strResp);
            }
            catch (JsonSerializationException e)
            {
                throw new TencentCloudSDKException(e.Message);
            }
            return(rsp.Response);
        }
Exemplo n.º 4
0
        public static PictureStorage MapCreatePictureRequestToPictureStorage(CreatePictureRequest createPictureRequest,
                                                                             string url, string userId)
        {
            if (createPictureRequest == null)
            {
                return(null);
            }

            return(new PictureStorage
            {
                CreatedAt = DateTime.UtcNow,
                Caption = createPictureRequest.Caption,
                ContestId = createPictureRequest.ContestId,
                Url = url,
                UserId = userId,
                Votes = 0
            });
        }
Exemplo n.º 5
0
        public async Task <CreatePictureResponse> CreateAsync(IFormFile picture, string userId,
                                                              CreatePictureRequest createPictureRequest)
        {
            var filePath = Path.GetFullPath(Environment.CurrentDirectory + "/Uploads/" + picture.FileName);

            if (picture.Length > 0)
            {
                using (var stream = new FileStream(filePath, FileMode.Create))
                    await picture.CopyToAsync(stream);
            }

            var result = await _pictureRepository.CreateAsync(
                PictureMapper.MapCreatePictureRequestToPictureStorage(createPictureRequest, picture.FileName, userId));

            return(new CreatePictureResponse {
                Id = result
            });
        }