public async Task Create(Entities.Content.Content product) { _products.Add(product); await _uow.SaveChangesAsync(); }
public void UpdateContent(Entities.Content.Content product, IFormFile photo, IFormFile photo2, IFormFile photo3, IFormFile[] files, IFormFile video, IFormFile pdf, IFormFile Mp364, IFormFile Mp3128, IFormFile Mp3320) { var directoryName = _categoryService.FindByIdAsync(product.CategoryId).Result.Description.SafeFileName(); if (!Directory.Exists(_hostingEnvironment.WebRootPath + "/content/files/album/" + directoryName)) { Directory.CreateDirectory(_hostingEnvironment.WebRootPath + "/content/files/album/" + directoryName); } if (photo != null && photo.Length > 0) { if (File.Exists(_hostingEnvironment.WebRootPath + "/content/files/album/" + product.Pic)) { File.Delete(_hostingEnvironment.WebRootPath + "/content/files/album/" + product.Pic); } if (File.Exists(_hostingEnvironment.WebRootPath + "/content/files/album/" + product.Thumbnail)) { File.Delete(_hostingEnvironment.WebRootPath + "/content/files/album/" + product.Thumbnail); } string fileName = ""; long fileSize = 0; var size = new[] { 300 }; var resized = new string[1]; fileName = _uploadService.UploadPicResize( photo, "/content/files/album/" + directoryName, 2000, ref fileSize, size, EnumC.Dimensions.Width, ref resized ); product.Pic = directoryName + "/" + fileName; product.Thumbnail = directoryName + "/" + size[0] + "/" + resized[0]; } if (photo2 != null && photo2.Length > 0) { string fileName = ""; long fileSize = 0; var size = new[] { 300 }; var resized = new string[1]; fileName = _uploadService.UploadPicResize( photo2, "/content/files/content", 2000, ref fileSize, size, EnumC.Dimensions.Width, ref resized ); product.Pic2 = fileName; product.Thumbnail2 = size[0] + "/" + resized[0]; } if (photo3 != null && photo3.Length > 0) { string fileName = ""; long fileSize = 0; var size = new[] { 300 }; var resized = new string[1]; fileName = _uploadService.UploadPicResize( photo3, "/content/files/content", 2000, ref fileSize, size, EnumC.Dimensions.Width, ref resized ); product.Pic3 = fileName; product.Thumbnail3 = size[0] + "/" + resized[0]; } if (Mp3128 != null) { if (File.Exists(_hostingEnvironment.WebRootPath + "/content/files/album/" + product.Mp3128)) { File.Delete(_hostingEnvironment.WebRootPath + "/content/files/album/" + product.Mp3128); } var allowed = new[] { //"application/pdf", "application/x-pdf" "audio/mpeg", "audio/mp3" }; string fileName = ""; long fileSize = 0; fileName = _uploadService.UploadFile( Mp3128, "/content/files/album/" + directoryName, ref fileSize, allowed ); product.Mp3128 = directoryName + "/" + fileName; } if (Mp364 != null) { if (File.Exists(_hostingEnvironment.WebRootPath + "/content/files/album/" + product.Mp364)) { File.Delete(_hostingEnvironment.WebRootPath + "/content/files/album/" + product.Mp364); } if (!Directory.Exists(_hostingEnvironment.WebRootPath + "/content/files/album/" + directoryName + "/64")) { Directory.CreateDirectory(_hostingEnvironment.WebRootPath + "/content/files/album/" + directoryName + "/64"); } var allowed = new[] { //"application/pdf", "application/x-pdf" "audio/mpeg", "audio/mp3" }; string fileName = ""; long fileSize = 0; fileName = _uploadService.UploadFile( Mp364, "/content/files/album/" + directoryName + "/64", ref fileSize, allowed ); product.Mp364 = directoryName + "/64/" + fileName; } if (Mp3320 != null) { if (File.Exists(_hostingEnvironment.WebRootPath + "/content/files/album/" + product.Mp3320)) { File.Delete(_hostingEnvironment.WebRootPath + "/content/files/album/" + product.Mp3320); } var allowed = new[] { //"application/pdf", "application/x-pdf" "audio/mpeg", "audio/mp3" }; if (!Directory.Exists(_hostingEnvironment.WebRootPath + "/content/files/album/" + directoryName + "/320")) { Directory.CreateDirectory(_hostingEnvironment.WebRootPath + "/content/files/album/" + directoryName + "/320"); } string fileName = ""; long fileSize = 0; fileName = _uploadService.UploadFile( Mp3320, "/content/files/album/" + directoryName + "/320", ref fileSize, allowed ); product.Mp3320 = directoryName + "/320/" + fileName; } if (video != null) { var allowed = new[] { "video/mp4", "image/x-png", "image/pjpeg", "image/jpeg", "image/gif" }; string fileName = ""; long fileSize = 0; fileName = _uploadService.UploadFile( video, "/content/files/content/video", ref fileSize, allowed ); product.Video = fileName; } if (pdf != null) { var allowed = new[] { //"application/pdf", "application/x-pdf" "audio/mpeg", "audio/mp3" }; string fileName = ""; long fileSize = 0; fileName = _uploadService.UploadFile( pdf, "/content/files/content/mp3", ref fileSize, allowed ); product.Pdf = fileName; } var allowedMimeType = new[] { "image/png", "image/x-png", "image/pjpeg", "image/jpeg", "image/gif", }; if (files != null) { // List<ContentFile> c = new List<ContentFile>(); var c = product.ContentFiles; foreach (var f in files) { string ffileName = ""; long ffileSize = 0; var fsize = new[] { 650 }; var fresized = new string[1]; if (allowedMimeType.Contains(f.ContentType)) { ffileName = _uploadService.UploadPicResize( f, "/content/files/content/files", 2000, ref ffileSize, fsize, EnumC.Dimensions.Width, ref fresized ); c.Add(new ContentFile { Ext = f.ContentType, FileName = ffileName, FileSize = ffileSize, Thumbnail = fsize[0] + "/" + fresized[0], Type = "pic" }); } else { ffileName = _uploadService.UploadFile( f, "/content/files/content/files", ref ffileSize, new[] { "image/svg+xml", "application/svg+xml", "text/html+svg", "video/mp4", "audio/mp3", "audio/mpeg", } ); c.Add(new ContentFile { Ext = f.ContentType, FileName = ffileName, FileSize = ffileSize, Type = "file" }); } } product.ContentFiles = c; } _products.Update(product); _uow.SaveChanges(); }