Files
data_organizer/DataOrganizer/Snapshot.cs
T

174 lines
6.6 KiB
C#

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
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, 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);
if(folderGroup.Type == DB.OrganizationType.SingleLevel)
ProcessFilesAsOneLevel(fileRecords, folderGroup);
//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.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])
{
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 = 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.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
{
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 Dictionary<string, Folder> Folders { get; private set; } = new Dictionary<string, Folder>();
public Folder OrganizationalFolder { get; private set; }
public Folder(string name, string fullPath)
{
Name = name;
FullPath = fullPath;
}
public void AddFile(DB.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 });
//}
}
public void AddDirectory(Folder folder)
{
Folders.Add(folder.FullPath, folder);
}
//public struct Duplicate
//{
// public DB.FileSnapshot FileA;
// public DB.FileSnapshot FileB;
//}
}
public class FileSnapshot
{
}
public class SnapshotComparsion
{
}
}