private void OnDeleted(object source, FileSystemEventArgs e) { if (!FileSystemUtilities.ExtensionIsSupported(e.FullPath) || !FileSystemUtilities.fileIsImportant(e.FullPath) || FileSystemUtilities.isTemporaryFile(e.FullPath)) { return; } String fileName = FileSystemUtilities.ExtractNameFromPath(e.Name); // This is done to account for move events nameOfDeletedFile = fileName; pathOfDeletedFile = e.FullPath; }
/// <summary> /// This is the most important method that keeps polling for documents and adding them to the queue /// It reads only true open events by keeping track of file close events /// </summary> public void monitor() { while (true) { ArrayList list; bool isStillOpen = false; list = GetOpenedWordFiles(); list.AddRange(GetOpenedExcelFiles()); list.AddRange(GetOpenedPowerpointFiles()); // remove multiple events for same open action and add newly opened files to openedFiles ArrayList listCopy = new ArrayList(list); foreach (Event currentEvent in listCopy) { if (openedFiles.Contains(currentEvent.File.Path)) { list.Remove(currentEvent); } else { openedFiles.Add(currentEvent.File.Path); } } ArrayList openedFilesCopy = new ArrayList(openedFiles); // Close the files which are no longer open foreach (String filePath in openedFilesCopy) { isStillOpen = false; foreach (Event currentEvent in listCopy) { if (currentEvent.File.Path.Equals(filePath)) { isStillOpen = true; } } if (!isStillOpen) { openedFiles.Remove(filePath); } } Console.WriteLine("Enqueueing: "); Print(FileSystemUtilities.FilterList(list)); foreach (Event currentEvent in list) { events.Enqueue(currentEvent); } list.Clear(); } }
private void OnCreated(object source, FileSystemEventArgs e) { if (!FileSystemUtilities.ExtensionIsSupported(e.FullPath) || !FileSystemUtilities.fileIsImportant(e.FullPath)) { return; } Console.WriteLine("You moved {0} and the deleted file last was {1}", e.Name, nameOfDeletedFile); String fileName = FileSystemUtilities.ExtractNameFromPath(e.Name); if (fileName.Equals(nameOfDeletedFile)) { Console.WriteLine("You moved {0}", e.Name); events.Enqueue(new Event(fileName, pathOfDeletedFile, e.FullPath, FileSystemUtilities.EVENT_ACTIONS.move)); } }
private void RenameHandler(Event currentEvent) { // Have to stop and consider what if the file is not in the database? String originalTransientFolderPath = FileSystemUtilities.GetTransientFolderPath(currentEvent.File.Path); Console.WriteLine("Original Path: " + originalTransientFolderPath); // If file never existed, don't need to do anything if (!SystemFile.Exists(originalTransientFolderPath)) { return; } String newTransientFolderPath = FileSystemUtilities.GetTransientFolderPath(currentEvent.File.NewPath); FileSystemUtilities.MoveFile(originalTransientFolderPath, newTransientFolderPath); SendRenameRequest(currentEvent.File); }
private void SendOpenRequest(File file) { NameValueCollection data = new NameValueCollection() { { "file_name", file.Name }, { "file_path", FileSystemUtilities.GetTransientFolderPath(file.Path) }, { "date", file.LastModified.ToString() } }; byte[] response = PostDataToServer(data, "open/"); if (response == null) { Console.WriteLine("An Error occured while posting Open Event to server"); } else { Console.WriteLine("Successfully sent open event on {0} to server", file.Name); } }
private bool SendMoveRequest(File file) { // Send the old path and the new path, that's it ;) NameValueCollection data = new NameValueCollection() { { "old_path", FileSystemUtilities.GetTransientFolderPath(file.Path) }, { "new_path", FileSystemUtilities.GetTransientFolderPath(file.NewPath) } }; byte[] response = PostDataToServer(data, "move/"); if (response == null) { return(false); } else { String responseString = System.Text.Encoding.Default.GetString(response); return(responseString.Equals("Exists") ? true : false); } }
private void MoveHandler(Event currentEvent) { // Have to stop and consider what if the file is not in the database? Should first send the request. // If the server says it's unimportant we can skip it String originalTransientFolderPath = FileSystemUtilities.GetTransientFolderPath(currentEvent.File.Path); Console.WriteLine("Original Path: " + originalTransientFolderPath); String newTransientFolderPath = FileSystemUtilities.GetTransientFolderPath(currentEvent.File.NewPath); Console.WriteLine("New Path: " + newTransientFolderPath); // If the original file existed then move it and send a put request otherwise do nothing if (!SystemFile.Exists(originalTransientFolderPath)) { return; } // Create the directories of destination if doesn't exist String directories = newTransientFolderPath.Substring(0, newTransientFolderPath.LastIndexOf(@"\")); System.IO.Directory.CreateDirectory(directories); FileSystemUtilities.MoveFile(originalTransientFolderPath, newTransientFolderPath); SendMoveRequest(currentEvent.File); }
private void SendModifyRequest(File file) { Console.WriteLine(FileSystemUtilities.GetTransientFolderPath(file.Path)); NameValueCollection data = new NameValueCollection() { { "name", file.Name }, { "path", FileSystemUtilities.GetTransientFolderPath(file.Path) }, { "expiration_date", file.LastModified.ToString() }, { "identifier", Environment.UserName }, { "file_size", new System.IO.FileInfo(file.Path).Length.ToString() } }; byte[] response = PostDataToServer(data, "modify/"); if (response == null) { Console.WriteLine("An Error occured while posting data to server"); } else { Console.WriteLine("Successfully created file {0} on server", file.Name); } }
private static void Initialize() { // load settings from configuration file Settings.directoriesToWatch = System.Configuration.ConfigurationManager.AppSettings["DirectoriesToWatch"].Split(','); Settings.apiUrl = System.Configuration.ConfigurationManager.AppSettings["ApiUrl"]; Settings.defaultDirectoriesToWatch = System.Configuration.ConfigurationManager.AppSettings["DefaultDirectoriesToWatch"].Split(','); if (NoDirectoriesSpecified()) { Settings.directoriesToWatch = Settings.defaultDirectoriesToWatch; } // Dropbox Folder Initializations Settings.dropboxDirectory = System.Configuration.ConfigurationManager.AppSettings["DropboxDirectory"]; Settings.transientCloudDirectoryName = Environment.UserName; Settings.transientCloudDirectoryPath = String.Concat(Settings.dropboxDirectory, Settings.transientCloudDirectoryName, @"\"); if (!Directory.Exists(Settings.transientCloudDirectoryPath)) { Directory.CreateDirectory(Settings.transientCloudDirectoryPath); } //Check if Web Server is Online Console.WriteLine(FileSystemUtilities.IsServerOnline() ? "Server is Online" : "Server is Offline"); }
private void OnRenamed(object source, RenamedEventArgs e) { if (!FileSystemUtilities.ExtensionIsSupported(e.FullPath) || !FileSystemUtilities.fileIsImportant(e.FullPath)) { return; } // If any temp file was renamed, send a modify event String oldFileName = FileSystemUtilities.ExtractNameFromPath(e.OldName); Console.WriteLine(oldFileName); if (FileSystemUtilities.isTemporaryFile(oldFileName)) { events.Enqueue(new Event(FileSystemUtilities.ExtractNameFromPath(e.Name), e.FullPath, FileSystemUtilities.EVENT_ACTIONS.modify)); return; } // Otherwise, it is a normal rename event so send a put request if (FileSystemUtilities.fileIsImportant(e.FullPath)) { Console.WriteLine(FileSystemUtilities.ExtractNameFromPath(e.FullPath) + "|" + e.FullPath); events.Enqueue(new Event(FileSystemUtilities.ExtractNameFromPath(e.FullPath), e.OldFullPath, e.FullPath, FileSystemUtilities.EVENT_ACTIONS.rename)); } }
private void ModifyHandler(Event currentEvent) { FileSystemUtilities.PutFileInTransientFolder(currentEvent.File); SendModifyRequest(currentEvent.File); }
// Special constructor for move, rename events public Event(String fileName, String oldPath, String newPath, FileSystemUtilities.EVENT_ACTIONS action) { File file = new File(fileName, oldPath, newPath); this.File = file; this.Action = action; }
public static void CopyFile(File file, String destination) { Console.WriteLine("Currently Processing: " + file.Name); destination = String.Concat(destination, file.Name); String source = file.Path; // Does destination file exist? // Don't copy if the last modified date of the file is the same as what is present in the directory if (!SystemFile.Exists(destination) || SystemFile.GetLastWriteTime(destination) <= SystemFile.GetLastWriteTime(source) && FileSystemUtilities.ExtensionIsSupported(source)) { try { System.IO.File.Copy(source, destination, true); } catch { Console.WriteLine("Couldn't copy at this time"); Console.WriteLine(source + "|" + destination); } } }