public IActionResult ViewImage(int Id, string Password) { ImagesDb db = new ImagesDb(_connectionString); var ids = HttpContext.Session.Get <List <int> >("Ids"); if (ids == null) { ids = new List <int>(); } var InSession = ids.Any(i => i == Id); var image = db.GetImage(Id); bool CorrectPassword = image.Password == Password; var message = (TempData["message"] != null); bool showImage = InSession || CorrectPassword; if (showImage) { db.UpdateImageViews(Id); } if (!InSession && CorrectPassword) { ids.Add(Id); HttpContext.Session.Set("Ids", ids); } ViewImageViewModel vm = new ViewImageViewModel { ShowMessage = message, ShowImage = showImage, Image = image }; if (TempData["message"] != null) { return(View(vm)); } if (Password != null) { TempData["message"] = "Invalid password"; message = true; return(Redirect($"/home/viewImage?id={Id}")); } return(View(vm)); }
public IActionResult Upload(IFormFile myImage, string password) { Guid guid = Guid.NewGuid(); string fileName = $"{guid}-{myImage.FileName}"; string finalFileName = Path.Combine(_enviroment.WebRootPath, "uploads", fileName); using var fileStream = new FileStream(finalFileName, FileMode.CreateNew); myImage.CopyTo(fileStream); ImagesDb db = new ImagesDb(_connectionString); Image image = new Image { Name = fileName, Password = password, Views = 0 }; db.AddImage(image); UploadViewModel vm = new UploadViewModel { Id = image.Id, Password = password }; return(View(vm)); }