public static void RenameFolder(MasterFile file, string currentDirectory, string newDirectory) { DirectoryInfo currentDir = Directory.Exists(currentDirectory) ? new DirectoryInfo(currentDirectory) : throw new IOException($"Cannot rename/move the source directory '{currentDirectory}'. It does not exist."); try { if (!Directory.Exists(newDirectory)) { Directory.CreateDirectory(newDirectory); } if (currentDirectory.Equals(newDirectory, StringComparison.OrdinalIgnoreCase)) { var tempPath = newDirectory.Replace(newDirectory, @"_temp\"); // If this ends up throwing an error, create the temp path first and use Directory.Move(); // Make sure it gets deleted after the move currentDir.MoveTo(tempPath); file.Filepath = Path.Combine(tempPath, file.SysIOProps["Name"].ToString()); } var newFilepath = file.Filepath.Replace(currentDirectory, newDirectory); File.Move(file.Filepath, newFilepath); file.Filepath = newFilepath; file.SysIOProps["Directory"] = newDirectory; } catch (IOException ex) { var log = new LogWriter($"FileManipulator.RenameFolder - Can not rename (move) '{currentDirectory}' " + $"to '{newDirectory}'. {ex.GetType()}: \"{ex.Message}\""); } }
public static GracenoteSong Query(MasterFile file, bool includeAlbumInQuery = true) { var artist = file.TagLibProps["Artist"].ToString(); var songTitle = file.TagLibProps["Title"].ToString(); var album = file.TagLibProps["Album"].ToString(); var xml = string.Empty; if (includeAlbumInQuery) { xml = XmlGenerator.CreateRequest(artist, songTitle, album); } else { xml = XmlGenerator.CreateRequest(artist, songTitle); } var result = PostXmlData(xml); if (String.IsNullOrEmpty(result)) { var log = new LogWriter($"GracenoteWebAPI.Query() - Received a null result from the PostXMLData() method. " + $"ArgumentNullException: Application terminated."); throw new ArgumentNullException(nameof(result)); } return(new GracenoteSong(XmlParser.XmlToObject(result)[0])); }
public UpdateHelper(MasterFile file, List <string> propsToChange, List <string> oldVals, List <string> newVals) { _file = file; _propsToChange = propsToChange; _oldVals = oldVals; _newVals = newVals; }
public bool Contains(MasterFile file) { try { var entryExists = false; using (var connection = new SqlConnection(ConnectionString)) using (var cmd = new SqlCommand("SELECT COUNT(*) FROM dbo.TagLibFields WHERE (Filepath = @path)", connection)) { connection.Open(); cmd.Parameters.AddWithValue("@path", file.Filepath); if ((int)cmd.ExecuteScalar() > 0) { entryExists = true; } } return(entryExists); } catch (Exception ex) { string errorMessage = $"Database.Contains() - Cannot determine whether or not the database contains {file}. " + $"{ex.GetType()} : \"{ex.Message}\""; var log = new LogWriter(errorMessage); throw ex; } }
public void InsertUpdateDeleteRecord(MasterFile file, StatementType statementType) { if (statementType == StatementType.Insert && this.Contains(file)) { return; } else if (statementType == StatementType.Update && !this.Contains(file)) { statementType = StatementType.Insert; } var sysIOcmd = new SqlCommand("dbo.usp_SysIO_InsertUpdateDelete") { CommandType = CommandType.StoredProcedure }; sysIOcmd.Parameters.Add("@Filepath", SqlDbType.NVarChar).Value = file.Filepath; sysIOcmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = file.SysIOProps["Name"].ToString(); sysIOcmd.Parameters.Add("@Directory", SqlDbType.NVarChar).Value = file.SysIOProps["Directory"].ToString(); sysIOcmd.Parameters.Add("@Extension", SqlDbType.NVarChar).Value = file.SysIOProps["Extension"].ToString(); sysIOcmd.Parameters.Add("@CreationTime", SqlDbType.DateTime).Value = Convert.ToDateTime(file.SysIOProps["CreationTime"]); sysIOcmd.Parameters.Add("@LastAccessTime", SqlDbType.DateTime).Value = Convert.ToDateTime(file.SysIOProps["LastAccessTime"]); sysIOcmd.Parameters.Add("@Size", SqlDbType.BigInt).Value = Convert.ToInt64(file.SysIOProps["Size"]); sysIOcmd.Parameters.Add("@StatementType", SqlDbType.NVarChar).Value = statementType.ToString(); var tagLibcmd = new SqlCommand("dbo.usp_TagLib_InsertUpdateDelete") { CommandType = CommandType.StoredProcedure }; tagLibcmd.Parameters.Add("@Filepath", SqlDbType.NVarChar).Value = file.Filepath; tagLibcmd.Parameters.Add("@BitRate", SqlDbType.Int).Value = Convert.ToInt32(file.TagLibProps["BitRate"]); tagLibcmd.Parameters.Add("@MediaType", SqlDbType.NVarChar).Value = file.TagLibProps["MediaType"].ToString(); tagLibcmd.Parameters.Add("@Artist", SqlDbType.NVarChar).Value = file.TagLibProps["Artist"].ToString(); tagLibcmd.Parameters.Add("@Album", SqlDbType.NVarChar).Value = file.TagLibProps["Album"].ToString(); tagLibcmd.Parameters.Add("@Genres", SqlDbType.NVarChar).Value = file.TagLibProps["Genres"].ToString(); tagLibcmd.Parameters.Add("@Lyrics", SqlDbType.NVarChar).Value = file.TagLibProps["Lyrics"].ToString(); tagLibcmd.Parameters.Add("@Title", SqlDbType.NVarChar).Value = file.TagLibProps["Title"].ToString(); tagLibcmd.Parameters.Add("@Track", SqlDbType.Int).Value = Convert.ToInt32(file.TagLibProps["Track"]); tagLibcmd.Parameters.Add("@Year", SqlDbType.Int).Value = Convert.ToInt32(file.TagLibProps["Year"]); tagLibcmd.Parameters.Add("@Rating", SqlDbType.TinyInt).Value = Convert.ToByte(file.TagLibProps["Rating"]); tagLibcmd.Parameters.Add("@IsCover", SqlDbType.Bit).Value = (bool)file.TagLibProps["IsCover"] == true ? 1 : 0; tagLibcmd.Parameters.Add("@IsLive", SqlDbType.Bit).Value = (bool)file.TagLibProps["IsLive"] == true ? 1 : 0; tagLibcmd.Parameters.Add("@Duration", SqlDbType.BigInt).Value = ((TimeSpan)file.TagLibProps["Duration"]).Ticks; tagLibcmd.Parameters.Add("@StatementType", SqlDbType.NVarChar).Value = statementType.ToString(); if (statementType == StatementType.Delete) { sysIOcmd.CommandType = CommandType.StoredProcedure; tagLibcmd.CommandType = CommandType.StoredProcedure; ExecuteSqlCommands(new SqlCommand[] { sysIOcmd, tagLibcmd }); } else { ExecuteSqlCommands(new SqlCommand[] { tagLibcmd, sysIOcmd }); } PopulateIDColumn(); }
internal static IEnumerable <MasterFile> ExtractFilesFromFolder(string directory) { foreach (var path in Directory.EnumerateFiles(directory)) { if (IsMediaFile(path)) { yield return(MasterFile.GetMasterFileFromFilepath(path)); } } }
public Dictionary <string, bool> CheckMetadataEquality(MasterFile file) { return(new Dictionary <string, bool>() { { "Artist", file.TagLibProps["Artist"].ToString() == _artist ? true : false }, { "Album", file.TagLibProps["Album"].ToString() == _album ? true : false }, { "Title", file.TagLibProps["Title"].ToString() == _title ? true : false }, { "Track", Convert.ToInt32(file.TagLibProps["Track"]) == _track ? true : false }, { "Year", file.TagLibProps["Year"].ToString() == _year ? true : false }, { "Genres", file.TagLibProps["Genres"].ToString() == _genres ? true : false } }); }
public Dictionary <string, bool> CheckMetadataEquality(MasterFile file) { // Threw an unhandled NullReferenceException return(new Dictionary <string, bool>() { { "Artist", file.TagLibProps["Artist"].ToString() == StringCleaner.ToCleanTitleCase(ALBUM.ARTIST) ? true : false }, { "Album", file.TagLibProps["Album"].ToString() == StringCleaner.ToCleanTitleCase(ALBUM.TITLE) ? true : false }, { "Title", file.TagLibProps["Title"].ToString() == StringCleaner.ToCleanTitleCase(ALBUM.TRACK.TITLE) ? true : false }, { "Track", Convert.ToInt32(file.TagLibProps["Track"]) == ALBUM.TRACK.TRACK_NUM ? true : false }, { "Year", file.TagLibProps["Year"].ToString() == ALBUM.DATE ? true : false }, { "Genres", file.TagLibProps["Genres"].ToString() == ALBUM.GENRE ? true : false } }); }
public bool Equals(MasterFile file) { bool isEqual = true; foreach (var record in CheckMetadataEquality(file)) { if (record.Value == false) { isEqual = false; } } return(isEqual); }
public bool Contains(MasterFile file) { var entryExists = false; using (var connection = new SqlConnection(ConnectionString)) using (var cmd = new SqlCommand("SELECT COUNT(*) FROM dbo.TagLibFields WHERE (Filepath = @path)", connection)) { connection.Open(); cmd.Parameters.AddWithValue("@path", file.Filepath); if ((int)cmd.ExecuteScalar() > 0) { entryExists = true; } } return(entryExists); }
private bool Equals(MasterFile mf) { if (mf.TagLibProps["Artist"].ToString() != TagLibProps["Artist"].ToString() || mf.TagLibProps["Album"].ToString() != TagLibProps["Album"].ToString() || Convert.ToUInt32(mf.TagLibProps["Track"]) != Convert.ToUInt32(TagLibProps["Track"]) || Convert.ToInt32(mf.TagLibProps["BitRate"]) != Convert.ToInt32(TagLibProps["BitRate"]) || mf.TagLibProps["IsLive"].ToString() != TagLibProps["IsLive"].ToString() || mf.TagLibProps["IsCover"].ToString() != TagLibProps["IsCover"].ToString() || (TimeSpan)mf.TagLibProps["Duration"] != (TimeSpan)TagLibProps["Duration"]) { return(false); } else { return(true); } }
private static void ExtractFiles(string directory) { var filesInFolder = Directory.EnumerateFiles(directory, "", SearchOption.AllDirectories); foreach (var path in Directory.EnumerateFiles(directory)) { if (IsMediaFile(path)) { files.Add(MasterFile.GetMasterFileFromFilepath(path)); } } foreach (var subdirectory in Directory.EnumerateDirectories(directory)) { ExtractFiles(subdirectory); } }
public static void UpdateFile(MasterFile mf) { TagLib.File tglb = TagLib.File.Create(mf.Filepath); tglb.Tag.Artists = new string[] { mf.TagLibProps["Artist"].ToString() }; tglb.Tag.AlbumArtists = new string[] { mf.TagLibProps["Artist"].ToString() }; tglb.Tag.Performers = new string[] { mf.TagLibProps["Artist"].ToString() }; tglb.Tag.Album = mf.TagLibProps["Album"].ToString(); tglb.Tag.Genres = new string[] { mf.TagLibProps["Genres"].ToString() }; tglb.Tag.Lyrics = mf.TagLibProps["Lyrics"].ToString(); tglb.Tag.Title = mf.TagLibProps["Title"].ToString(); tglb.Tag.Track = Convert.ToUInt32(mf.TagLibProps["Track"]); tglb.Tag.Year = Convert.ToUInt32(mf.TagLibProps["Year"]); try { var tag = tglb.GetTag(TagLib.TagTypes.Id3v2); var frame = TagLib.Id3v2.PopularimeterFrame.Get((TagLib.Id3v2.Tag)tag, "WindowsUser", true); frame.Rating = Convert.ToByte(mf.TagLibProps["Rating"]); } catch (Exception) { } // tglb original artist (cover) -- Might have to check with some API if isCover is true to retrieve original artist tglb.Tag.Comment = (bool)mf.TagLibProps["IsLive"] ? "Live" : ""; try { tglb.Save(); } catch (IOException ex) { var log = new LogWriter($"Can not save database data to {mf.Filepath}. \"{ex.Message}\""); } catch (Exception ex) { var log = new LogWriter($"Can not save database data to {mf.Filepath}. \"{ex.Message}\""); } var fileInfo = new FileInfo(mf.Filepath); var nameInDB = mf.SysIOProps["Name"].ToString(); if (fileInfo.Name != nameInDB) { Rename(fileInfo, nameInDB); } }
public MasterFile GetMasterFile(Database db, string filepath) { return(MasterFile.GetMasterFileFromDB(db.QueryRecord(filepath))); }
public MasterFile GetMasterFile(string filepath) { return(MasterFile.GetMasterFileFromDB(QueryRecord(filepath))); }