/// <summary>Writes the artist ini.</summary> /// <param name="folder">The folder.</param> /// <param name="artist">The artist.</param> /// <exception cref="ArgumentNullException">MusicBrainzArtistGuid</exception> public void WriteArtistIni(MDBFolder folder, MDBArtist artist) { if (artist.MusicBrainzArtistGuid == null) { throw new ArgumentNullException("MusicBrainzArtistGuid"); } string artistIniFile = folder.GetFullPath(this, "artist.ini"); FileSystem.TouchFile(artistIniFile); var ini = IniReader.FromFile(artistIniFile); var writer = new IniWriter(ini); writer.WriteSetting("Artist", "Guid", artist.MusicBrainzArtistGuid.ToString()); var artists = new Set <string>(ini.ReadSection("Artists")); artists.Include(artist.Name); writer.WriteSection("Artists", artists); writer.Save(); }
/// <summary>Registers the file.</summary> /// <param name="mdbFolder">The folder.</param> /// <param name="fileName">Name of the file.</param> /// <param name="mdbFile">The MDB file.</param> /// <returns></returns> public MDBUpdateType RegisterFile(MDBFolder mdbFolder, string fileName, out MDBFile mdbFile) { mdbFile = default(MDBFile); string name = Path.GetFileNameWithoutExtension(fileName); string extension = Path.GetExtension(fileName); MDBFileType fileType = GetFileType(extension); if (fileType == MDBFileType.unknown) { return(MDBUpdateType.Ignored); } foreach (MDBFile f in Files.GetStructs( Search.FieldEquals(nameof(MDBFile.Name), name) & Search.FieldEquals(nameof(MDBFile.Extension), extension) & Search.FieldEquals(nameof(MDBFile.FileType), fileType) & Search.FieldEquals(nameof(MDBFile.FolderID), mdbFolder.ID))) { if (mdbFile.ID == 0) { mdbFile = f; } else { this.LogWarning("Delete duplicate file {0}", f); Files.Delete(f.ID); } } string fullPath = mdbFolder.GetFullPath(this, fileName); var fileInfo = new FileInfo(fullPath); long fileSize = fileInfo.Length; DateTime fileLastWriteTime = fileInfo.LastWriteTimeUtc; bool replaceDataset = false; if (mdbFile.Name != name) { replaceDataset = true; this.LogDebug("File new {0}", name); } else if (mdbFile.Extension != extension) { replaceDataset = true; this.LogDebug("File new {0}", name); } else if (mdbFile.DateTime.ToUniversalTime().Ticks != fileLastWriteTime.ToUniversalTime().Ticks) { replaceDataset = true; this.LogDebug("File datetime changed {0}", name); } else if (mdbFile.Size != fileSize) { replaceDataset = true; this.LogDebug("File size changed {0}", name); } else if (mdbFile.FileType != fileType) { replaceDataset = true; this.LogDebug("File type changed {0}", name); } else if (mdbFile.FolderID != mdbFolder.ID) { replaceDataset = true; this.LogDebug("File FolderID changed {0}", name); } if (replaceDataset) { if (mdbFile.ID > 0) { this.LogDebug("File changed? Scanning whole file again: {0}", fullPath); } mdbFile.FileType = fileType; mdbFile.FolderID = mdbFolder.ID; mdbFile.DateTime = fileLastWriteTime; mdbFile.Size = fileSize; mdbFile.Name = name; mdbFile.Extension = extension; } if (mdbFile.ID <= 0) { this.LogInfo("New file <cyan>{0}", fullPath); mdbFile.ID = Files.Insert(mdbFile); return(MDBUpdateType.New); } else if (replaceDataset) { this.LogInfo("Update file <cyan>{0}", fullPath); Files.Replace(mdbFile); return(MDBUpdateType.Updated); } return(MDBUpdateType.NoChange); }
bool SaveImage(byte[] data, MDBFolder mdbFolder, string name, ref MDBImage image, object obj) { string fullPath; var file = new MDBFile() { FolderID = mdbFolder.ID, Name = name, }; ImageType imgType; switch (image.Type) { case MDBImageType.ArtistMusicBanner: case MDBImageType.ArtistMusicLogo: case MDBImageType.ArtistMusicLogoHD: case MDBImageType.AlbumCDArt: fullPath = mdbFolder.GetFullPath(this, name + ".png"); file.Extension = ".png"; file.FileType = MDBFileType.png; imgType = ImageType.Png; image.MimeType = "image/png"; break; default: fullPath = mdbFolder.GetFullPath(this, name + ".jpg"); file.Extension = ".jpg"; imgType = ImageType.Jpeg; file.FileType = MDBFileType.jpeg; image.MimeType = "image/jpg"; break; } int width, height; using (var img = Bitmap32.Create(data)) { width = img.Width; height = img.Height; //save if not present at disk if (!File.Exists(fullPath)) { using (var ms = new MemoryStream()) { img.Save(ms, imgType, 99); data = ms.ToArray(); } } } bool writeFile = true; //find old dataset (check for replace) { if (TryGetFile(fullPath, false, out MDBFile mdbFile)) { file.ID = mdbFile.ID; if (mdbFile.GetFullPath(this) == fullPath) { writeFile = false; } else { string oldPath = mdbFile.GetFullPath(this); File.Delete(oldPath); } } } //save image data if (writeFile) { foreach (string oldFile in Directory.GetFiles(Path.GetDirectoryName(fullPath), Path.GetFileNameWithoutExtension(fullPath) + ".*")) { File.Delete(oldFile); if (TryGetFile(oldFile, false, out MDBFile mdbFile)) { Files.Delete(mdbFile.ID); } this.LogInfo("Deleted old file {0}", oldFile); } File.WriteAllBytes(fullPath, data); this.LogInfo("Saved new image {0}", fullPath); } //get fileinfo var fileInfo = new FileInfo(fullPath); //create file dataset file.DateTime = fileInfo.LastWriteTimeUtc; file.Size = fileInfo.Length; if (file.ID > 0) { Files.Replace(file); } else { file.ID = Files.Insert(file); } //update image dataset image.Width = width; image.Height = height; image.FileID = file.ID; if (Images.Exist(file.ID)) { Images.Replace(image); this.LogNotice("<cyan>Update {0} image<default> dataset for <yellow>{1} <default>{2}", image, obj, mdbFolder); return(false); } else { Images.Insert(image); this.LogNotice("<green>New {0} image<default> dataset for <yellow>{1} <default>{2}", image, obj, mdbFolder); return(true); } }