Introduced a concept of folder depth in the database code, and updated the Snapshot class to take advantage of it. Only working for a 'OneLevel' Folder Group type.
This commit is contained in:
+110
-20
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace DataOrganizer
|
||||
{
|
||||
@@ -12,42 +13,112 @@ namespace DataOrganizer
|
||||
public Folder Root { get; private set; }
|
||||
public Dictionary<string, DB.FileSnapshot> Files { get; private set; } = new Dictionary<string, DB.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 Snapshot(List<DB.FileSnapshot> fileRecords, DB.Profile profile, DB.FolderGroup folderGroup)
|
||||
{
|
||||
Root = new Folder(folderGroup.Name, folderGroup.Path);
|
||||
Directories.Add(Root.FullPath, Root);
|
||||
|
||||
var rootDepth = folderGroup.Path.Split(new char[] { Path.DirectorySeparatorChar }).Length;
|
||||
if(folderGroup.Type == DB.OrganizationType.SingleLevel)
|
||||
ProcessFilesAsOneLevel(fileRecords, folderGroup);
|
||||
|
||||
foreach (var file in fileRecords)
|
||||
//var rootDepth = folderGroup.Path.Split(new char[] { Path.DirectorySeparatorChar }).Length;
|
||||
|
||||
//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 } });
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
private void ProcessFilesAsOneLevel(List<DB.FileSnapshot> files, DB.FolderGroup folderGroup)
|
||||
{
|
||||
Folder firstLevelFolder = null;
|
||||
|
||||
foreach (var file in files)
|
||||
{
|
||||
var folders = file.FullPath.Split(new char[] { Path.DirectorySeparatorChar });
|
||||
var currentPath = folderGroup.Path;
|
||||
var folders = file.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 (folders.Length == rootDepth + 1)
|
||||
if(firstLevelFolder == null || firstLevelFolder.Name != folders[0])
|
||||
{
|
||||
Root.AddFile(file);
|
||||
Files.Add(file.FullPath, file);
|
||||
continue;
|
||||
firstLevelFolder = new Folder(folders[0], Path.Combine(folderGroup.Path, folders[0]));
|
||||
Directories.Add(orgFolder, firstLevelFolder);
|
||||
OrganizationFolders.Add(orgFolder, new List<DB.FileSnapshot>());
|
||||
Root.AddDirectory(firstLevelFolder);
|
||||
}
|
||||
|
||||
for (var i = rootDepth; i < folders.Length - 1; i++)
|
||||
for(var i = 1; i < folders.Length - 1; i++)
|
||||
{
|
||||
string previousPath = currentPath;
|
||||
var parentFolder = currentPath;
|
||||
currentPath = Path.Combine(currentPath, folders[i]);
|
||||
|
||||
if (!Directories.ContainsKey(currentPath))
|
||||
if(!Directories.ContainsKey(currentPath))
|
||||
{
|
||||
var newFolder = new Folder(folders[i], currentPath);
|
||||
Directories[previousPath].AddDirectory(newFolder);
|
||||
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 } });
|
||||
}
|
||||
|
||||
OrganizationFolders[orgFolder].Add(file);
|
||||
Directories[currentPath].AddFile(file);
|
||||
Files.Add(file.PathNoRoot, file);
|
||||
}
|
||||
}
|
||||
|
||||
public struct Duplicate
|
||||
{
|
||||
public DB.FileSnapshot File;
|
||||
public List<DB.FileSnapshot> Duplicates;
|
||||
}
|
||||
}
|
||||
|
||||
public class Folder
|
||||
@@ -56,7 +127,7 @@ namespace DataOrganizer
|
||||
public string FullPath { get; private set; }
|
||||
public Dictionary<string, DB.FileSnapshot> Files { get; private set; } = new Dictionary<string, DB.FileSnapshot>();
|
||||
public Dictionary<string, Folder> Folders { get; private set; } = new Dictionary<string, Folder>();
|
||||
public List<Duplicate> Duplicates { get; private set; }
|
||||
public Folder OrganizationalFolder { get; private set; }
|
||||
|
||||
public Folder(string name, string fullPath)
|
||||
{
|
||||
@@ -66,7 +137,16 @@ namespace DataOrganizer
|
||||
|
||||
public void AddFile(DB.FileSnapshot file)
|
||||
{
|
||||
Files.Add(file.FullPath, 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 });
|
||||
//}
|
||||
}
|
||||
|
||||
public void AddDirectory(Folder folder)
|
||||
@@ -74,10 +154,20 @@ namespace DataOrganizer
|
||||
Folders.Add(folder.FullPath, folder);
|
||||
}
|
||||
|
||||
public struct Duplicate
|
||||
{
|
||||
DB.FileSnapshot FileA;
|
||||
DB.FileSnapshot FileB;
|
||||
}
|
||||
//public struct Duplicate
|
||||
//{
|
||||
// public DB.FileSnapshot FileA;
|
||||
// public DB.FileSnapshot FileB;
|
||||
//}
|
||||
}
|
||||
|
||||
public class FileSnapshot
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class SnapshotComparsion
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user