public async Task <IActionResult> Create([Bind("MediaKitFileId,DateCreated,DateModified,Description,Enabled,MediaType,OwnerId,ThumbnailURL,URL")] MediaKitFile mediaKitFile) { if (ModelState.IsValid) { _context.Add(mediaKitFile); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewData["OwnerId"] = new SelectList(_context.Owner, "OwnerId", "Name", mediaKitFile.OwnerId); return(View(mediaKitFile)); }
public async Task <IActionResult> EditMediaKitFile() { Boolean blnError = false; String errorMessage = ""; String errorElement = ""; Int32 id = 0; if (!string.IsNullOrEmpty(Request.Form["id"])) { bool result = Int32.TryParse(Request.Form["id"].ToString().Trim(), out id); if (id == 0) { blnError = true; errorMessage = "Media kit file cannot be updated."; errorElement = "id"; } } else { blnError = true; errorMessage = "Media kit file cannot be updated."; errorElement = "id"; } string newFileTags = Request.Form["FileTags"].ToString(); if (string.IsNullOrEmpty(newFileTags)) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the tags."; errorElement = "FileTags"; } else { if (newFileTags.Length < 2) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the tags."; errorElement = "FileTags"; } } DateTime copyrightDate = DateTime.Now; string rawFilename = Request.Form["url"].ToString().Trim().ToLower(); if (!string.IsNullOrEmpty(Request.Form["copyrightDate"])) { try { copyrightDate = Convert.ToDateTime(Request.Form["copyrightDate"].ToString().Trim()); } catch (Exception ex) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the copyright date."; errorElement = "copyrightDate"; } } else { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the copyright date."; errorElement = "copyrightDate"; } Int32 ownerid = 0; if (!string.IsNullOrEmpty(Request.Form["ownerId"])) { bool result = Int32.TryParse(Request.Form["ownerId"].ToString().Trim(), out ownerid); if (ownerid == 0) { if (!string.IsNullOrEmpty(Request.Form["ownerName"])) { Owner newOwner = new Owner(); newOwner.Name = Request.Form["ownerName"].ToString().Trim(); newOwner.Address = Request.Form["address"].ToString().Trim(); newOwner.Email = Request.Form["email"].ToString().Trim(); newOwner.Phone = Request.Form["phone"].ToString().Trim(); newOwner.SocialMedia = Request.Form["socialMedia"].ToString().Trim(); newOwner.Website = Request.Form["website"].ToString().Trim(); newOwner.DateCreated = DateTime.Now; _context.Add(newOwner); await _context.SaveChangesAsync(); ownerid = newOwner.OwnerId; } } } if (ownerid == 0) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the owner."; errorElement = "ownerId"; } string description = Request.Form["description"].ToString().Trim(); if (string.IsNullOrEmpty(description)) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the caption."; errorElement = "description"; } string altText = Request.Form["altText"].ToString().Trim(); if (string.IsNullOrEmpty(altText)) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the alt text."; errorElement = "altText"; } if (blnError) { return(Json(new { status = "error", message = errorMessage, element = errorElement })); } else { MediaKitFile mediaKitFile = _context.MediaKitFile.SingleOrDefault(m => m.MediaKitFileId == id); if (mediaKitFile != null) { string filename = ""; string rawFile = ""; var files = Request.Form.Files; if (Request.Form.Files.Count() >= 1) { filename = hostingEnv.WebRootPath + $@"\mediakitfiles\{mediaKitFile.URL}"; if (System.IO.File.Exists(filename)) { System.IO.File.Delete(filename); } foreach (var file in files) { rawFile = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); string tempNewFileName = Request.Form["url"].ToString().Trim(); string uploadFileExt = getFileExt(rawFile); mediaKitFile.MediaType = getFileType(uploadFileExt); string cleanFileName = ""; if (!string.IsNullOrEmpty(tempNewFileName)) { string customFileExt = getFileExt(tempNewFileName); int place = tempNewFileName.LastIndexOf(customFileExt); if (place == -1) { cleanFileName = tempNewFileName; } cleanFileName = tempNewFileName.Remove(place, customFileExt.Length).Insert(place, ""); filename = hostingEnv.WebRootPath + $@"\mediakitfiles\{cleanFileName + uploadFileExt}"; if (!string.IsNullOrEmpty(rawFilename)) { filename = hostingEnv.WebRootPath + $@"\mediakitfiles\{rawFilename + uploadFileExt}"; if (fileExists(rawFilename + uploadFileExt)) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the file."; errorElement = "files"; return(Json(new { status = "error", message = errorMessage, element = errorElement })); } else { mediaKitFile.URL = cleanFileName + uploadFileExt; } } else { filename = hostingEnv.WebRootPath + $@"\mediakitfiles\{rawFile}"; if (fileExists(rawFile)) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the file."; errorElement = "files"; return(Json(new { status = "error", message = errorMessage, element = errorElement })); } else { mediaKitFile.URL = rawFile; } } using (FileStream fs = System.IO.File.Create(filename)) { file.CopyTo(fs); fs.Flush(); } } else { //no custom url } } } else { if (mediaKitFile.URL.ToLower().Trim() != rawFilename) { string fileExt = getFileExt(mediaKitFile.URL); string cleanFileName = ""; string customFileExt = getFileExt(rawFilename); int place = rawFilename.LastIndexOf(customFileExt); if (place == -1) { cleanFileName = rawFilename; } cleanFileName = rawFilename.Remove(place, customFileExt.Length).Insert(place, ""); System.IO.File.Move(hostingEnv.WebRootPath + $@"\mediakitfiles\{mediaKitFile.URL}", hostingEnv.WebRootPath + $@"\mediakitfiles\{cleanFileName + fileExt}"); mediaKitFile.URL = cleanFileName + fileExt; } } mediaKitFile.Description = description; mediaKitFile.OwnerId = ownerid; mediaKitFile.DateModified = DateTime.Now; mediaKitFile.CopyrightDate = copyrightDate; mediaKitFile.AltText = altText; String tempEnabled = Request.Form["enabled"].ToString().Trim(); if (!string.IsNullOrEmpty(tempEnabled)) { if (tempEnabled.ToLower() == "true") { mediaKitFile.Enabled = true; } else { mediaKitFile.Enabled = false; } } _context.Update(mediaKitFile); await _context.SaveChangesAsync(); List <MediaKitFileTag> selectedMediaKitFileTags = _context.MediaKitFileTag.Where(m => m.MediaKitFileId == id).ToList(); foreach (MediaKitFileTag mediaKitFileTag in selectedMediaKitFileTags) { _context.MediaKitFileTag.Remove(mediaKitFileTag); } _context.SaveChanges(); List <MediaKitFileTag> mediaKitFileTags = _context.MediaKitFileTag.Where(m => m.MediaKitFileId == id).ToList(); foreach (MediaKitFileTag mediaKitFileTag in mediaKitFileTags) { _context.MediaKitFileTag.Remove(mediaKitFileTag); } _context.SaveChanges(); List <MediaKitFileTag> fileTags = new List <MediaKitFileTag>(); if (!string.IsNullOrEmpty(newFileTags)) { if (newFileTags.Substring(0, 1) == ",") { newFileTags = newFileTags.Substring(1, newFileTags.Length - 1); } if (newFileTags.Substring(newFileTags.Length - 1, 1) == ",") { newFileTags = newFileTags.Substring(0, newFileTags.Length - 1); } List <String> tags = newFileTags.Split(',').ToList(); for (var x = 0; x < tags.Count; x++) { _context.MediaKitFileTag.Add(new MediaKitFileTag() { MediaKitFileId = id, TagId = Convert.ToInt32(tags[x]) }); } await _context.SaveChangesAsync(); } } } return(Json("st")); }
public async Task <IActionResult> UploadFilesAjax() { Boolean blnError = false; String errorMessage = ""; String errorElement = ""; Int32 ownerid = 0; DateTime copyrightDate = DateTime.Now; string newFileTags = Request.Form["FileTags"].ToString(); if (string.IsNullOrEmpty(newFileTags)) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the tags."; errorElement = "FileTags"; } else { if (newFileTags.Length < 2) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the tags."; errorElement = "FileTags"; } } string rawFilename = Request.Form["url"].ToString().Trim().ToLower(); if (!string.IsNullOrEmpty(Request.Form["copyrightDate"])) { try { copyrightDate = Convert.ToDateTime(Request.Form["copyrightDate"].ToString().Trim()); } catch (Exception ex) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the copyright date."; errorElement = "copyrightDate"; } } else { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the copyright date."; errorElement = "copyrightDate"; } if (!string.IsNullOrEmpty(Request.Form["ownerId"])) { bool result = Int32.TryParse(Request.Form["ownerId"].ToString().Trim(), out ownerid); if (ownerid == 0) { if (!string.IsNullOrEmpty(Request.Form["ownerName"])) { Owner newOwner = new Owner(); newOwner.Name = Request.Form["ownerName"].ToString().Trim(); newOwner.Address = Request.Form["address"].ToString().Trim(); newOwner.Email = Request.Form["email"].ToString().Trim(); newOwner.Phone = Request.Form["phone"].ToString().Trim(); newOwner.SocialMedia = Request.Form["socialMedia"].ToString().Trim(); newOwner.Website = Request.Form["website"].ToString().Trim(); newOwner.DateCreated = DateTime.Now; _context.Add(newOwner); await _context.SaveChangesAsync(); ownerid = newOwner.OwnerId; } } } if (ownerid == 0) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the owner."; errorElement = "ownerId"; } string description = Request.Form["description"].ToString().Trim(); if (string.IsNullOrEmpty(description)) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the caption."; errorElement = "description"; } string altText = Request.Form["altText"].ToString().Trim(); if (string.IsNullOrEmpty(altText)) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the alt text."; errorElement = "altText"; } var files = Request.Form.Files; if (files.Count == 0) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the file."; errorElement = "files"; } if (blnError) { return(Json(new { status = "error", message = errorMessage, element = errorElement })); } else { var filename = ""; var rawFile = ""; var convertedFilename = ""; var fileExt = ""; foreach (var file in files) { rawFile = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); string[] fileParts = rawFile.ToString().Split('.'); if (fileParts.Length >= 2) { fileExt = "." + fileParts[fileParts.Length - 1]; } if (!string.IsNullOrEmpty(rawFilename)) { filename = hostingEnv.WebRootPath + $@"\mediakitfiles\{rawFilename + fileExt}"; convertedFilename = rawFilename + fileExt; if (fileExists(rawFilename + fileExt)) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the file."; errorElement = "files"; return(Json(new { status = "error", message = errorMessage, element = errorElement })); } } else { filename = hostingEnv.WebRootPath + $@"\mediakitfiles\{rawFile}"; convertedFilename = rawFile; if (fileExists(filename)) { blnError = true; errorMessage = "Media kit file cannot be updated because there is a problem with the file."; errorElement = "files"; return(Json(new { status = "error", message = errorMessage, element = errorElement })); } } using (FileStream fs = System.IO.File.Create(filename)) { file.CopyTo(fs); fs.Flush(); } } MediaKitFile newKitFile = new MediaKitFile(); MediaKitFile tempKitFile = new MediaKitFile(); MediaKitFileViewModel output = new MediaKitFileViewModel(); tempKitFile = _context.MediaKitFile.SingleOrDefault(m => m.URL.ToLower() == convertedFilename); if (tempKitFile == null) { newKitFile.DateCreated = DateTime.Now; newKitFile.DateModified = DateTime.Now; newKitFile.Description = description; newKitFile.Enabled = true; newKitFile.MediaType = getFileType(fileExt); newKitFile.URL = convertedFilename; newKitFile.OwnerId = ownerid; newKitFile.CopyrightDate = copyrightDate; newKitFile.AltText = altText; _context.Add(newKitFile); await _context.SaveChangesAsync(); List <MediaKitFileTag> fileTags = new List <MediaKitFileTag>(); if (!string.IsNullOrEmpty(newFileTags)) { if (newFileTags.Substring(0, 1) == ",") { newFileTags = newFileTags.Substring(1, newFileTags.Length - 1); } if (newFileTags.Substring(newFileTags.Length - 1, 1) == ",") { newFileTags = newFileTags.Substring(0, newFileTags.Length - 1); } List <String> tags = newFileTags.Split(',').ToList(); for (var x = 0; x < tags.Count; x++) { _context.MediaKitFileTag.Add(new MediaKitFileTag() { MediaKitFileId = newKitFile.MediaKitFileId, TagId = Convert.ToInt32(tags[x]) }); } await _context.SaveChangesAsync(); output.MediaKitFileId = newKitFile.MediaKitFileId; output.URL = newKitFile.URL; output.CopyrightDate = newKitFile.CopyrightDate; output.Description = newKitFile.Description; output.IconURL = newKitFile.URL; output.TagNames = _context.Tag.Where(t => tags.Contains(t.TagId.ToString())).Select(tt => new TagName { Name = tt.TagName }).ToList(); } } //return Json(newKitFile); return(Json(output)); } }
public async Task <IActionResult> Edit(int id, [Bind("MediaKitFileId,Description,Enabled,MediaType,OwnerId,URL,AltText")] MediaKitFile mediaKitFile) { var files = Request.Form.Files; if (Request.Form.Files.Count() >= 1) { MediaKitFile tempKitFile = _context.MediaKitFile.SingleOrDefault(m => m.MediaKitFileId == id); string filename = hostingEnv.WebRootPath + $@"\mediakitfiles\{tempKitFile.URL}"; if (System.IO.File.Exists(filename)) { System.IO.File.Delete(filename); } foreach (var file in files) { filename = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var fileExt = ""; string[] fileParts = filename.ToString().Split('.'); if (fileParts.Length >= 2) { fileExt = "." + fileParts[fileParts.Length - 1]; } filename = hostingEnv.WebRootPath + $@"\mediakitfiles\{Request.Form["url"] + fileExt}"; using (FileStream fs = System.IO.File.Create(filename)) { file.CopyTo(fs); fs.Flush(); } } } if (id != mediaKitFile.MediaKitFileId) { return(NotFound()); } if (ModelState.IsValid) { try { Int32 ownerid = 0; if (!string.IsNullOrEmpty(Request.Form["ownerId"])) { bool result = Int32.TryParse(Request.Form["ownerId"].ToString().Trim(), out ownerid); } if (!string.IsNullOrEmpty(Request.Form["ownerName"])) { Owner newOwner = new Owner(); newOwner.Name = Request.Form["ownerName"].ToString().Trim(); newOwner.Address = Request.Form["address"].ToString().Trim(); newOwner.Email = Request.Form["email"].ToString().Trim(); newOwner.Phone = Request.Form["phone"].ToString().Trim(); newOwner.SocialMedia = Request.Form["socialMedia"].ToString().Trim(); newOwner.Website = Request.Form["website"].ToString().Trim(); newOwner.DateCreated = DateTime.Now; _context.Add(newOwner); await _context.SaveChangesAsync(); ownerid = newOwner.OwnerId; } List <MediaKitFileTag> selectedMediaKitFileTags = _context.MediaKitFileTag.Where(m => m.MediaKitFileId == id).ToList(); foreach (MediaKitFileTag mediaKitFileTag in selectedMediaKitFileTags) { _context.MediaKitFileTag.Remove(mediaKitFileTag); } _context.SaveChanges(); List <MediaKitFileTag> fileTags = new List <MediaKitFileTag>(); string newFileTags = Request.Form["FileTags"].ToString(); if (!string.IsNullOrEmpty(newFileTags)) { if (newFileTags.Substring(0, 1) == ",") { newFileTags = newFileTags.Substring(1, newFileTags.Length - 1); } if (newFileTags.Substring(newFileTags.Length - 1, 1) == ",") { newFileTags = newFileTags.Substring(0, newFileTags.Length - 1); } List <String> tags = newFileTags.Split(',').ToList(); for (var x = 0; x < tags.Count; x++) { _context.MediaKitFileTag.Add(new MediaKitFileTag() { MediaKitFileId = id, TagId = Convert.ToInt32(tags[x]) }); } await _context.SaveChangesAsync(); } _context.Update(mediaKitFile); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MediaKitFileExists(mediaKitFile.MediaKitFileId)) { return(NotFound()); } else { throw; } } return(RedirectToAction("Index")); } List <MediaKitFileTag> mediaKitFileTags = _context.MediaKitFileTag.Where(m => m.MediaKitFileId == id).ToList(); foreach (MediaKitFileTag mediaKitFileTag in mediaKitFileTags) { _context.MediaKitFileTag.Remove(mediaKitFileTag); } _context.SaveChanges(); ViewData["OwnerId"] = new SelectList(_context.Owner, "OwnerId", "Name", mediaKitFile.OwnerId); return(View(mediaKitFile)); }