/// <summary>Tries to get file with the specified fullpath.</summary> /// <param name="fullPath">The full path.</param> /// <param name="checkExtension">if set to <c>true</c> check if the file exists with another extension.</param> /// <param name="file">The file.</param> /// <returns>Returns true on success, false otherwise</returns> public bool TryGetFile(string fullPath, bool checkExtension, out MDBFile file) { MDBFolder folder = Folders.TryGetStruct(nameof(MDBFolder.Name), Path.GetDirectoryName(fullPath)); if (folder.ID > 0) { Search search = Search.FieldEquals(nameof(MDBFile.FolderID), folder.ID) & Search.FieldEquals(nameof(MDBFile.Name), Path.GetFileNameWithoutExtension(fullPath)); if (checkExtension) { search &= Search.FieldEquals(nameof(MDBFile.Extension), Path.GetExtension(fullPath)); } var files = Files.GetStructs(search); for (int i = 1; i < files.Count; i++) { this.LogWarning("Removing duplicate file registration. <red>{0}", files[i]); Files.Delete(files[i].ID); } if (files.Count > 0) { file = files.First(); return(true); } } file = default(MDBFile); return(false); }
/// <summary>Gets the full name of the image file.</summary> /// <param name="folder">The folder.</param> /// <param name="fileName">Name of the file without extension.</param> /// <returns></returns> public string GetImageFileNameWithExtension(MDBFolder folder, string fileName) { string[] files = Directory.GetFiles(folder.Name, fileName + ".*"); if (!files.Any()) { return(null); } return(files.First()); }
/// <summary>Saves the album image.</summary> /// <param name="folder">The folder.</param> /// <param name="name">The name.</param> /// <param name="imageType">Type of the image.</param> /// <param name="album">The album.</param> /// <param name="data">The data.</param> /// <exception cref="Exception">Invalid image type!</exception> public void SaveAlbumImage(MDBFolder folder, string name, MDBImageType imageType, MDBAlbum album, byte[] data) { var image = new MDBImage() { MusicBrainzGuid = album.MusicBrainzReleaseGroupGuid, Type = imageType, }; if (!image.IsAlbumArt) { throw new Exception("Invalid image type!"); } SaveImage(data, folder, name, ref image, album); }
/// <summary>Saves the artist image.</summary> /// <param name="folder">The folder.</param> /// <param name="name">The name.</param> /// <param name="imageType">Type of the image.</param> /// <param name="artist">The artist.</param> /// <param name="data">The data.</param> /// <exception cref="Exception">Invalid image type!</exception> public void SaveArtistImage(MDBFolder folder, string name, MDBImageType imageType, MDBArtist artist, byte[] data) { var image = new MDBImage() { MusicBrainzGuid = artist.MusicBrainzArtistGuid, Type = imageType, }; if (!image.IsArtistArt) { throw new Exception("Invalid image type!"); } SaveImage(data, folder, name, ref image, artist); WriteArtistIni(folder, artist); }
void FanArtTV_CheckArtistImage(MDBImageType imageType, MDBArtist artist, JsonNode node) { foreach (JsonNode v in node.Values) { try { string source = v["url"].Value.ToString(); MDBFolder folder = mdb.GetArtistArtFolder(artist); string fileNameWithoutExtension = imageType.ToString() + " " + v["id"].Value.ToString(); byte[] imageData = null; //download only if not exists string fullPath = mdb.GetImageFileNameWithExtension(folder, fileNameWithoutExtension); if (fullPath != null) { //file exists MDBFile mdbFile; mdb.RegisterFile(folder, fullPath, out mdbFile); if (mdb.Images.Exist(nameof(MDBImage.FileID), mdbFile.ID)) { continue; } //register image (image dataset missing) imageData = mdb.TryLoadImageData(fullPath); } //download if (imageData == null) { this.LogInfo("Download FanArtTV Image <cyan>{0}", source); imageData = mdb.TryDownloadImageData(source); if (imageData == null) { this.LogInfo("<yellow>Invalid FanArtTV Artist image <default>for artist <red>{0}", artist.Name); continue; } } mdb.SaveArtistImage(folder, fileNameWithoutExtension, imageType, artist, imageData); } catch (Exception ex) { this.LogWarning(ex, "Error saving artist image {0}.", artist); } } }
/// <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>Gets or creates a folder dataset.</summary> /// <param name="fullPath">The full path.</param> /// <returns></returns> /// <exception cref="System.Exception">Invalid root!</exception> public MDBFolder GetOrCreateFolder(string fullPath) { MDBFolder folder; bool newFolder = false; lock (Folders) { if (!Folders.TryGetStruct(Search.FieldEquals(nameof(MDBFolder.Name), fullPath), out folder)) { folder = new MDBFolder() { Name = fullPath, }; folder.ID = Folders.Insert(folder); newFolder = true; } } if (newFolder) { this.LogInfo("New folder <green>{0}", folder); } return(folder); }
/// <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); } }