public void ChangeBackupDirectory() { const string backupDir = "C:\\backup\\"; var userPath = new UserPath(backupDir); Assert.True(userPath.BackupDirectory == backupDir); }
public void ChangeBackupFilenameAndDirectory() { const string backupDir = "C:\\backup\\"; const string backupFilename = "backupFile.txt"; var userPath = new UserPath(backupDir, backupFilename); Assert.True((userPath.BackupDirectory == backupDir) && (userPath.BackupFilename == backupFilename)); }
private void Add_Click(object sender, RoutedEventArgs e) { using (var dialog = new System.Windows.Forms.FolderBrowserDialog()) { if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var entry = new AnnotatedPathEntry(PathEntry.FromFilePath(dialog.SelectedPath)); UserPath.Add(entry); } } }
private void DoDelete(object sender, ExecutedRoutedEventArgs e) { if (systemList.IsFocused || e.Source == systemList) { SystemPath.Remove(GetSelectedEntry(e)); } if (userList.IsFocused || e.Source == userList) { UserPath.Remove(GetSelectedEntry(e)); } }
/// <summary> /// Remove paths that don't exist or are listed multiple times /// </summary> private void Clean_Click(object sender, RoutedEventArgs e) { lock (stateLock) { var sp = SystemPath.Select(_ => _.Path); var up = UserPath.Select(_ => _.Path); var cp = sp.Concat(up); var s = sp.Where((p, index) => p.Exists && !sp.Take(index).Contains(p)); var u = up.Where((p, index) => p.Exists && !cp.Take(sp.Count() + index).Contains(p)); SetPaths(s, u); } }
public void AddToUserPathWithBackup() { var userPath = new UserPath(); userPath.AddToPath("LibraryTests_AddToUserPathWithBackup", true); string path = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.User); bool isAddedToThePath = path.Contains("LibraryTests_AddToUserPathWithBackup;"); bool backupExists = File.Exists(userPath.BackupDirectory + userPath.BackupFilename); output.WriteLine(isAddedToThePath ? "Variable is added to the path" : "Variable is NOT added to the path"); output.WriteLine(backupExists ? "Path is backed up" : "Path is NOT backed up"); output.WriteLine(userPath.BackupDirectory + userPath.BackupFilename); Assert.True((isAddedToThePath && backupExists)); }
public ActionResult UserDetail() { UserPath userPath = new UserPath(); IEnumerable <User> userDetails = userPath.GetDB(); //foreach(User user in userDetails) //{ // if(user.Role!="Admin") // { // TempData["userDetails"] = user; // } //} TempData["UserDetails"] = userDetails; return(View()); }
private void Scan_Click(object sender, RoutedEventArgs e) { var currentPaths = CompletePath.Select(_ => _.Path); var search = new SearchOperation("C:\\", 4, new ScanningWindow()); Task <IEnumerable <string> > .Factory.StartNew(search.Run).ContinueWith(task => { if (task.IsFaulted) { return; // Too bad } var binDirectories = task.Result; binDirectories .Select(PathEntry.FromFilePath) .Where(path => !currentPaths.Contains(path)) .Each(path => UserPath.Add(new AnnotatedPathEntry(path))); }, TaskScheduler.FromCurrentSynchronizationContext()); }
private void Scan_Click(object sender, RoutedEventArgs e) { var currentPaths = CompletePath.Select(_ => _.Path); var window = new ScanningWindow(); var search = new SearchOperation("C:\\", 4, window); Task <IEnumerable <string> > .Factory.StartNew(search.Run); if (window.ShowDialog() == true) { UserPath.SupressNotification = true; search.Result .Select(PathEntry.FromFilePath) .Where(path => !currentPaths.Contains(path)) .Each(path => UserPath.Add(new AnnotatedPathEntry(path))); UserPath.SupressNotification = false; } }
public async Task <JsonResult> UploadFile(IList <IFormFile> files) { try { if (!files.Any()) { return(Json(new { state = 0, message = "There were no files to upload." })); } var appUser = await userManager.GetUserAsync(HttpContext.User); var document = dbContext.GetDocument(appUser, "documentName"); var file = files.ElementAt(0); if (file.Length > FileConverter.GetMaxUploadSizeInBytes()) { return(Json(new { state = 1, message = "Sorry, the maximum file size is " + FileConverter.GetMaxUploadSizeAsString() + "." })); } if (!FileConverter.IsUploadFileTypeValid(Path.GetExtension(file.FileName))) { return(Json(new { state = 1, message = "Sorry, only pdf, odt, doc and docx files can be upload." })); } var userPath = new UserPath(appUser.Id); var savePath = Path.Combine(new TmpPath().Path, file.FileName); using (var originalFileStream = file.OpenReadStream()) { using (var saveFileStream = System.IO.File.Create(savePath)) { originalFileStream.CopyTo(saveFileStream); } } var dbFileNames = dbContext.DocumentFiles .Where(x => x.Document.Name == "documentName" || true) .Select(x => x.Name); var diskFileNames = System.IO.Directory.EnumerateFiles(new UserPath(appUser.Id).UserDirectory) .Select(x => Path.GetFileName(x)); var uploadedFileData = FileConverter.GetUploadedFileData(appUser.Id, savePath, dbFileNames, diskFileNames); if (uploadedFileData.ConvertAndSave()) { dbContext.Add( new DocumentFile { Document = document, Name = uploadedFileData.DisplayedFileName, Path = uploadedFileData.SavedFileName, } ); dbContext.SaveChanges(); return(Json(new { state = 0, message = uploadedFileData.DisplayedFileName })); } else { return(Json(new { state = 1, message = "Sorry, the upload failed." })); } } catch (Exception err) { log.Error("", err); return(Json(new { state = 1, message = "Sorry, an error occurred." })); } }
public async Task <JsonResult> ApplyNow() { try { var appUser = await userManager.GetUserAsync(HttpContext.User); var document = dbContext.GetDocument(appUser, "documentName"); var documentEmail = (DocumentEmail)dbContext.GetDbObject("DocumentEmail", appUser, document); var userValues = (UserValues)dbContext.GetDbObject("UserValues", appUser, document); var customVariables = (IEnumerable <CustomVariable>)dbContext.GetDbObject("CustomVariables", appUser, document); var documentFiles = (IEnumerable <DocumentFile>)dbContext.GetDbObject("DocumentFiles", appUser, document); var sentApplication = new SentApplication { Document = document, UserValues = userValues, SentDate = DateTime.Now }; dbContext.Add(sentApplication); var pdfFilePaths = new List <string>(); var dict = getVariableDict(document.Employer, userValues, documentEmail, customVariables, document.JobName); var tmpDirectory = new TmpPath().Path; var userDirectory = new UserPath(appUser.Id).UserDirectory; foreach (var documentFile in documentFiles) { var currentFile = Path.Combine(userDirectory, documentFile.Path); var extension = Path.GetExtension(currentFile).ToLower(); var convertedToPdfPath = Path.Combine(tmpDirectory, $"{Path.GetFileNameWithoutExtension(documentFile.Path)}_{Guid.NewGuid().ToString()}.pdf"); if (extension == ".odt") { var tmpPath2 = Path.Combine(tmpDirectory, $"{Path.GetFileNameWithoutExtension(documentFile.Path)}_replaced_{Guid.NewGuid().ToString()}{extension}"); FileConverter.ReplaceInOdt(currentFile, tmpPath2, dict); currentFile = tmpPath2; } if (FileConverter.ConvertTo(currentFile, convertedToPdfPath)) { pdfFilePaths.Add(convertedToPdfPath); } else { throw new Exception("Failed to convert file " + documentFile.Name); } } var mergedPath = Path.Combine(new TmpPath().Path, "merged_" + Guid.NewGuid().ToString() + ".pdf"); if (!FileConverter.MergePdfs(pdfFilePaths, mergedPath)) { throw new Exception("Failed to merge pdfs."); } var newEmployer = new Employer(); dbContext.Add(newEmployer); var documentCopy = new Document(document) { Employer = newEmployer }; dbContext.Add(documentCopy); dbContext.Add(new UserValues(userValues) { AppUser = appUser }); dbContext.Add(new DocumentEmail(documentEmail) { Document = documentCopy }); foreach (var customVariable in customVariables) { dbContext.Add(new CustomVariable(customVariable) { Document = documentCopy }); } foreach (var documentFile in documentFiles) { dbContext.Add(new DocumentFile(documentFile) { Document = documentCopy }); } var attachments = new List <EmailAttachment>(); if (pdfFilePaths.Count >= 1) { var attachmentName = FileConverter.ReplaceInString(documentEmail.AttachmentName, dict); if (attachmentName.Length >= 1 && !attachmentName.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase)) { attachmentName = attachmentName + ".pdf"; } else if (!(attachmentName.Length >= 4 && attachmentName.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase))) { attachmentName = "Bewerbung.pdf"; } attachments.Add(new EmailAttachment { Path = mergedPath, Name = attachmentName }); } HelperFunctions.SendEmail( new EmailData { Attachments = attachments, Body = FileConverter.ReplaceInString(documentEmail.Body, dict), Subject = FileConverter.ReplaceInString(documentEmail.Subject, dict), ToEmail = document.Employer.Email, FromEmail = userValues.Email, FromName = (userValues.Degree == "" ? "" : userValues.Degree + " ") + userValues.FirstName + " " + userValues.LastName } ); dbContext.SaveChanges(); return(Json(new { status = 0 })); } catch (Exception err) { log.Error("", err); return(Json(new { status = 1 })); } }
public static UploadedFileData GetUploadedFileData (string userId, string filePath, IEnumerable <string> dbFileNames, IEnumerable <string> diskFileNames) { try { string userDirectory = new UserPath(userId).UserDirectory; var fileName = Path.GetFileName(filePath); var extension = Path.GetExtension(filePath).ToLower(); switch (extension) { case ".pdf": { var unusedDiskFileName = FindUnusedFileName(fileName, diskFileNames); var savePath = Path.Combine(userDirectory, unusedDiskFileName); var unusedDbFileName = FindUnusedFileName(fileName, dbFileNames); return (new UploadedFileData { OriginalFilePath = filePath, SavedFileName = unusedDiskFileName, DisplayedFileName = unusedDbFileName, ConvertAndSave = () => { if (MergePdfs(new[] { filePath }, Path.Combine(new TmpPath().Path, "mypdf.pdf"))) { File.Copy(filePath, savePath, true); return true; } else { return false; } } }); } case ".odt": { var unusedDiskFileName = FindUnusedFileName(fileName, diskFileNames); var savePath = Path.Combine(userDirectory, unusedDiskFileName); var unusedDbFileName = FindUnusedFileName(fileName, dbFileNames); return (new UploadedFileData { OriginalFilePath = filePath, SavedFileName = unusedDiskFileName, DisplayedFileName = unusedDbFileName, ConvertAndSave = () => { var tmpPath = new TmpPath(); var path = Path.Combine(tmpPath.Path, "mypdf.pdf"); if (ConvertTo(filePath, path)) { File.Copy(filePath, savePath, true); return true; } else { return false; } } }); } case ".doc": case ".docx": { var unusedDiskFileName = FindUnusedFileName(Path.ChangeExtension(fileName, ".odt"), diskFileNames); var savePath = Path.Combine(userDirectory, unusedDiskFileName); var unusedDbFileName = FindUnusedFileName(Path.ChangeExtension(fileName, ".odt"), dbFileNames); return (new UploadedFileData { OriginalFilePath = filePath, SavedFileName = unusedDiskFileName, DisplayedFileName = unusedDbFileName, ConvertAndSave = () => { var tmpPath = new TmpPath(); var outputPath = Path.Combine(tmpPath.Path, "myodt.odt"); if (ConvertTo(filePath, outputPath)) { File.Copy(outputPath, savePath, true); return true; } else { return false; } } }); } default: throw new Exception("Filetype not found " + extension); } } catch (Exception e) { throw; } }