public IActionResult SetTags(int id, string tagLine = "") { if (string.IsNullOrEmpty(tagLine)) { tagLine = ""; } var newTags = Helpers.TagLine2Tags(tagLine); var didSomething = false; var result = ""; using (var db = new FusekiContext()) { var image = db.Images .Include(el => el.ImageTags) .FirstOrDefault(el => el.Id == id); var exiTags = image.ImageTags ?? new List <ImageTag>(); //exiTags = new List<ImageTag>(); var toDelete = new List <ImageTag>(); foreach (var tag in newTags) { if (exiTags.FirstOrDefault(el => el.Name == tag) == null) { var newImageTag = new ImageTag(); newImageTag.Image = image; newImageTag.Name = tag; db.Add(newImageTag); didSomething = true; result += $"\ncreated new tag for image: {tag}"; } } foreach (var exiTag in exiTags) { if (newTags.FirstOrDefault(el => el == exiTag.Name) == null) { db.Remove(exiTag); didSomething = true; result += $"\nremoved old tag on image: {exiTag.Name}"; } } if (didSomething) { db.SaveChanges(); } } var arm = new ActionResultModel(); arm.NextLink = "/image/viewall"; arm.NextLinkDescription = "back to image list"; arm.SetResult(result); return(View("ActionResult", arm)); }
public IActionResult UploadPost(UploadFileModel file, string tagLine) { //possibly accept the override filename var filename = file.Image.FileName; if (string.IsNullOrEmpty(file.Filename)) { file.Filename = file.Filename.ToLower(); } else { filename = file.Filename; } filename = CleanFilename(filename); if (!ValidateImageExtension(filename)) { var extFromImage = Path.GetExtension(file.Image.FileName); if (ValidateImageExtension(extFromImage)) { filename = filename + extFromImage; } else { throw new Exception("Invalid extension on filename" + filename); } } var targetPath = $"{PublishConfiguration.ImageSource}/{filename}"; if (System.IO.File.Exists(targetPath)) { throw new System.Exception("Already exists."); } using (var stream = System.IO.File.Create(targetPath)) { file.Image.CopyTo(stream); } using (var db = new FusekiContext()) { var image = new Image(); image.Filename = filename; db.Add(image); var tags = Helpers.TagLine2Tags(tagLine); foreach (var tag in tags) { var t = new ImageTag(); t.Image = image; t.Name = tag; db.Add(t); } db.SaveChanges(); } var arm = new ActionResultModel(); arm.NextLink = "/image/upload"; arm.NextLinkDescription = "Return to image upload"; arm.SetResult("Uploaded image"); return(View("ActionResult", arm)); }