public static ArtworkData GetArtwork(string track) { ArtworkData result = null; try { result = GetArtworkForTrack(track); } catch { } return(result); }
internal void WriteResponseArtwork(Socket client, string trackFile) { using (ArtworkData data = Artwork.GetArtwork(trackFile)) { if (data == null) { WriteResponse(client, HttpStatusCode.InternalServerError, "no file"); } else { string contentType = "image/" + data.type; WriteResponseStream(client, data.data, data.data.Length, 0, contentType); } } }
private static ArtworkData OpenArtworkFile(string file) { ArtworkData result = null; FileStream data = null; try { data = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Write); byte[] bytes = new byte[MAX_MAGIC_LENGTH]; data.Read(bytes, 0, MAX_MAGIC_LENGTH); string type = GetImageTypeFromBuffer(bytes); data.Seek(0, SeekOrigin.Begin); if (type != null) { result = new ArtworkData { type = type, data = data }; } else { ReportFailure(file, bytes); } } catch { if (data != null) { data.Dispose(); } result = null; } return(result); }
private static ArtworkData GetArtworkFromID3(string filename) { ArtworkData result = null; using (FileStream data = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) using (BinaryReader reader = new BinaryReader(data)) { byte[] buffer = null; const int EXTENDED_HEADER = (1 << 6); const int APIC_FRAME_ID = 0x43495041; const int CLEARED_BITS_MASK = 0xE0FF; const int ANSI = 0; if (reader.ReadChar() != 'I') { return(null); } if (reader.ReadChar() != 'D') { return(null); } if (reader.ReadChar() != '3') { return(null); } // skip version reader.BaseStream.Seek(2, SeekOrigin.Current); byte flags = reader.ReadByte(); int size = swapEndianness(reader.ReadInt32()); if ((flags & EXTENDED_HEADER) != 0) { int extendedHeaderSize = swapEndianness(reader.ReadInt32()); reader.BaseStream.Seek(extendedHeaderSize, SeekOrigin.Current); } uint frameId = reader.ReadUInt32(); while (frameId != APIC_FRAME_ID && reader.BaseStream.Position < reader.BaseStream.Length) { int chunkSize = swapEndianness(reader.ReadInt32()); if (chunkSize < 0) { return(null); } ushort frameFlags = reader.ReadUInt16(); if ((frameFlags & CLEARED_BITS_MASK) != 0) { return(null); } if (reader.BaseStream.Position + chunkSize > reader.BaseStream.Length) { return(null); } reader.BaseStream.Seek(chunkSize, SeekOrigin.Current); frameId = reader.ReadUInt32(); } int imageSize = swapEndianness(reader.ReadInt32()); //skip flags reader.BaseStream.Seek(2, SeekOrigin.Current); byte encoding = reader.ReadByte(); for (byte limit = 0, terminator = 0xFF; limit < 64 && terminator != 0; limit++) { terminator = reader.ReadByte(); } // skip picture type reader.BaseStream.Seek(1, SeekOrigin.Current); if (encoding == ANSI) { for (byte limit = 0, terminator = 0xFF; limit < 64 && terminator != 0; limit++) { terminator = reader.ReadByte(); } } else { for (short limit = 0, terminator = -1; limit < 64 && terminator != 0; limit++) { terminator = reader.ReadInt16(); } } buffer = reader.ReadBytes(imageSize); string type = GetImageTypeFromBuffer(buffer); if (type != null) { result = new ArtworkData { type = type, data = new MemoryStream(buffer) }; } else { ReportFailure(filename, buffer); } } return(result); }
private static ArtworkData GetArtworkForTrack(string file) { ArtworkData result = null; string artworkFile = String.Empty; byte[] artworkData = { }; if (Plugin.mbApi.Library_GetArtworkEx(file, 0, true, ref GET_ARTWORK_FLAGS, ref artworkFile, ref artworkData) == true && artworkData != null) { string type = GetImageTypeFromBuffer(artworkData); if (type != null) { result = new ArtworkData { type = type, data = new MemoryStream(artworkData) }; } else { ReportFailure(file, artworkData); } } else { // Sometimes GetArtworkEx fails... string directory = Path.GetDirectoryName(file); foreach (var rawPattern in Plugin.artworkPatterns) { string pattern = rawPattern.Replace("<Filename>", Path.GetFileNameWithoutExtension(file)); // ...if there is embedded artwork, GetArtworkEx does not fail #if false if (pattern == "Embedded") { result = GetArtworkFromID3(file); if (result != null) { break; } } else #endif { var matches = Directory.EnumerateFiles(directory, pattern, SearchOption.AllDirectories); if (matches.Count() > 10) { // We've hit some parent directory continue; } foreach (var artwork in matches) { if (AudioStream.IsAudioFile(artwork) == false) { result = OpenArtworkFile(artwork); if (result != null) { goto done; } } } } } done :; } return(result); }