public void HandleTableProcessorBegin(object sender, TableProcessorBeginEventArgs args) { Detail = new UploadDetail() { TableName = args.TableName, ClassName = args.ClassName, Begin = DateTime.Now, UploadHeader = Header }; Context.UploadDetails.Add(Detail); Context.SaveChanges(); }
public void TestCreateAndPopulate() { UploadContext context = new UploadContext(); UploadHeader header = new UploadHeader(); header.Begin = DateTime.Now; context.UploadHeaders.Add(header); DateTime dt = DateTime.Now; int minutes = 0; IList <String> tables = new List <String>() { "IN_GUEST", "IN_TRN", "IN_MSG", "IN_RES" }; foreach (String table in tables.OrderBy(s => s)) { minutes++; UploadDetail dtl = new UploadDetail() { TableName = table, Begin = dt.AddMinutes(minutes++), End = dt.AddMinutes(minutes++), UploadHeader = header }; context.UploadDetails.Add(dtl); } context.SaveChanges(); TestContext.WriteLine("Done - {0}", minutes); }
public ActionResult Register(RegisterViewModel model) { if (ModelState.IsValid) { using (UploadContext db = new UploadContext()) { User existUser = db.Users.FirstOrDefault(u => u.Login == model.UserName); if (existUser != null) { ModelState.AddModelError("", "This login already exist. Enter another login"); return(View()); } Role role = db.Roles.FirstOrDefault(r => r.Name == "User"); if (role != null) { var user = new User { Login = model.UserName, Password = model.Password, Role = role, RoleId = role.Id }; db.Users.Add(user); db.SaveChanges(); return(RedirectToAction("Login", "Account")); } } } // If we got this far, something failed, redisplay form return(View(model)); }
public SqlTableProcessorEventHandler(String connectionName) { Context = new UploadContext(connectionName); Header = new UploadHeader(); Header.Begin = DateTime.Now; Context.UploadHeaders.Add(Header); Context.SaveChanges(); }
public async Task <IActionResult> Post(UploadItem model, IFormFile fileToUpload) { if (fileToUpload != null) { // limit size to 100MB if (fileToUpload.Length > 104857600) { return(BadRequest("File is to large. Allowed size < 100MB")); } var privateFileName = Hash.GetSecureToken(_rndProvider); var safeFileName = WebUtility.HtmlEncode(Path.GetFileName(fileToUpload.FileName)); var fileExtension = Path.GetExtension(safeFileName); if (!string.IsNullOrEmpty(fileExtension)) { privateFileName += fileExtension; } var basePath = Path.Combine("wwwroot", "Uploads"); var filePath = Path.Combine(basePath, privateFileName); using (var memStream = new MemoryStream()) { await fileToUpload.CopyToAsync(memStream); var byteData = _protector.EncryptStream(memStream); Stream encryptedStream = new MemoryStream(byteData); using (var fileStream = new FileStream(filePath, FileMode.Create)) { await encryptedStream.CopyToAsync(fileStream); } } model.Token = $"https://{Request.Host}/upload/" + privateFileName; _context.UploadItems.Add(new UploadItem { Token = privateFileName, OriginalName = safeFileName, CreatedDate = DateTime.Now, ExpiredBy = DateTime.Now + TimeSpan.FromMinutes(5) }); _context.SaveChanges(); } return(View("~/Views/Upload/Index.cshtml", model)); }
private void ProcessFiles(List <Upload> uploads) { foreach (Upload u in uploads) { u.ProcessedTimestamp = DateTime.Now; _logger.LogInformation("File Id: " + u.Id + " Name: " + u.FileName + " processed at " + DateTime.Now.ToString("dddd, MMMM dd, yyyy HH:mm:ss.fffK")); } _context.SaveChanges(); }
private void ProcessFiles(List <Upload> uploads) { foreach (Upload u in uploads) { // Base64 decode file byte[] base64Data = System.Convert.FromBase64String(u.FileContent); string base64Decoded = System.Text.ASCIIEncoding.UTF8.GetString(base64Data); try { // Deserialize and process JSON file, persisting entities to database if (u.FileType.ToLower() == "patients") { List <Patient> patients = JsonSerializer.Deserialize <List <Patient> >(base64Decoded); foreach (Patient p in patients) { p.ModifiedDate = DateTime.Now; _patientContext.Patients.Add(p); } _patientContext.SaveChanges(); } if (u.FileType.ToLower() == "organizations") { List <Organization> organizations = JsonSerializer.Deserialize <List <Organization> >(base64Decoded); foreach (Organization o in organizations) { o.ModifiedDate = DateTime.Now; _organizationContext.Organizations.Add(o); } _organizationContext.SaveChanges(); } if (u.FileType.ToLower() == "claims") { List <Claim> claims = JsonSerializer.Deserialize <List <Claim> >(base64Decoded); foreach (Claim c in claims) { c.ModifiedDate = DateTime.Now; _claimContext.Claims.Add(c); } _claimContext.SaveChanges(); } } catch (Exception ex) { _logger.LogInformation("Exception Type: " + ex.GetType()); } // Update file processing status in the system u.ProcessedTimestamp = DateTime.Now; _logger.LogInformation("File Id: " + u.Id + " Name: " + u.FileType + " processed at " + DateTime.Now.ToString("dddd, MMMM dd, yyyy HH:mm:ss.fffK")); } _uploadContext.SaveChanges(); }
public ActionResult Create([Bind(Include = "ProdutoId,Nome,Quantidade")] Produto produto, HttpPostedFileBase file) { if (ModelState.IsValid) { db.Produtos.Add(produto); if (file != null && file.ContentLength > 0) { try { string path = Path.Combine(Server.MapPath("~/Public"), Path.GetFileName(file.FileName)); file.SaveAs(path); produto.Imagem = "Public/" + file.FileName; ViewBag.Message = "File uploaded successfully"; } catch (Exception ex) { ViewBag.Message = "ERROR:" + ex.Message.ToString(); } } else { ViewBag.Message = "You have not specified a file."; } db.SaveChanges(); return(RedirectToAction("Index")); } return(View(produto)); }
public IActionResult Create(Upload upload) { upload.UploadTimestamp = DateTime.Now; upload.ProcessedTimestamp = null; if (ModelState.IsValid) { _context.Uploads.Add(upload); _context.SaveChanges(); return(CreatedAtRoute("GetUpload", new { id = upload.Id }, upload)); } else { return(BadRequest(ModelState)); } }
public IActionResult Index(IFormFile files) { if (files != null) { if (files.Length > 0) { //Getting FileName var fileName = Path.GetFileName(files.FileName); //Getting file Extension var fileExtension = Path.GetExtension(fileName); // concatenating FileName + FileExtension var newFileName = String.Concat(Convert.ToString(Guid.NewGuid()), fileExtension); // Combines two strings into a path. var filepath = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "UploadAudioFiles")).Root + $@"\{newFileName}"; var objfiles = new Files() { DocumentId = 0, Name = newFileName, FileType = fileExtension, FilePath = "~/UploadAudioFiles/" + newFileName, CreatedOn = DateTime.Now }; using (var target = new MemoryStream()) { files.CopyTo(target); objfiles.DataFiles = target.ToArray(); } _context.Files.Add(objfiles); _context.SaveChanges(); using (FileStream fs = System.IO.File.Create(filepath)) { files.CopyTo(fs); fs.Flush(); } } } return(RedirectToAction("Index")); }
public IActionResult Create(Upload upload) { upload.UploadTimestamp = DateTime.Now; upload.ProcessedTimestamp = null; // Trim the header from the Base 64 encoded content int position = upload.FileContent.IndexOf("base64,"); string trimmedContent = upload.FileContent.Remove(0, position + 7); upload.FileContent = trimmedContent; if (ModelState.IsValid) { _context.Uploads.Add(upload); _context.SaveChanges(); return(CreatedAtRoute("GetUpload", new { id = upload.Id }, upload)); } else { return(BadRequest(ModelState)); } }
private void Cleanup(object state) { foreach (var item in _secretContext.SecretItems) { if (item.ExpiredBy < DateTime.Now) { _secretContext.SecretItems.Remove(item); _secretContext.SaveChanges(); } } foreach (var upload in _uploadContext.UploadItems) { if (upload.ExpiredBy < DateTime.Now) { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Uploads", upload.Token); _uploadContext.UploadItems.Remove(upload); _uploadContext.SaveChanges(); FileOperator.DeleteUploadedFile(path); } } // Clean-up leftover uploads that have no records in the DB var allUploadedFiles = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Uploads")); foreach (var uploadedFile in allUploadedFiles) { if (Path.GetFileName(uploadedFile) == ".placeholder.txt") { continue; } if (!_uploadContext.UploadItems.Any(b => b.Token == Path.GetFileName(uploadedFile))) { FileOperator.DeleteUploadedFile(uploadedFile); } } }
public void Commit() { _context.SaveChanges(); }