Added some helper functions to make removing files and folders in a Snapshot object easier.

This commit is contained in:
Admin
2020-12-03 20:28:24 -06:00
parent 826d788d9f
commit 2d52886e87
6 changed files with 404 additions and 147 deletions
+27 -2
View File
@@ -24,13 +24,18 @@ namespace DataOrganizer
ConnectionString = $"Data Source={DatabasePath}";
if (!Directory.Exists(new FileInfo(DatabasePath).Directory.FullName))
if (!File.Exists(DatabasePath))
{
Directory.CreateDirectory(new FileInfo(DatabasePath).Directory.FullName);
CreateDatabase();
}
}
public void DeleteDatabase()
{
if (File.Exists(DatabasePath)) File.Delete(DatabasePath);
}
private void CreateDatabase()
{
//TODO: this is tmp
@@ -345,6 +350,26 @@ namespace DataOrganizer
}
}
public string GetRootPathById(int id)
{
var query = @"SELECT path FROM folder_root WHERE root_id = @ID";
using (var connection = new SqliteConnection(ConnectionString))
{
using (var command = new SqliteCommand(query, connection))
{
command.Parameters.AddWithValue("ID", id);
connection.Open();
var reader = command.ExecuteReader();
reader.Read();
return reader["path"].ToString();
}
}
}
public bool UpdateNamingPatterById(int id, string newPattern)
{
var query = @"UPDATE main.naming_pattern SET regex = @pattern WHERE pattern_id = @ID";
@@ -417,7 +442,7 @@ namespace DataOrganizer
{
foreach (var file in files)
{
command.Parameters.AddWithValue("FullPath", file.PathNoRoot.Remove(0, rootLength));
command.Parameters.AddWithValue("FullPath", file.PathNoRoot.Remove(0, rootLength + 1)); //Remove the root and the trailing slash that the root folder would have.
command.Parameters.AddWithValue("Name", file.Name);
command.Parameters.AddWithValue("Size", file.Size);
command.Parameters.AddWithValue("DateCreated", file.CreationDate);
+82 -58
View File
@@ -37,10 +37,16 @@ namespace DataOrganizer
ProfileComboBox.SelectedIndexChanged += ProfileComboBox_SelectedIndexChanged;
RootFolderComboBox.SelectedIndexChanged += SelectedFolderComboBox_SelectedIndexChanged;
PrimaryFilesTreeView.NodeMouseClick += PrimaryFilesTreeView_NodeMouseClick;
FolderSelectButton.Click += FolderSelectButton_Click1;
ProfileComboBox.SelectedIndex = 0;
}
private void FolderSelectButton_Click1(object sender, EventArgs e)
{
WriteFilesToDb(GetActiveFolderGroup().Path);
}
private void PrimaryFilesTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if(ActiveSnapshotForPrimaryTree.Files.ContainsKey(e.Node.Name))
@@ -48,7 +54,7 @@ namespace DataOrganizer
var file = ActiveSnapshotForPrimaryTree.Files[e.Node.Name];
FileDetailsNameLabel.Text = $"Name: {file.Name}";
FileDetailsFullPathLabel.Text = $"Full Path: {file.PathNoRoot}";
FileDetailsFullPathLabel.Text = $"Full Path: {file.FullPath}";
}
}
@@ -64,7 +70,9 @@ namespace DataOrganizer
PrimaryTreeNodes.Clear();
var files = db.GetFilesForProfileByFolderGroup(profile.Id, folderGroup.Id);
ActiveSnapshotForPrimaryTree = new Snapshot(files, profile, folderGroup);
ActiveSnapshotForPrimaryTree = new Snapshot(files, folderGroup);
if(ActiveSnapshotForPrimaryTree.Directories.ContainsKey(@"W:\Test\Amagi Brilliant Park\Season 1")) ActiveSnapshotForPrimaryTree.DeleteFolder(ActiveSnapshotForPrimaryTree.Directories[@"W:\Test\Amagi Brilliant Park\Season 1"]);
foreach(var folder in ActiveSnapshotForPrimaryTree.Root.Folders)
{
@@ -164,82 +172,98 @@ namespace DataOrganizer
private void ScanRootFolderButton_Click(object sender, EventArgs e)
{
var folderGroup = GetActiveFolderGroup();
var rootDepth = folderGroup.Path.Split(new char[] { Path.DirectorySeparatorChar }).Length;
var newFiles = new List<FileInfo>();
var files = new List<FileInfo>();
var rootFolder = new DirectoryInfo(folderGroup.Path);
var pendingChanges = 0;
foreach (var dir in rootFolder.EnumerateDirectories()) newFiles.AddRange(ScanDirectory(dir));
foreach (var dir in rootFolder.EnumerateDirectories())
files.AddRange(ScanDirectory(dir));
foreach (var file in rootFolder.EnumerateFiles())
var snapShot = new Snapshot(files, folderGroup);
var newFiles = snapShot.Files.Values.Except(ActiveSnapshotForPrimaryTree.Files.Values);
var existingFiles = ActiveSnapshotForPrimaryTree.Files.Values.Except(snapShot.Files.Values);
var deletedFiles = new List<FileSnapshot>();
foreach(var existingFile in existingFiles)
{
if (!ActiveSnapshotForPrimaryTree.Files.ContainsKey(file.FullName))
if (!snapShot.Files.ContainsKey(existingFile.FullPath))
{
newFiles.Add(file);
existingFile.Status = FileSnapshot.FileStatus.PendingDelete;
deletedFiles.Add(existingFile);
pendingChanges++;
}
else if (existingFile.ModifiedDate != snapShot.Files[existingFile.FullPath].ModifiedDate)
{
existingFile.ModifiedDate = snapShot.Files[existingFile.FullPath].ModifiedDate;
pendingChanges++;
}
}
foreach (var newFile in newFiles)
pendingChanges += newFiles.Count();
foreach(var f in newFiles)
{
var folders = newFile.DirectoryName.Split(new char[] { Path.DirectorySeparatorChar });
var currentPath = folderGroup.Path;
// AddNewFileToPrimaryTreeNode(f);
}
for(var i = rootDepth; i < folders.Length; i++)
{
var previousPath = currentPath;
currentPath = Path.Combine(currentPath, folders[i]);
if(!PrimaryTreeNodes.ContainsKey(currentPath))
{
var node = new TreeNode(folders[i]) { ForeColor = Color.Blue, ToolTipText = currentPath };
PrimaryTreeNodes[currentPath] = node;
if (previousPath == folderGroup.Path)
{
PrimaryFilesTreeView.Nodes.Add(node);
}
else
{
PrimaryTreeNodes[previousPath].Nodes.Add(node);
}
}
}
//foreach (var file in rootFolder.EnumerateFiles())
//{
// if (!ActiveSnapshotForPrimaryTree.Files.ContainsKey(file.FullName))
// {
// newFiles.Add(file);
// }
//}
//foreach (var newFile in newFiles)
//{
// var folders = newFile.DirectoryName.Split(new char[] { Path.DirectorySeparatorChar });
// var currentPath = folderGroup.Path;
// for(var i = rootDepth; i < folders.Length; i++)
// {
// var previousPath = currentPath;
// currentPath = Path.Combine(currentPath, folders[i]);
// if(!PrimaryTreeNodes.ContainsKey(currentPath))
// {
// var node = new TreeNode(folders[i]) { ForeColor = Color.Blue, ToolTipText = currentPath };
// PrimaryTreeNodes[currentPath] = node;
// if (previousPath == folderGroup.Path)
// {
// PrimaryFilesTreeView.Nodes.Add(node);
// }
// else
// {
// PrimaryTreeNodes[previousPath].Nodes.Add(node);
// }
// }
// }
// PrimaryTreeNodes[currentPath].Nodes.Add(new TreeNode(newFile.Name) { ForeColor = Color.Blue });
//}
//WriteFilesToDb(folderGroup.Path);
}
private void AddNewFileToPrimaryTreeNode(List<FileSnapshot> files)
{
foreach(var file in files)
{
PrimaryTreeNodes[currentPath].Nodes.Add(new TreeNode(newFile.Name) { ForeColor = Color.Blue });
}
}
private List<FileInfo> ScanDirectory(DirectoryInfo dir)
{
var newFiles = new List<FileInfo>();
var files = new List<FileInfo>();
foreach (var d in dir.EnumerateDirectories())
{
UpdateStatusStrip($"Processing {d.FullName}");
newFiles.AddRange(ScanDirectory(d));
}
files.AddRange(dir.EnumerateFiles());
foreach(var file in dir.EnumerateFiles())
{
if (!ActiveSnapshotForPrimaryTree.Files.ContainsKey(file.FullName))
{
newFiles.Add(file);
}
else
{
//TODO: check for modifications
}
}
foreach (var folder in dir.EnumerateDirectories())
files.AddRange(ScanDirectory(folder));
foreach(var file in ActiveSnapshotForPrimaryTree.Files)
{
UpdateStatusStrip($"Checking for the existance of {file.Value.Name}");
if(File.Exists(file.Key))
{
//TODO: mark this file as deleted. Also scan the folders.
}
}
return newFiles;
return files;
}
private void WriteFilesToDb(string root)
{
+32 -12
View File
@@ -30,9 +30,11 @@
{
this.MainMenu = new System.Windows.Forms.MenuStrip();
this.FileMainMenu = new System.Windows.Forms.ToolStripMenuItem();
this.MainLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
this.ManageProfilesFileMainMenu = new System.Windows.Forms.ToolStripMenuItem();
this.FileManagerFileMainMenu = new System.Windows.Forms.ToolStripMenuItem();
this.MainLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
this.debugToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.deleteDBToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.MainMenu.SuspendLayout();
this.SuspendLayout();
//
@@ -41,7 +43,8 @@
this.MainMenu.GripMargin = new System.Windows.Forms.Padding(2, 2, 0, 2);
this.MainMenu.ImageScalingSize = new System.Drawing.Size(24, 24);
this.MainMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.FileMainMenu});
this.FileMainMenu,
this.debugToolStripMenuItem});
this.MainMenu.Location = new System.Drawing.Point(0, 0);
this.MainMenu.Name = "MainMenu";
this.MainMenu.Size = new System.Drawing.Size(800, 33);
@@ -57,6 +60,20 @@
this.FileMainMenu.Size = new System.Drawing.Size(54, 29);
this.FileMainMenu.Text = "&File";
//
// ManageProfilesFileMainMenu
//
this.ManageProfilesFileMainMenu.Name = "ManageProfilesFileMainMenu";
this.ManageProfilesFileMainMenu.Size = new System.Drawing.Size(270, 34);
this.ManageProfilesFileMainMenu.Text = "Manage &Profiles";
this.ManageProfilesFileMainMenu.Click += new System.EventHandler(this.ManageProfilesFileMainMenu_Click);
//
// FileManagerFileMainMenu
//
this.FileManagerFileMainMenu.Name = "FileManagerFileMainMenu";
this.FileManagerFileMainMenu.Size = new System.Drawing.Size(270, 34);
this.FileManagerFileMainMenu.Text = "&File Manager";
this.FileManagerFileMainMenu.Click += new System.EventHandler(this.FileManagerFileMainMenu_Click);
//
// MainLayoutPanel
//
this.MainLayoutPanel.ColumnCount = 2;
@@ -71,19 +88,20 @@
this.MainLayoutPanel.Size = new System.Drawing.Size(800, 791);
this.MainLayoutPanel.TabIndex = 1;
//
// ManageProfilesFileMainMenu
// debugToolStripMenuItem
//
this.ManageProfilesFileMainMenu.Name = "ManageProfilesFileMainMenu";
this.ManageProfilesFileMainMenu.Size = new System.Drawing.Size(270, 34);
this.ManageProfilesFileMainMenu.Text = "Manage &Profiles";
this.ManageProfilesFileMainMenu.Click += new System.EventHandler(this.ManageProfilesFileMainMenu_Click);
this.debugToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.deleteDBToolStripMenuItem});
this.debugToolStripMenuItem.Name = "debugToolStripMenuItem";
this.debugToolStripMenuItem.Size = new System.Drawing.Size(82, 29);
this.debugToolStripMenuItem.Text = "&Debug";
//
// FileManagerFileMainMenu
// deleteDBToolStripMenuItem
//
this.FileManagerFileMainMenu.Name = "FileManagerFileMainMenu";
this.FileManagerFileMainMenu.Size = new System.Drawing.Size(270, 34);
this.FileManagerFileMainMenu.Text = "&File Manager";
this.FileManagerFileMainMenu.Click += new System.EventHandler(this.FileManagerFileMainMenu_Click);
this.deleteDBToolStripMenuItem.Name = "deleteDBToolStripMenuItem";
this.deleteDBToolStripMenuItem.Size = new System.Drawing.Size(270, 34);
this.deleteDBToolStripMenuItem.Text = "&Delete DB";
this.deleteDBToolStripMenuItem.Click += new System.EventHandler(this.deleteDBToolStripMenuItem_Click);
//
// MainForm
//
@@ -110,6 +128,8 @@
private System.Windows.Forms.TableLayoutPanel MainLayoutPanel;
private System.Windows.Forms.ToolStripMenuItem ManageProfilesFileMainMenu;
private System.Windows.Forms.ToolStripMenuItem FileManagerFileMainMenu;
private System.Windows.Forms.ToolStripMenuItem debugToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem deleteDBToolStripMenuItem;
}
}
+6
View File
@@ -28,5 +28,11 @@ namespace DataOrganizer
var form = new FileViewer();
form.ShowDialog();
}
private void deleteDBToolStripMenuItem_Click(object sender, EventArgs e)
{
var db = new DB();
db.DeleteDatabase();
}
}
}
+257 -75
View File
@@ -11,60 +11,32 @@ namespace DataOrganizer
class Snapshot
{
public Folder Root { get; private set; }
public Dictionary<string, DB.FileSnapshot> Files { get; private set; } = new Dictionary<string, DB.FileSnapshot>();
public Dictionary<string, FileSnapshot> Files { get; private set; } = new Dictionary<string, FileSnapshot>();
public Dictionary<string, Folder> Directories { get; private set; } = new Dictionary<string, Folder>();
public Dictionary<string, Duplicate> FileDuplicates { get; private set; } = new Dictionary<string, Duplicate>();
//Contains files associated with particular, first level folders.
//So if the root folder group is X:\Anime and we have a folder path of x:\Anime\Death Note, then indexing this dictonary
//with the text "Death Note" would return all files in that folder.
public Dictionary<string, List<DB.FileSnapshot>> OrganizationFolders { get; private set; } = new Dictionary<string, List<DB.FileSnapshot>>();
public Dictionary<string, List<FileSnapshot>> OrganizationFolders { get; private set; } = new Dictionary<string, List<FileSnapshot>>();
public Snapshot(List<DB.FileSnapshot> fileRecords, DB.Profile profile, DB.FolderGroup folderGroup)
public Snapshot(List<DB.FileSnapshot> fileRecords, DB.FolderGroup folderGroup)
{
Root = new Folder(folderGroup.Name, folderGroup.Path);
Root = new Folder(folderGroup.Name, folderGroup.Path, null);
Directories.Add(Root.FullPath, Root);
if(folderGroup.Type == DB.OrganizationType.SingleLevel)
if (folderGroup.Type == DB.OrganizationType.SingleLevel)
ProcessFilesAsOneLevel(fileRecords, folderGroup);
//else if (folderGroup.Type == DB.OrganizationType.Flat)
// ProcessFilesAsFlat(fileRecords, folderGroup);
}
//var rootDepth = folderGroup.Path.Split(new char[] { Path.DirectorySeparatorChar }).Length;
public Snapshot(List<FileInfo> files, DB.FolderGroup folderGroup)
{
Root = new Folder(folderGroup.Name, folderGroup.Path, null);
Directories.Add(Root.FullPath, Root);
//foreach (var file in fileRecords)
//{
// var folders = file.PathNoRoot.Split(new char[] { Path.DirectorySeparatorChar });
// var currentPath = folderGroup.Path;
// if (folders.Length == rootDepth + 1)
// {
// Root.AddFile(file);
// Files.Add(file.PathNoRoot, file);
// continue;
// }
// for (var i = rootDepth; i < folders.Length - 1; i++)
// {
// string previousPath = currentPath;
// currentPath = Path.Combine(currentPath, folders[i]);
// if (!Directories.ContainsKey(currentPath))
// {
// var newFolder = new Folder(folders[i], currentPath);
// Directories[previousPath].AddDirectory(newFolder);
// Directories.Add(currentPath, newFolder);
// }
// }
// Directories[currentPath].AddFile(file);
// var duplicates = Files.Values.Where(x => x.Size == file.Size && x.Id != file.Id);
// Files.Add(file.PathNoRoot, file);
// foreach (var duplicate in duplicates)
// {
// FileDuplicates.Add(file.PathNoRoot, new Duplicate() { File = file, Duplicates = new List<DB.FileSnapshot> { duplicate } });
// }
//}
if (folderGroup.Type == DB.OrganizationType.SingleLevel)
ProcessFilesAsOneLevel(files, folderGroup);
}
private void ProcessFilesAsOneLevel(List<DB.FileSnapshot> files, DB.FolderGroup folderGroup)
@@ -79,9 +51,9 @@ namespace DataOrganizer
if(firstLevelFolder == null || firstLevelFolder.Name != folders[0])
{
firstLevelFolder = new Folder(folders[0], Path.Combine(folderGroup.Path, folders[0]));
firstLevelFolder = new Folder(folders[0], Path.Combine(folderGroup.Path, folders[0]), Root);
Directories.Add(orgFolder, firstLevelFolder);
OrganizationFolders.Add(orgFolder, new List<DB.FileSnapshot>());
OrganizationFolders.Add(orgFolder, new List<FileSnapshot>());
Root.AddDirectory(firstLevelFolder);
}
@@ -91,33 +63,117 @@ namespace DataOrganizer
currentPath = Path.Combine(currentPath, folders[i]);
if(!Directories.ContainsKey(currentPath))
{
var newFolder = new Folder(folders[i], currentPath);
var newFolder = new Folder(folders[i], currentPath, Directories[parentFolder]);
Directories.Add(currentPath, newFolder);
Directories[parentFolder].AddDirectory(newFolder);
}
}
var duplicates = OrganizationFolders[orgFolder].Where(x => x.Size == file.Size && x.Id != file.Id);
//var duplicates = OrganizationFolders[orgFolder].Where(x => x.Size == file.Size && x.Id != file.Id);
foreach(var dup in duplicates)
//foreach(var dup in duplicates)
//{
// if(FileDuplicates.ContainsKey(file.PathNoRoot))
// {
// FileDuplicates[file.PathNoRoot].Duplicates.Add(dup);
// }
// else FileDuplicates.Add(file.PathNoRoot, new Duplicate() { File = file, Duplicates = new List<DB.FileSnapshot>() { dup } });
//}
var snap = new FileSnapshot(file, firstLevelFolder, Directories[currentPath], folderGroup.Path);
OrganizationFolders[orgFolder].Add(snap);
Directories[currentPath].AddFile(snap);
Files.Add(Path.Combine(folderGroup.Path, file.PathNoRoot), snap);
}
}
private void ProcessFilesAsOneLevel(List<FileInfo> files, DB.FolderGroup folderGroup)
{
Folder firstLevelFolder = null;
foreach (var file in files)
{
//Add one to the length to remove the trailing slash in the root path.
var pathNoRoot = file.FullName.Remove(0, folderGroup.Path.Length + 1);
var folders = pathNoRoot.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
var orgFolder = Path.Combine(folderGroup.Path, folders[0]);
var currentPath = Path.Combine(folderGroup.Path, folders[0]);
if (firstLevelFolder == null || firstLevelFolder.Name != folders[0])
{
if(FileDuplicates.ContainsKey(file.PathNoRoot))
{
FileDuplicates[file.PathNoRoot].Duplicates.Add(dup);
}
else FileDuplicates.Add(file.PathNoRoot, new Duplicate() { File = file, Duplicates = new List<DB.FileSnapshot>() { dup } });
firstLevelFolder = new Folder(folders[0], Path.Combine(folderGroup.Path, folders[0]), Root);
Directories.Add(orgFolder, firstLevelFolder);
OrganizationFolders.Add(orgFolder, new List<FileSnapshot>());
Root.AddDirectory(firstLevelFolder);
}
OrganizationFolders[orgFolder].Add(file);
Directories[currentPath].AddFile(file);
Files.Add(file.PathNoRoot, file);
for (var i = 1; i < folders.Length - 1; i++)
{
var parentFolder = currentPath;
currentPath = Path.Combine(currentPath, folders[i]);
if (!Directories.ContainsKey(currentPath))
{
var newFolder = new Folder(folders[i], currentPath, Directories[parentFolder]);
Directories.Add(currentPath, newFolder);
Directories[parentFolder].AddDirectory(newFolder);
}
}
//var duplicates = OrganizationFolders[orgFolder].Where(x => x.Size == file.Size && x.Id != file.Id);
//foreach(var dup in duplicates)
//{
// if(FileDuplicates.ContainsKey(file.PathNoRoot))
// {
// FileDuplicates[file.PathNoRoot].Duplicates.Add(dup);
// }
// else FileDuplicates.Add(file.PathNoRoot, new Duplicate() { File = file, Duplicates = new List<DB.FileSnapshot>() { dup } });
//}
var snap = new FileSnapshot(file, firstLevelFolder, Directories[currentPath], folderGroup.Path);
OrganizationFolders[orgFolder].Add(snap);
Directories[currentPath].AddFile(snap);
Files.Add(snap.FullPath, snap);
}
}
private void ProcessFilesAsFlat(List<DB.FileSnapshot> files, DB.FolderGroup folderGroup)
{
}
public void DeleteFile(FileSnapshot file)
{
if (Files.ContainsKey(file.FullPath)) Files.Remove(file.FullPath);
Root.DeleteFile(file);
file.ParentFolder.DeleteFile(file);
}
public void DeleteFolder(Folder folder)
{
ClearSubFolders(folder);
}
private void ClearSubFolders(Folder folder)
{
if (Directories.ContainsKey(folder.FullPath)) Directories.Remove(folder.FullPath);
if (OrganizationFolders.ContainsKey(folder.FullPath)) OrganizationFolders.Remove(folder.FullPath);
Root.DeleteFolder(folder);
folder.Parent.DeleteFolder(folder);
foreach (var file in folder.Files)
{
var f = Files.Remove(file.Key);
}
foreach (var dir in folder.Folders)
ClearSubFolders(dir.Value);
}
public struct Duplicate
{
public DB.FileSnapshot File;
public List<DB.FileSnapshot> Duplicates;
public List<FileSnapshot> Duplicates;
}
}
@@ -125,28 +181,28 @@ namespace DataOrganizer
{
public string Name { get; private set; }
public string FullPath { get; private set; }
public Dictionary<string, DB.FileSnapshot> Files { get; private set; } = new Dictionary<string, DB.FileSnapshot>();
public Folder Parent { get; private set; }
public Dictionary<string, FileSnapshot> Files { get; private set; } = new Dictionary<string, FileSnapshot>();
public Dictionary<string, Folder> Folders { get; private set; } = new Dictionary<string, Folder>();
public Folder OrganizationalFolder { get; private set; }
public bool IsEmpty
{
get { return !(Files.Count > 0 || Folders.Count > 0); }
}
public bool IsRootFolder
{
get { return Parent == null; }
}
public Folder(string name, string fullPath)
public Folder(string name, string fullPath, Folder parent)
{
Name = name;
FullPath = fullPath;
Parent = parent;
}
public void AddFile(DB.FileSnapshot file)
public void AddFile(FileSnapshot file)
{
//var possibleDuplicates = Files.Values.Where(x => x.Size == file.Size);
Files.Add(file.PathNoRoot, file);
//foreach (var f in possibleDuplicates)
//{
// if (f.Id == file.Id) continue;
// Duplicates.Add(new Duplicate() { FileA = file, FileB = f });
//}
Files.Add(file.FullPath, file);
}
public void AddDirectory(Folder folder)
@@ -154,16 +210,142 @@ namespace DataOrganizer
Folders.Add(folder.FullPath, folder);
}
//public struct Duplicate
//{
// public DB.FileSnapshot FileA;
// public DB.FileSnapshot FileB;
//}
public List<FileSnapshot> GetAllPendingChanges()
{
var files = new List<FileSnapshot>();
files.AddRange(Files.Values.Where(x => x.Status != FileSnapshot.FileStatus.None));
foreach (var folder in Folders.Values)
files.AddRange(folder.GetAllPendingChanges());
return files;
}
public void DeleteFile(FileSnapshot file)
{
if (Files.ContainsKey(file.FullPath)) Files.Remove(file.FullPath);
}
public void DeleteFolder(Folder folder)
{
if (Folders.ContainsKey(folder.FullPath)) Folders.Remove(folder.FullPath);
}
}
public class FileSnapshot
{
public Folder OrganizationalFolder { get; }
public Folder ParentFolder { get; }
public string PathNoRoot { get; }
private string Root { get; }
private int _FileId { get; set; }
private string _Name { get; set; }
private DateTime _ModifiedDate { get; set; }
public DateTime CreatedDate { get; }
public long Size { get; }
private string _NamingPattern { get; set; }
public FileStatus Status { get; set; }
public int FileId
{
get { return _FileId; }
set
{
//Since this file is getting an ID, that means we've been commited to the database, so change the Status to reflect this.
Status = FileStatus.None;
FileId = value;
}
}
public DateTime ModifiedDate
{
get { return _ModifiedDate; }
set
{
if (Status != FileStatus.PendingInsert) Status = FileStatus.PendingUpdate;
_ModifiedDate = value;
}
}
public string NamingPattern
{
get { return _NamingPattern; }
set
{
if (Status != FileStatus.PendingInsert) Status = FileStatus.PendingUpdate;
_NamingPattern = value;
}
}
public string Name
{
get { return _Name; }
}
public FileSnapshot(DB.FileSnapshot file, Folder organizationalFolder, Folder parentFolder, string rootPath)
{
OrganizationalFolder = organizationalFolder;
ParentFolder = parentFolder;
var db = new DB();
if (file.PatternId != 0) _NamingPattern = db.GetNamingPatternById(file.PatternId);
PathNoRoot = file.PathNoRoot;
Root = rootPath;
_Name = file.Name;
_ModifiedDate = file.ModifiedDate;
CreatedDate = file.CreationDate;
Size = file.Size;
_FileId = file.Id;
Status = FileStatus.None;
}
public FileSnapshot(FileInfo file, Folder organizationalFolder, Folder parentFolder, string rootPath)
{
OrganizationalFolder = organizationalFolder;
ParentFolder = parentFolder;
_Name = file.Name;
//Add one to the length to remove the trailing slash in the root path.
PathNoRoot = file.FullName.Remove(0, rootPath.Length + 1);
Root = rootPath;
_ModifiedDate = file.LastWriteTimeUtc;
CreatedDate = file.CreationTimeUtc;
Size = file.Length;
_FileId = 0;
Status = FileStatus.PendingInsert;
}
public bool HasOwnNamingPattern
{
get
{
return !string.IsNullOrEmpty(NamingPattern);
}
}
public string FullPath
{
get
{
return Path.Combine(Root, PathNoRoot);
}
}
public bool HasPendingChanges
{
get { return Status != FileStatus.None; }
}
public enum FileStatus
{
PendingInsert,
PendingUpdate,
PendingDelete,
None
}
}
public class SnapshotComparsion