public async Task UpdateEBook(Guid id, StreamFormDataBooks formData) { var model = await _context.ClamUserBooks.FindAsync(id); _context.Entry(model).Entity.BookTitle = formData.BookTitle; _context.Entry(model).Entity.Status = bool.Parse(formData.Status); _context.Entry(model).Entity.LastModified = DateTime.Now; _context.Entry(model).State = EntityState.Modified; _context.Update(model); await _context.SaveChangesAsync(); }
public async Task UpdateMusicTrack(Guid id, StreamFormDataMusic formData) { var model = await _context.ClamUserMusic.FindAsync(id); _context.Entry(model).Entity.SongTitle = formData.SongTitle; _context.Entry(model).Entity.SongArtist = formData.SongArtist; _context.Entry(model).Entity.Status = bool.Parse(formData.Status); _context.Entry(model).Entity.LastModified = DateTime.Now; _context.Entry(model).State = EntityState.Modified; _context.Update(model); await _context.SaveChangesAsync(); }
public async Task UpdateTicket(AreaUserTicket formData, string userName) { var model = _context.ClamUserSystemTickets.Find(formData.SystemTicketId); _context.Entry(model).Entity.TicketResponse = formData.TicketResponse; _context.Entry(model).Entity.TicketStatus = formData.TicketStatus; _context.Entry(model).Entity.DesignatedMember = userName; _context.Entry(model).Entity.LastModified = DateTime.Now; _context.Entry(model).State = EntityState.Modified; _context.Update(model); await _context.SaveChangesAsync(); }
public async Task UpdateCategory(Guid id, SectionAcademicRegister formData) { var model = await _context.ClamSectionAcademicCategories.FindAsync(id); _context.Entry(model).Entity.AcademicDisciplineDescription = formData.AcademicDisciplineDescription; _context.Entry(model).Entity.AcademicDisciplineTitle = formData.AcademicDisciplineTitle; _context.Entry(model).Entity.LastModified = DateTime.Now; _context.Entry(model).State = EntityState.Modified; _context.Update(model); Task.WaitAll(_context.SaveChangesAsync()); }
public async Task UpdateFilm(Guid id, StreamEditModelFilmflixDisplay formData) { var model = _context.ClamUserFilms.Find(formData.FilmId); _context.Entry(model).Entity.FilmTitle = formData.FilmTitle; _context.Entry(model).Entity.UrlEmbeddedVideo = FilePathUrlHelper.YoutubePathFilter(formData.UrlEmbeddedVideo); _context.Entry(model).Entity.Status = bool.Parse(formData.Status); _context.Entry(model).Entity.Year = formData.Year; _context.Entry(model).Entity.LastModified = DateTime.Now; _context.Entry(model).State = EntityState.Modified; _context.Update(model); await _context.SaveChangesAsync(); }
public async Task UpdateGenre(Guid id, SectionTVShowCategory model) { var trustedFileNameForDisplay = string.Empty; var trustedFilePathForStorage = string.Empty; var streamedFilePhysicalContent = new byte[0]; if (!ModelState.IsValid) { ModelState.AddModelError("Genre", $"The request couldn't be processed (Error 1)."); // Log error } streamedFilePhysicalContent = await FileHelpers.ProcessFormFile <StreamFileUploadDatabase>( model.FormFile, ModelState, _permittedExtentions, _fileSizeLimit); if (!ModelState.IsValid) { ModelState.AddModelError("Genre", $"The request couldn't be processed (Error 1)."); // Log error } var result = _context.Set <ClamSectionTVShowCategory>().Find(id); if (Directory.Exists(FilePathUrlHelper.GetFileDirectoryPath(result.ItemPath))) { System.IO.File.Delete(result.ItemPath); } trustedFilePathForStorage = String.Format("{0}\\{1}\\{2}", _targetImagePath, Guid.NewGuid().ToString(), Path.GetRandomFileName()); trustedFileNameForDisplay = String.Format("{0}_{1}", Guid.NewGuid(), WebUtility.HtmlEncode(model.FormFile.FileName)); Directory.CreateDirectory(trustedFilePathForStorage); using (var targetStream = System.IO.File.Create( Path.Combine(trustedFilePathForStorage, trustedFileNameForDisplay))) { await targetStream.WriteAsync(streamedFilePhysicalContent); //_logger.LogInformation( // "Uploaded file '{TrustedFileNameForDisplay}' saved to " + // "'{TargetFilePath}' as {TrustedFileNameForFileStorage}", // trustedFileNameForDisplay, trustedFilePathForStorage, // trustedFileNameForDisplay); } // TODO: Add update logic here var entity = await _context.ClamSectionTVShowCategories.FindAsync(id); _context.Entry(entity).Entity.Genre = model.Genre; _context.Entry(entity).Entity.ItemPath = Path.Combine(trustedFilePathForStorage, trustedFileNameForDisplay); _context.Entry(entity).Entity.LastModified = DateTime.Now; _context.Entry(entity).State = EntityState.Modified; _context.Update(entity); Task.WaitAll(_context.SaveChangesAsync()); }
public async Task <IActionResult> UploadModifiedVideo() { if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) { ModelState.AddModelError("File", $"The request couldn't be processed (Error 1)."); // Log error return(BadRequest(ModelState)); } // Accumulate the form data key-value pairs in the request (formAccumulator). var formAccumulator = new KeyValueAccumulator(); var trustedFileNameForDisplay = string.Empty; var untrustedFileNameForStorage = string.Empty; var trustedFilePathStorage = string.Empty; var trustedFileNameForFileStorage = string.Empty; var streamedFileImageContent = new byte[0]; var streamedFilePhysicalContent = new byte[0]; // Test Files List <byte[]> filesByteStorage = new List <byte[]>(); List <string> filesNameStorage = new List <string>(); var fileStoredData = new Dictionary <string, byte[]>(); List <string> storedPaths = new List <string>(); var boundary = MultipartRequestHelper.GetBoundary( MediaTypeHeaderValue.Parse(Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit); var reader = new MultipartReader(boundary, HttpContext.Request.Body); var section = await reader.ReadNextSectionAsync(); while (section != null) { var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse( section.ContentDisposition, out var contentDisposition); if (hasContentDispositionHeader) { if (MultipartRequestHelper .HasFileContentDisposition(contentDisposition)) { untrustedFileNameForStorage = contentDisposition.FileName.Value; // Don't trust the file name sent by the client. To display // the file name, HTML-encode the value. trustedFileNameForDisplay = WebUtility.HtmlEncode( contentDisposition.FileName.Value); if (!Directory.Exists(_targetFilePath)) { string path = String.Format("{0}", _targetFilePath); Directory.CreateDirectory(path); } //streamedFileContent = // await FileHelpers.ProcessStreamedFile(section, contentDisposition, // ModelState, _permittedExtentions, _fileSizeLimit); streamedFilePhysicalContent = await FileHelpers.ProcessStreamedFile( section, contentDisposition, ModelState, _permittedExtentions, _fileSizeLimit); filesNameStorage.Add(trustedFileNameForDisplay); filesByteStorage.Add(streamedFilePhysicalContent); fileStoredData.Add(trustedFileNameForDisplay, streamedFilePhysicalContent); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } } else if (MultipartRequestHelper .HasFormDataContentDisposition(contentDisposition)) { // Don't limit the key name length because the // multipart headers length limit is already in effect. var key = HeaderUtilities .RemoveQuotes(contentDisposition.Name).Value; var encoding = GetEncoding(section); if (encoding == null) { ModelState.AddModelError("File", $"The request couldn't be processed (Error 2)."); // Log error return(BadRequest(ModelState)); } using (var streamReader = new StreamReader( section.Body, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true)) { // The value length limit is enforced by // MultipartBodyLengthLimit var value = await streamReader.ReadToEndAsync(); if (string.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase)) { value = string.Empty; } formAccumulator.Append(key, value); if (formAccumulator.ValueCount > _defaultFormOptions.ValueCountLimit) { // Form key count limit of // _defaultFormOptions.ValueCountLimit // is exceeded. ModelState.AddModelError("File", $"The request couldn't be processed (Error 3)."); // Log error return(BadRequest(ModelState)); } } } } // Drain any remaining section body that hasn't been consumed and // read the headers for the next section. section = await reader.ReadNextSectionAsync(); } // Bind form data to the model var formData = new StreamFormFilmflixData(); var formValueProvider = new FormValueProvider( BindingSource.Form, new FormCollection(formAccumulator.GetResults()), CultureInfo.CurrentCulture); var bindingSuccessful = await TryUpdateModelAsync(formData, prefix : "", valueProvider : formValueProvider); var keyPathFolder = FilePathUrlHelper.GenerateKeyPath(formData.UserId); trustedFilePathStorage = String.Format("{0}\\{1}\\{2}\\{3}", //_targetFilePath, _targetFolderPath, keyPathFolder, formData.UserId, Path.GetRandomFileName()); if (!bindingSuccessful) { ModelState.AddModelError("File", "The request couldn't be processed (Error 5)."); // Log error return(BadRequest(ModelState)); } // **WARNING!** // In the following example, the file is saved without // scanning the file's contents. In most production // scenarios, an anti-virus/anti-malware scanner API // is used on the file before making the file available // for download or for use by other systems. // For more information, see the topic that accompanies // this sample app. Directory.CreateDirectory(trustedFilePathStorage); if (fileStoredData.Count == 2) { foreach (var item in fileStoredData) { using (var targetStream = System.IO.File.Create( Path.Combine(trustedFilePathStorage, item.Key))) { await targetStream.WriteAsync(item.Value); _logger.LogInformation( "Uploaded file '{TrustedFileNameForDisplay}' saved to " + "'{TargetFilePath}' as {TrustedFileNameForFileStorage}", item.Key, trustedFilePathStorage, item.Key); } storedPaths.Add(Path.Combine(trustedFilePathStorage, item.Key)); } var file = new ClamUserFilm() { ItemPath = storedPaths[0], ImagePath = storedPaths[1], WallpaperPath = storedPaths[3], FilmTitle = formData.FilmTitle, Size = streamedFilePhysicalContent.Length, DateAdded = DateTime.Now, UrlEmbeddedVideo = formData.UrlEmbeddedVideo, Year = formData.Year, Status = bool.Parse(formData.Status), UserId = formData.UserId }; var model = _context.ClamUserFilms.Find(formData.FilmId); _context.Entry(model).Entity.FilmTitle = formData.FilmTitle; _context.Entry(model).Entity.UrlEmbeddedVideo = formData.UrlEmbeddedVideo; _context.Entry(model).Entity.Year = formData.Year; _context.Entry(model).Entity.LastModified = DateTime.Now; _context.Entry(model).State = EntityState.Modified; _context.Update(model); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "Film")); } else { var file = new ClamUserFilm() { FilmTitle = formData.FilmTitle, Size = streamedFilePhysicalContent.Length, DateAdded = DateTime.Now, UrlEmbeddedVideo = formData.UrlEmbeddedVideo, Year = formData.Year, Status = bool.Parse(formData.Status), UserId = formData.UserId }; var model = _context.ClamUserFilms.Find(formData.FilmId); _context.Entry(model).Entity.FilmTitle = formData.FilmTitle; _context.Entry(model).Entity.UrlEmbeddedVideo = formData.UrlEmbeddedVideo; _context.Entry(model).Entity.Year = formData.Year; _context.Entry(model).Entity.LastModified = DateTime.Now; _context.Entry(model).State = EntityState.Modified; _context.Update(model); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "Film")); } }
public async Task UpdateProject(ProjectFormData model, ModelStateDictionary modelState, Guid id, string userName) { var user = await _userManager.FindByNameAsync(userName); var trustedFileNameForDisplay = string.Empty; var streamedFileImageContent = new byte[0]; var untrustedFileNameForStorage = string.Empty; var trustedFilePathStorage = string.Empty; var trustedFileNameForFileStorage = string.Empty; var checkState = string.Empty; // Get Project Id and update all new fields var getProject = await _context.ClamUserProjects.FindAsync(id); if (model.File != null) { streamedFileImageContent = await FileHelpers.ProcessFormFile <ProjectFormData>( model.File, modelState, _permittedExtentions, _fileSizeLimit); // Filter Check the state of the file if (!modelState.IsValid) { checkState = "ModelState is Invalid"; } untrustedFileNameForStorage = model.File.FileName; // Don't trust the file name sent by the client. To display // the file name, HTML-encode the value. trustedFileNameForDisplay = WebUtility.HtmlEncode( model.File.FileName); // Bind form data to the model var keyPathFolder = FilePathUrlHelper.GenerateKeyPath(user.Id); var generateKeyFolder = GenerateSecurity.Encode(user.Id); // Path Location & Directory Check trustedFilePathStorage = String.Format("{0}\\{1}\\{2}\\{3}", _targetFolderPath, keyPathFolder, generateKeyFolder, Path.GetRandomFileName()); Directory.CreateDirectory(trustedFilePathStorage); using (var fileStream = new FileStream(Path.Combine(trustedFilePathStorage, untrustedFileNameForStorage), FileMode.Create, FileAccess.Write)) { await model.File.CopyToAsync(fileStream); fileStream.Close(); } // Remove Physical Location await RemoveProject(id); _context.Entry(getProject).Entity.Title = model.Title; _context.Entry(getProject).Entity.Author = model.Author; _context.Entry(getProject).Entity.Description = model.Description; _context.Entry(getProject).Entity.Language = model.Language; _context.Entry(getProject).Entity.GithubLink = model.GithubLink; _context.Entry(getProject).Entity.Status = bool.Parse(model.Status); _context.Entry(getProject).Entity.LastModified = DateTime.Now; _context.Entry(getProject).Entity.ImageGifLocation = Path.Combine(trustedFilePathStorage, untrustedFileNameForStorage); _context.Update(getProject); await _context.SaveChangesAsync(); } else { _context.Entry(getProject).Entity.Title = model.Title; _context.Entry(getProject).Entity.Author = model.Author; _context.Entry(getProject).Entity.Description = model.Description; _context.Entry(getProject).Entity.Language = model.Language; _context.Entry(getProject).Entity.GithubLink = model.GithubLink; _context.Entry(getProject).Entity.Status = bool.Parse(model.Status); _context.Entry(getProject).Entity.LastModified = DateTime.Now; _context.Update(getProject); await _context.SaveChangesAsync(); } }