public async Task UpdateVideo(Guid id, UpdateVideoParam param, IFormFile file) { var video = await _catalogDb.Videos.FirstOrDefaultAsync(v => v.Id == id); if (file != null) { var imgDirectoryPath = Path.Combine(_fileStorageSettings.Path, "images"); var dir = new DirectoryInfo(imgDirectoryPath); if (!dir.Exists) { dir.Create(); } foreach (var oldFile in dir.EnumerateFiles(id + ".*")) { oldFile.Delete(); } var imageFileName = id + file.FileName.Substring(file.FileName.LastIndexOf(".")); var filePath = imgDirectoryPath = Path.Combine(imgDirectoryPath, imageFileName); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } video.ImageFileName = imageFileName; } video.Name = param.Title; video.Description = param.Description; await _catalogDb.SaveChangesAsync(); }
public async Task <IActionResult> UpdateVideo(Guid id, [FromForm] IFormFile file, [FromForm] string jsonString) { if (!User.IsAdmin()) { _logger.LogInformation("User is not in admin role, admin rights are required for updating the video."); throw new ForbiddenException("User is not in admin role, admin rights are required for updating the video."); } var allowedContentTypes = new List <string> { "image/jpeg" }; var allowedExtensions = new List <string> { "jpg" }; if (file != null) { var splittedFilename = file.FileName.Split("."); if (!allowedContentTypes.Contains(file.ContentType) || !allowedExtensions.Contains(splittedFilename[splittedFilename.Length - 1])) { return(BadRequest()); } } UpdateVideoParam param = JsonConvert.DeserializeObject <UpdateVideoParam>(jsonString); var video = await _catalogService.GetVideo(id); if (video == null) { return(NotFound()); } await _catalogService.UpdateVideo(id, param, file); return(Ok(new { })); }