private async Task<List<File>> UpdateDb(StorageFolder currentfolder, Directory parentDirectory, List<string> relativepath)
{
List<File> songlist = new List<File>();
//
// Get all files and sub folders in current folder
//
var files = await currentfolder.GetFilesAsync();
//
// Get all files matching the avaible playback froamts
//
var matchingFiles = files.Where<StorageFile>(o => allowedFileTypes.Contains<string>(o.FileType.ToLower()));
var folders = await currentfolder.GetFoldersAsync();
//
// Add the current foldername to list of relativepaths
//
relativepath.Add(currentfolder.DisplayName);
//
// Get the full current relative path
//
string currentrelativepath = string.Empty;
for (int i = 0; i < relativepath.Count; i++)
{
currentrelativepath += relativepath[i];
currentrelativepath += (i < relativepath.Count - 1 && i > 0) ? "/" : string.Empty;
}
//
// Check if the Folder already exist, if not create the folder
//
if (await GetDirectory(currentfolder.DisplayName, parentDirectory.DirectoryId) == null)
{
//
// Create new Directory from current folder
//
Directory newDirectory = new Directory()
{
Name = currentfolder.DisplayName,
Path = currentfolder.Path,
RelativePath = currentrelativepath,
ParentDirectoryId = parentDirectory.DirectoryId,
FolderRelativeId = currentfolder.FolderRelativeId
};
//
// Add the Directory for current folder to Directories
//
await Dbconnection.InsertAsync(newDirectory);
}
//
// Get the Directory for the current folder
//
Directory currentDirectory = await GetDirectory(currentfolder.DisplayName, parentDirectory.DirectoryId) ;
//
// Check if files don't exist in the filesystem any more and remove the files from the database
//
var currentDirectoryFiles = await Dbconnection.QueryAsync<File>("SELECT * FROM Files WHERE DirectoryId = " + currentDirectory.DirectoryId);
//
// Get any files to remove
//
IEnumerable<File> filestoremove =
from cfile in currentDirectoryFiles
where !((from file in matchingFiles select file.Name).ToList<string>().Contains<string>(cfile.Name))
select cfile;
//
// Remove files that don't exist on filesystem anymore
//
foreach (File removefile in filestoremove)
{
await Dbconnection.DeleteAsync(removefile);
}
//
// Check if files don't exist in any more in filesystem and remove the files from the database
//
var currentSubDirectory = await Dbconnection.QueryAsync<Directory>("SELECT * FROM Directories WHERE ParentDirectoryId = " + currentDirectory.DirectoryId);
//
// Get any directories to remove
//
IEnumerable<Directory> directroiesstoremove =
from cDirectory in currentSubDirectory
where !((from folder in folders
select folder.DisplayName).ToList<string>().Contains<string>(cDirectory.Name))
select cDirectory;
//
// Remove directories that don't exist on filesystem anymore
//
foreach (Directory removedirectory in directroiesstoremove)
{
List<Directory> subDirToRemove = await GetSubDirectories(removedirectory);
foreach(Directory d in subDirToRemove)
{
var removesubdirectoryFiles = await Dbconnection.QueryAsync<File>("SELECT * FROM Files WHERE DirectoryId = " + d.DirectoryId);
foreach (File f in removesubdirectoryFiles)
{
await Dbconnection.DeleteAsync(f);
}
await Dbconnection.DeleteAsync(d);
}
var removedirectoryFiles = await Dbconnection.QueryAsync<File>("SELECT * FROM Files WHERE DirectoryId = " + removedirectory.DirectoryId);
foreach(File f in removedirectoryFiles)
{
await Dbconnection.DeleteAsync(f);
}
await Dbconnection.DeleteAsync(removedirectory);
}
foreach (StorageFile storagefile in matchingFiles)
{
//
// Check if the file aready exist in the database, if not the add the file
//
if (await GetFile(storagefile.Name, currentDirectory.DirectoryId) == null)
{
Windows.Storage.FileProperties.MusicProperties mp = null;
int? bitrate = null;
mp = mp ?? await storagefile.Properties.GetMusicPropertiesAsync();
Tuple<int, string> genre = await GetGenreId(mp.Genre);
File newFile = new File()
{
Name = storagefile.Name,
FilePath = storagefile.Path,
DirectoryId = currentDirectory.DirectoryId,
RelativePath = (string.IsNullOrEmpty(currentrelativepath)) ? storagefile.Name : currentrelativepath + "/" + storagefile.Name,
Title = mp.Title,
Date = (mp.Year > 0) ? mp.Year.ToString() : string.Empty,
AlbumArtist = mp.AlbumArtist,
Time = Convert.ToInt32(mp.Duration.TotalSeconds),
Disc = -1,
PositionId = -1,
Prio = 0,
Pos = 0,
LastModified = storagefile.DateCreated.ToString(),
Track = Convert.ToInt32(mp.TrackNumber),
Album = mp.Album,
Artist = mp.Artist,
Genre = genre.Item2,
GenreId = genre.Item1,
AlbumId = await GetAlbumId(mp.Album),
ArtistId = await GetArtistId(mp.Artist),
FolderRelativeId = storagefile.FolderRelativeId,
Bitrate = bitrate ?? Convert.ToInt32(mp.Bitrate),
Type = "File"
};
songlist.Add(newFile);
}
}
//
// Loop through sub folders and add any files to current songlist
//
foreach (StorageFolder folder in folders)
{
songlist.AddRange(await UpdateDb(folder, currentDirectory, relativepath));
}
//
// Remove this current folder from relativepaths
//
if (relativepath.Count > 0) relativepath.RemoveAt(relativepath.Count - 1);
return songlist;
}