Files
data_organizer/DataOrganizer/Snapshot.cs
T

84 lines
2.7 KiB
C#

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 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;
foreach (var file in fileRecords)
{
var folders = file.FullPath.Split(new char[] { Path.DirectorySeparatorChar });
var currentPath = folderGroup.Path;
if (folders.Length == rootDepth + 1)
{
Root.AddFile(file);
Files.Add(file.FullPath, 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);
}
}
}
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 List<Duplicate> Duplicates { get; private set; }
public Folder(string name, string fullPath)
{
Name = name;
FullPath = fullPath;
}
public void AddFile(DB.FileSnapshot file)
{
Files.Add(file.FullPath, file);
}
public void AddDirectory(Folder folder)
{
Folders.Add(folder.FullPath, folder);
}
public struct Duplicate
{
DB.FileSnapshot FileA;
DB.FileSnapshot FileB;
}
}
}