public ActionResult Download(string id, string fileName) { if (!string.IsNullOrEmpty(Request.Headers["If-Modified-Since"])) { Response.StatusCode = 304; Response.StatusDescription = "Not Modified"; Response.AddHeader("Content-Length", "0"); return(Content(String.Empty)); } var file = AsefianFileContextHelper.GetFile(id, fileName); try { MemoryStream outStream = new MemoryStream(); using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false)) { ISupportedImageFormat format = new JpegFormat { Quality = 70 }; imageFactory.Load(file.Data) .Format(format) .Save(outStream); return(File(outStream, MimeMapping.GetMimeMapping(fileName))); } } catch (Exception) { return(File(file.Data, file.MimeType)); } }
public JsonResult Delete(int id) { try { var currentUser = GetAuthenticatedUser(); var entity = _context.Product.Single(x => x.StatusId != ProductStatus.Deleted.Id && x.Id == id); if (!string.IsNullOrEmpty(entity.FileId)) { AsefianFileContextHelper.DeleteFile(entity.FileId, entity.FileName, currentUser.id, "admin", "product"); } entity.StatusId = ProductStatus.Deleted.Id; entity.ModifyUserId = currentUser.id; entity.ModifyDate = GetDatetime(); entity.ModifyIp = GetCurrentIp(); _context.SaveChanges(); return(Success("محصول با موفقیت حذف شد.")); } catch (Exception ex) { return(ServerError(ex)); } }
public ActionResult ResizeImage(string size, string fileId, string fileName) { try { if (!string.IsNullOrEmpty(Request.Headers["If-Modified-Since"])) { Response.StatusCode = 304; Response.StatusDescription = "Not Modified"; Response.AddHeader("Content-Length", "0"); return(Content(string.Empty)); } #region Temp Path var tempPath = Server.MapPath("~/Temp/Thumb/resize"); if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); } var filePath = $"{tempPath}\\{size}-{fileId}{Path.GetExtension(fileName)}"; if (System.IO.File.Exists(filePath)) { byte[] filedata = System.IO.File.ReadAllBytes(filePath); string contentType = MimeMapping.GetMimeMapping(filePath); return(File(filedata, contentType)); } #endregion var sizes = size.Split('x').Select(x => int.Parse(x)).ToArray(); var file = AsefianFileContextHelper.GetFile(fileId, fileName); MemoryStream outStream = new MemoryStream(); ISupportedImageFormat format = new JpegFormat { Quality = 70 }; using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false)) { var resizeLayer = new ResizeLayer(new Size(sizes[0], sizes[1]), ResizeMode.Min); imageFactory.Load(file.Data) .Resize(resizeLayer) .Format(format) .Save(filePath) .Save(outStream); return(File(outStream, MimeMapping.GetMimeMapping(fileName))); } } catch (Exception ex) { ServerError(ex); return(HttpNotFound()); } }
public JsonResult RemoveFile(string id, string fileName) { try { var currentUser = GetAuthenticatedUser(); AsefianFileContextHelper.DeleteFile(id, fileName, currentUser.id); return(Success("فایل با موفقیت حذف شد.")); } catch (Exception ex) { return(ServerError(ex)); } }
/// <summary> /// تایید فایل تصاویر در کد های اچ تی ام ال /// </summary> /// <param name="text">متن اچ تی ام ال</param> protected void VerifyHtmlImageFile(string text) { if (string.IsNullOrEmpty(text)) { return; } foreach (Match item in Regex.Matches(text, "<img.+?src=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase)) { string matchString = item.Groups[1].Value; var imageParts = matchString.Split('/'); AsefianFileContextHelper.VerifyFile(imageParts[3], imageParts[4]); } }
public JsonResult AddImage(int id) { try { var currentUser = GetAuthenticatedUser(); if (Request.Files.Count > 0) { var file = Request.Files[0]; var data = AsefianFileContextHelper.Save(file, currentUser.id, GetCurrentIp()); if (data != null) { _context.ProductFile.Add(new ProductFile() { ProductId = id, FileName = data.fileName, FileId = data.id, TypeId = ProductFileType.Picture.Id, StatusId = ProductFileStatus.Active.Id, CreateUserId = currentUser.id, ModifyUserId = currentUser.id, CreateDate = GetDatetime(), ModifyDate = GetDatetime(), CreateIp = GetCurrentIp(), ModifyIp = GetCurrentIp() }); AsefianFileContextHelper.VerifyFile(data.id, data.fileName); _context.SaveChanges(); return(Success("فایل با موفقیت ذخیره شد.", data)); } else { return(Error("فایلی جهت ذخیره یافت نشد.")); } } else { return(Error("فایلی برای ذخیره یافت نشد.")); } } catch (Exception ex) { return(ServerError(ex)); } }
/// <summary> /// تایید فایل تصاویر در کد های اچ تی ام ال /// </summary> /// <param name="text">متن اچ تی ام ال</param> protected void DeleteHtmlImageFile(string text) { if (string.IsNullOrEmpty(text)) { return; } var currentUser = GetAuthenticatedUser(); foreach (Match item in Regex.Matches(text, "<img.+?src=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase)) { string matchString = item.Groups[1].Value; var imageParts = matchString.Split('/'); AsefianFileContextHelper.DeleteFile(imageParts[3], imageParts[4], currentUser.id); } }
public JsonResult AddProductImage(AddProductImageViewModel model) { var currentUser = GetAuthenticatedUser(); try { var entity = _context.Product.Single(x => x.Id == model.id); if (Request.Files.Count > 0) { if (!string.IsNullOrEmpty(entity.FileId)) { AsefianFileContextHelper.DeleteFile(entity.FileId, entity.FileName, currentUser.id, "admin", "product"); } var fileEntity = AsefianFileContextHelper.Save(Request.Files[0], currentUser.id, GetCurrentIp(), "admin", "product"); model.productFileId = fileEntity.id; model.productFileName = fileEntity.fileName; } else if (string.IsNullOrEmpty(model.productFileId) && !string.IsNullOrEmpty(entity.FileId)) { if (!string.IsNullOrEmpty(entity.FileId)) { AsefianFileContextHelper.DeleteFile(entity.FileId, entity.FileName, currentUser.id, "admin", "Product"); } } entity.FileId = model.productFileId; entity.FileName = model.productFileName; _context.SaveChanges(); var data = new { id = entity.Id, fileName = entity.FileName, fileId = entity.FileId }; return(Success("Done", data)); } catch (Exception ex) { return(ServerError(ex)); } }
public JsonResult Upload() { try { var currentUser = GetAuthenticatedUser(); if (Request.Files.Count > 0) { var file = Request.Files[0]; var data = AsefianFileContextHelper.Save(file, currentUser.id, GetCurrentIp()); return(Success("فایل با موفقیت ذخیره شد.", data)); } else { return(Error("فایلی برای ذخیره یافت نشد.")); } } catch (Exception ex) { return(ServerError(ex)); } }
public JsonResult RemoveGalleryFile(int id) { try { var currentUser = GetAuthenticatedUser(); var file = _context.ProductFile.Single(x => x.Id == id); if (file != null) { AsefianFileContextHelper.DeleteFile(file.FileId, file.FileName, currentUser.id); file.StatusId = ProductFileStatus.Deleted.Id; file.ModifyUserId = currentUser.id; file.ModifyDate = GetDatetime(); file.ModifyIp = GetCurrentIp(); _context.SaveChanges(); } return(Success("فایل با موفقیت حذف شد.")); } catch (Exception ex) { return(ServerError(ex)); } }
public JsonResult Save(ProductViewModel model) { try { var currentUser = GetAuthenticatedUser(); if (model.translations.Count != Language.GetList().Count()) { return(Error("ترجمه های وارد شده صحیح نیست.")); } foreach (var item in model.translations) { if (string.IsNullOrEmpty(item.title)) { return(Error(string.Format("وارد کردن عنوان در زبان {0} اجباری است.", Language.GetList().Single(x => x.Id == item.languageId).PersianTitle))); } } if (model.id != null && model.id > 0) { var entity = _context.Product.Single(x => x.StatusId != ProductStatus.Deleted.Id && x.Id == model.id); if (Request.Files.Count > 0) { var file = Request.Files[0]; var contentTypes = new List <string>() { "image/x-png", "image/gif", "image/jpeg" }; if (!contentTypes.Contains(file.ContentType)) { return(Error("پسوند فایل انتخاب شده معتبر نیست.")); } if (!string.IsNullOrEmpty(entity.FileId)) { //AsefianFileContextHelper.DeleteFilePermanently(entity.FileId, entity.FileName); } var fileEntity = AsefianFileContextHelper.Save(file, currentUser.id, GetCurrentIp(), "admin", "category"); model.fileId = fileEntity.id; model.fileName = fileEntity.fileName; } else if (string.IsNullOrEmpty(model.fileId) && !string.IsNullOrEmpty(entity.FileId)) { if (!string.IsNullOrEmpty(entity.FileId)) { //AsefianFileContextHelper.DeleteFilePermanently(entity.FileId, entity.FileName); } } entity.CategoryId = model.categoryId; entity.CategoryFeatureId = model.categoryFeatureId; entity.Sku = model.translations.Single(x => x.languageId == Language.Persian.Id).title.ToStandardPersian(); entity.BrandId = model.brandId; entity.Code = model.code.ToStandardPersian(); entity.FileId = model.fileId; entity.FileName = model.fileName; entity.Price = model.price; entity.Width = model.width; entity.Height = model.height; entity.Discount = model.discount; entity.StatusId = model.statusId; entity.ModifyUserId = currentUser.id; entity.ModifyDate = GetDatetime(); entity.ModifyIp = GetCurrentIp(); model.translations.ForEach(item => { var entityItem = entity.TranslationList.SingleOrDefault(x => x.LanguageId == item.languageId); if (entityItem != null) { entityItem.Title = item.title.ToStandardPersian(); entityItem.Description = item.description?.ToStandardPersian(); } else { entityItem = new ProductTranslation() { ProductId = entity.Id, LanguageId = item.languageId, Title = item.title.ToStandardPersian(), Description = item.description?.ToStandardPersian(), Review = item.review }; _context.ProductTranslation.Add(entityItem); } }); _context.SaveChanges(); return(Success("اطلاعات محصول با موفقیت ویرایش شد.")); } else { if (Request.Files.Count > 0) { var file = Request.Files[0]; var contentTypes = new List <string>() { "image/x-png", "image/gif", "image/jpeg" }; if (!contentTypes.Contains(file.ContentType)) { return(Error("پسوند فایل انتخاب شده معتبر نیست.")); } var fileEntity = AsefianFileContextHelper.Save(file, currentUser.id, GetCurrentIp(), "admin", "category"); model.fileId = fileEntity.id; model.fileName = fileEntity.fileName; } else { model.fileId = null; model.fileName = null; } var entity = new Product() { CategoryId = model.categoryId, CategoryFeatureId = model.categoryFeatureId, Sku = model.translations.Single(x => x.languageId == Language.Persian.Id).title.ToStandardPersian(), BrandId = model.brandId, Code = model.code.ToStandardPersian(), FileId = model.fileId, FileName = model.fileName, Price = model.price, Width = model.width, Height = model.height, Discount = model.discount, StatusId = model.statusId, CreateUserId = currentUser.id, ModifyUserId = currentUser.id, CreateDate = GetDatetime(), ModifyDate = GetDatetime(), CreateIp = GetCurrentIp(), ModifyIp = GetCurrentIp() }; _context.Product.Add(entity); model.translations.ForEach(item => { var entityItem = entity.TranslationList.SingleOrDefault(x => x.LanguageId == item.languageId); if (entityItem != null) { entityItem.Title = item.title.ToStandardPersian(); entityItem.Description = item.description?.ToStandardPersian(); entityItem.Review = item.review; } else { entityItem = new ProductTranslation() { ProductId = entity.Id, LanguageId = item.languageId, Title = item.title.ToStandardPersian(), Description = item.description?.ToStandardPersian(), Review = item.review }; _context.ProductTranslation.Add(entityItem); } }); _context.SaveChanges(); return(Success("محصول با موفقیت ایجاد شد.")); } } catch (Exception ex) { return(ServerError(ex)); } }
public JsonResult Save(CategoryViewModel model) { var currentUser = GetAuthenticatedUser(); try { if (model.translations.Count != Language.GetList().Count()) { return(Error("ترجمه های وارد شده صحیح نیست.")); } foreach (var item in model.translations) { if (string.IsNullOrEmpty(item.title)) { return(Error(string.Format("وارد کردن عنوان در زبان {0} اجباری است.", Language.GetList().Single(x => x.Id == item.languageId).PersianTitle))); } } if (model.id != null && model.id > 0) { if (model.id == model.parentId) { return(Error("دسته بندی نمی تواند با خودش رابطه داشته باشد.")); } var entity = _context.Category.Single(x => x.StatusId != CategoryStatus.Deleted.Id && x.CategoryTypeId == CategoryType.Product.Id && x.Id == model.id); if (Request.Files.Count > 0) { var file = Request.Files[0]; var contentTypes = new List <string>() { "image/x-png", "image/gif", "image/jpeg" }; //if (!contentTypes.Contains(file.ContentType)) //{ // return Error("پسوند فایل انتخاب شده معتبر نیست."); //} if (!string.IsNullOrEmpty(entity.FileId)) { //AsefianFileContextHelper.DeleteFilePermanently(entity.FileId, entity.FileName); } var fileEntity = AsefianFileContextHelper.Save(file, currentUser.id, GetCurrentIp(), "admin", "category"); model.fileId = fileEntity.id; model.fileName = fileEntity.fileName; } else if (string.IsNullOrEmpty(model.fileId) && !string.IsNullOrEmpty(entity.FileId)) { if (!string.IsNullOrEmpty(entity.FileId)) { //AsefianFileContextHelper.DeleteFilePermanently(entity.FileId, entity.FileName); } } entity.ParentId = model.parentId; entity.Sku = model.translations.Single(x => x.languageId == Language.Persian.Id).title.ToStandardPersian(); entity.Order = model.order; entity.FileId = model.fileId; entity.FileName = model.fileName; entity.StatusId = model.statusId; entity.ModifyUserId = currentUser.id; entity.ModifyDate = GetDatetime(); entity.ModifyIp = GetCurrentIp(); model.translations.ForEach(item => { var entityItem = entity.TranslationList.SingleOrDefault(x => x.LanguageId == item.languageId); if (entityItem != null) { entityItem.Title = item.title.ToStandardPersian(); entityItem.Description = item.description?.ToStandardPersian(); } else { entityItem = new CategoryTranslation() { CategoryId = entity.Id, LanguageId = item.languageId, Title = item.title.ToStandardPersian(), Description = item.description?.ToStandardPersian() }; _context.CategoryTranslation.Add(entityItem); } }); _context.SaveChanges(); return(Success("اطلاعات دسته بندی با موفقیت ویرایش شد.")); } else { if (Request.Files.Count > 0) { var file = Request.Files[0]; var contentTypes = new List <string>() { "image/x-png", "image/gif", "image/jpeg" }; if (!contentTypes.Contains(file.ContentType)) { return(Error("پسوند فایل انتخاب شده معتبر نیست.")); } var fileEntity = AsefianFileContextHelper.Save(file, currentUser.id, GetCurrentIp(), "admin", "category"); model.fileId = fileEntity.id; model.fileName = fileEntity.fileName; } else { model.fileId = null; model.fileName = null; } var entity = new Category() { ParentId = model.parentId, Sku = model.translations.Single(x => x.languageId == Language.Persian.Id).title.ToStandardPersian(), Order = model.order, FileId = model.fileId, FileName = model.fileName, StatusId = model.statusId, CategoryTypeId = CategoryType.Product.Id, CreateUserId = currentUser.id, ModifyUserId = currentUser.id, CreateDate = GetDatetime(), ModifyDate = GetDatetime(), CreateIp = GetCurrentIp(), ModifyIp = GetCurrentIp() }; _context.Category.Add(entity); model.translations.ForEach(item => { var entityItem = entity.TranslationList.SingleOrDefault(x => x.LanguageId == item.languageId); if (entityItem != null) { entityItem.Title = item.title.ToStandardPersian(); entityItem.Description = item.description?.ToStandardPersian(); } else { entityItem = new CategoryTranslation() { CategoryId = entity.Id, LanguageId = item.languageId, Title = item.title.ToStandardPersian(), Description = item.description?.ToStandardPersian() }; _context.CategoryTranslation.Add(entityItem); } }); _context.SaveChanges(); return(Success(string.Format("دسته بندی \"{0}\" با موفقیت ایجاد شد.", entity.Sku))); } } catch (Exception ex) { return(ServerError(ex)); } }
public JsonResult AddImage(AddSpecialProjectFileViewModel model) { try { var currentUser = GetAuthenticatedUser(); if (Request.Files.Count > 0) { var file = Request.Files[0]; var data = AsefianFileContextHelper.Save(file, currentUser.id, GetCurrentIp()); if (data != null) { var entity = new SpecialProjectFile() { SpecialProjectId = model.id, FileName = data.fileName, Sku = model.translations.Single(x => x.languageId == Language.Persian.Id).title.ToStandardPersian(), FileId = data.id, StatusId = SpecialProjectFileStatus.Active.Id, CreateUserId = currentUser.id, ModifyUserId = currentUser.id, CreateDate = GetDatetime(), ModifyDate = GetDatetime(), CreateIp = GetCurrentIp(), ModifyIp = GetCurrentIp() }; _context.SpecialProjectFile.Add(entity); model.translations.ForEach(item => { var entityItem = entity.TranslationList.SingleOrDefault(x => x.LanguageId == item.languageId); if (entityItem != null) { entityItem.Title = item.title.ToStandardPersian(); entityItem.Description = item.description.ToStandardPersian(); } else { entityItem = new SpecialProjectFileTranslation() { SpecialProjectFileId = entity.Id, LanguageId = item.languageId, Title = item.title, Description = item.description, }; _context.SpecialProjectFileTranslation.Add(entityItem); } }); AsefianFileContextHelper.VerifyFile(data.id, data.fileName); _context.SaveChanges(); return(Success("فایل با موفقیت ذخیره شد.", data)); } else { return(Error("فایلی جهت ذخیره یافت نشد.")); } } else { return(Error("فایلی برای ذخیره یافت نشد.")); } } catch (Exception ex) { return(ServerError(ex)); } }
public JsonResult Save(SpecialProjectViewModel model) { try { if (string.IsNullOrEmpty(model.fileId) && Request.Files.Count == 0) { return(Error("وارد کردن فایل تصویر الزامی است.")); } var currentUser = GetAuthenticatedUser(); if (model.id != null && model.id > 0) { var entity = _context.SpecialProject.Single(x => x.StatusId != SpecialProjectStatus.Deleted.Id && x.Id == model.id); if (Request.Files.Count > 0) { if (!string.IsNullOrEmpty(entity.FileId)) { AsefianFileContextHelper.DeleteFile(entity.FileId, entity.FileName, currentUser.id, "admin", "mainPage"); } var fileEntity = AsefianFileContextHelper.Save(Request.Files[0], currentUser.id, GetCurrentIp(), "admin", "mainPage"); model.fileId = fileEntity.id; model.fileName = fileEntity.fileName; } else if (string.IsNullOrEmpty(model.fileId) && !string.IsNullOrEmpty(entity.FileId)) { if (!string.IsNullOrEmpty(entity.FileId)) { AsefianFileContextHelper.DeleteFile(entity.FileId, entity.FileName, currentUser.id, "admin", "mainPage"); } } entity.Order = model.order; entity.FileId = model.fileId; entity.FileName = model.fileName; entity.Sku = model.translations.Single(x => x.languageId == Language.Persian.Id).title.ToStandardPersian(); entity.StatusId = model.statusId; entity.ModifyUserId = currentUser.id; entity.ModifyDate = GetDatetime(); entity.ModifyIp = GetCurrentIp(); model.translations.ForEach(item => { var entityItem = entity.TranslationList.SingleOrDefault(x => x.LanguageId == item.languageId); if (entityItem != null) { entityItem.Title = item.title.ToStandardPersian(); entityItem.Description = item.description.ToStandardPersian(); } else { entityItem = new SpecialProjectTranslation() { SpecialProjectId = entity.Id, LanguageId = item.languageId, Title = item.title, Description = item.description.ToStandardPersian(), }; _context.SpecialProjectTranslation.Add(entityItem); } }); _context.SaveChanges(); return(Success("اطلاعات پروژه های خاص با موفقیت ویرایش شد.")); } else { if (Request.Files.Count > 0) { var fileEntity = AsefianFileContextHelper.Save(Request.Files[0], currentUser.id, GetCurrentIp(), "admin", "mainPage"); model.fileId = fileEntity.id; model.fileName = fileEntity.fileName; } else { model.fileId = null; model.fileName = null; } var entity = new SpecialProject() { Order = model.order, FileId = model.fileId, FileName = model.fileName, Sku = model.translations.Single(x => x.languageId == Language.Persian.Id).title.ToStandardPersian(), StatusId = model.statusId, CreateUserId = currentUser.id, ModifyUserId = currentUser.id, CreateDate = GetDatetime(), ModifyDate = GetDatetime(), CreateIp = GetCurrentIp(), ModifyIp = GetCurrentIp() }; _context.SpecialProject.Add(entity); model.translations.ForEach(item => { var entityItem = entity.TranslationList.SingleOrDefault(x => x.LanguageId == item.languageId); if (entityItem != null) { entityItem.Title = item.title.ToStandardPersian(); entityItem.Description = item.description.ToStandardPersian(); } else { entityItem = new SpecialProjectTranslation() { SpecialProjectId = entity.Id, LanguageId = item.languageId, Title = item.title, Description = item.description.ToStandardPersian(), }; _context.SpecialProjectTranslation.Add(entityItem); } }); _context.SaveChanges(); return(Success("پروژه های خاص با موفقیت ایجاد شد.")); } } catch (Exception ex) { return(ServerError(ex)); } }
public JsonResult Save(NewsViewModel model) { var currentUser = GetAuthenticatedUser(); try { DateTime?expiredDate = null; if (!string.IsNullOrEmpty(model.PersianExpiredDate)) { expiredDate = DateUtility.GetDateTime(model.PersianExpiredDate); } if (model.id != null && model.id > 0) { if (string.IsNullOrEmpty(model.fileId) && Request.Files.Count == 0) { return(Error("وارد کردن تصویر الزامی است.")); } var entity = _context.News.Single(x => x.StatusId != NewsStatus.Deleted.Id && x.Id == model.id); if (Request.Files.Count > 0) { if (!string.IsNullOrEmpty(entity.FileId)) { AsefianFileContextHelper.DeleteFile(entity.FileId, entity.FileName, currentUser.id, "admin", "news"); } var fileEntity = AsefianFileContextHelper.Save(Request.Files[0], currentUser.id, GetCurrentIp(), "admin", "news"); model.fileId = fileEntity.id; model.fileName = fileEntity.fileName; } else if (string.IsNullOrEmpty(model.fileId) && !string.IsNullOrEmpty(entity.FileId)) { if (!string.IsNullOrEmpty(entity.FileId)) { AsefianFileContextHelper.DeleteFile(entity.FileId, entity.FileName, currentUser.id, "admin", "news"); } } entity.Sku = model.translations.Single(x => x.languageId == Language.Persian.Id).title.ToStandardPersian(); entity.FileId = model.fileId; entity.FileName = model.fileName; entity.PublishDate = string.IsNullOrEmpty(model.PersianPublishDate) ? GetDatetime() : DateUtility.GetDateTime(model.PersianPublishDate); entity.ExpiredDate = expiredDate; entity.StatusId = model.statusId; entity.ModifyUserId = currentUser.id; entity.ModifyDate = GetDatetime(); entity.ModifyIp = GetCurrentIp(); model.translations.ForEach(item => { var entityItem = entity.TranslationList.SingleOrDefault(x => x.LanguageId == item.languageId); if (entityItem != null) { entityItem.Title = item.title?.ToStandardPersian(); entityItem.AbstractText = item.abstractText?.ToStandardPersian(); entityItem.Text = item.text?.ToStandardPersian(); entityItem.Keywords = item.keywords?.ToStandardPersian(); } else { entityItem = new NewsTranslation() { NewsId = entity.Id, LanguageId = item.languageId, Title = item.title.ToStandardPersian(), AbstractText = item.abstractText?.ToStandardPersian(), Text = item.text?.ToStandardPersian(), Keywords = item.keywords?.ToStandardPersian(), }; _context.NewsTranslation.Add(entityItem); } }); _context.SaveChanges(); // تایید تمام فایل های جدید ذخیره شده در نقد و بررسی //VerifyHtmlImageFile(entity.Text); return(Success("اطلاعات خبر با موفقیت ویرایش شد.")); } else { if (Request.Files.Count > 0) { var fileEntity = AsefianFileContextHelper.Save(Request.Files[0], currentUser.id, GetCurrentIp(), "admin", "service"); model.fileId = fileEntity.id; model.fileName = fileEntity.fileName; } else { return(Error("وارد کردن تصویر خبر الزامی است.")); } var entity = new News() { Sku = model.translations.Single(x => x.languageId == Language.Persian.Id).title.ToStandardPersian(), FileId = model.fileId, FileName = model.fileName, View = model.view, PublishDate = string.IsNullOrEmpty(model.PersianPublishDate) ? GetDatetime() : DateUtility.GetDateTime(model.PersianPublishDate), ExpiredDate = expiredDate, StatusId = model.statusId, CreateUserId = currentUser.id, ModifyUserId = currentUser.id, CreateDate = GetDatetime(), ModifyDate = GetDatetime(), CreateIp = GetCurrentIp(), ModifyIp = GetCurrentIp() }; _context.News.Add(entity); model.translations.ForEach(item => { var entityItem = entity.TranslationList.SingleOrDefault(x => x.LanguageId == item.languageId); if (entityItem != null) { entityItem.Title = item.title?.ToStandardPersian(); entityItem.AbstractText = item.abstractText?.ToStandardPersian(); entityItem.Text = item.text?.ToStandardPersian(); entityItem.Keywords = item.keywords?.ToStandardPersian(); } else { entityItem = new NewsTranslation() { NewsId = entity.Id, LanguageId = item.languageId, Title = item.title.ToStandardPersian(), AbstractText = item.abstractText?.ToStandardPersian(), Text = item.text?.ToStandardPersian(), Keywords = item.keywords?.ToStandardPersian(), }; _context.NewsTranslation.Add(entityItem); } }); _context.SaveChanges(); // تایید تمام فایل های جدید ذخیره شده در نقد و بررسی //VerifyHtmlImageFile(entity.Text); return(Success("خبر با موفقیت ایجاد شد.")); } } catch (Exception ex) { return(ServerError(ex)); } }
public JsonResult Save(ServiceViewModel model) { var currentUser = GetAuthenticatedUser(); try { //if (string.IsNullOrEmpty(model.title)) //{ // return Error("وارد کردن عنوان اجباری است."); //} //if (string.IsNullOrEmpty(model.summary)) //{ // return Error("وارد کردن خلاصه خدمات اجباری است."); //} if (model.id != null && model.id > 0) { var entity = _context.Service.Single(x => x.StatusId != ServiceStatus.Deleted.Id && x.Id == model.id); if (Request.Files.Count > 0) { if (!string.IsNullOrEmpty(entity.FileId)) { AsefianFileContextHelper.DeleteFile(entity.FileId, entity.FileName, currentUser.id, "admin", "service"); } var fileEntity = AsefianFileContextHelper.Save(Request.Files[0], currentUser.id, GetCurrentIp(), "admin", "service"); model.fileId = fileEntity.id; model.fileName = fileEntity.fileName; } else if (string.IsNullOrEmpty(model.fileId) && !string.IsNullOrEmpty(entity.FileId)) { if (!string.IsNullOrEmpty(entity.FileId)) { AsefianFileContextHelper.DeleteFile(entity.FileId, entity.FileName, currentUser.id, "admin", "service"); } } entity.Order = model.order; entity.Sku = model.translations.Single(x => x.languageId == Language.Persian.Id).title.ToStandardPersian(); entity.FileId = model.fileId; entity.FileName = model.fileName; entity.StatusId = model.statusId; entity.ModifyUserId = currentUser.id; entity.ModifyDate = GetDatetime(); entity.ModifyIp = GetCurrentIp(); model.translations.ForEach(item => { var entityItem = entity.TranslationList.SingleOrDefault(x => x.LanguageId == item.languageId); if (entityItem != null) { entityItem.Title = item.title.ToStandardPersian(); entityItem.Description = item.description?.ToStandardPersian(); entityItem.Summary = item.summary?.ToStandardPersian(); } else { entityItem = new ServiceTranslation() { ServiceId = entity.Id, LanguageId = item.languageId, Title = item.title.ToStandardPersian(), Description = item.description?.ToStandardPersian(), Summary = item.summary?.ToStandardPersian() }; _context.ServiceTranslation.Add(entityItem); } }); _context.SaveChanges(); // تایید تمام فایل های جدید ذخیره شده در نقد و بررسی //VerifyHtmlImageFile(model.description); return(Success("اطلاعات خدمات با موفقیت ویرایش شد.")); } else { if (Request.Files.Count > 0) { var fileEntity = AsefianFileContextHelper.Save(Request.Files[0], currentUser.id, GetCurrentIp(), "admin", "service"); model.fileId = fileEntity.id; model.fileName = fileEntity.fileName; } else { model.fileId = null; model.fileName = null; } var entity = new Service() { Order = model.order, Sku = model.translations.Single(x => x.languageId == Language.Persian.Id).title.ToStandardPersian(), FileId = model.fileId, FileName = model.fileName, StatusId = model.statusId, CreateUserId = currentUser.id, ModifyUserId = currentUser.id, CreateDate = GetDatetime(), ModifyDate = GetDatetime(), CreateIp = GetCurrentIp(), ModifyIp = GetCurrentIp() }; _context.Service.Add(entity); model.translations.ForEach(item => { var entityItem = entity.TranslationList.SingleOrDefault(x => x.LanguageId == item.languageId); if (entityItem != null) { entityItem.Title = item.title.ToStandardPersian(); entityItem.Description = item.description?.ToStandardPersian(); entityItem.Summary = item.summary?.ToStandardPersian(); } else { entityItem = new ServiceTranslation() { ServiceId = entity.Id, LanguageId = item.languageId, Title = item.title.ToStandardPersian(), Description = item.description?.ToStandardPersian(), Summary = item.summary?.ToStandardPersian(), }; _context.ServiceTranslation.Add(entityItem); } }); _context.SaveChanges(); // تایید تمام فایل های جدید ذخیره شده در نقد و بررسی //VerifyHtmlImageFile(model.description); return(Success("خدمات با موفقیت ایجاد شد.")); } } catch (Exception ex) { return(ServerError(ex)); } }