/// <summary>
        ///     Retrieves the specified tag data as a byte array. This method does not attempt to read the tag data, it simply
        ///     reads the header and if present the tag bytes are read directly from the stream. This means that typical exceptions
        ///     that get thrown in a tag read will not occur in this method.
        /// </summary>
        /// <param name="version">The tag version number.</param>
        /// <returns>A byte array of the tag data.</returns>
        public byte[] GetTagBytes(Id3Version version)
        {
            Id3Handler handler = ExistingHandlers.FirstOrDefault(h => h.Version == version);

            byte[] tagBytes = handler?.GetTagBytes(Stream);
            return(tagBytes);
        }
Example #2
0
        /// <summary>
        ///     Converts an ID3 tag to another version after resolving the differences between the two versions. The resultant tag
        ///     will have all the frames from the source tag, but those frames not recognized in the new version will be treated as
        ///     UnknownFrame objects.
        ///     Similarly, frames recognized in the output tag version, but not in the source version are converted accordingly.
        /// </summary>
        /// <param name="version">Version of the tag to convert to.</param>
        /// <returns>The converted tag of the specified version, or null if there were any errors.</returns>
        public Id3Tag ConvertTo(Id3Version version)
        {
            //If the requested version is the same as this version, just return the same instance.
            if (Version == version)
            {
                return(this);
            }

            //Get the ID3 tag handlers for the destination and create a empty tag
            var    destinationHandler = Id3Handler.GetHandler(version);
            Id3Tag destinationTag     = destinationHandler.CreateTag();

            foreach (Id3Frame sourceFrame in this)
            {
                if (sourceFrame is UnknownFrame unknownFrame)
                {
                    string   frameId          = unknownFrame.Id;
                    Id3Frame destinationFrame = destinationHandler.GetFrameFromFrameId(frameId);
                    destinationTag.AddUntypedFrame(destinationFrame);
                }
                else
                {
                    destinationTag.AddUntypedFrame(sourceFrame);
                }
            }

            return(destinationTag);
        }
Example #3
0
        /// <summary>
        /// Returns a collection of all ID3 tags present in the MP3 data.
        /// </summary>
        /// <returns>A collection of all ID3 tags present in the MP3 data.</returns>
        public IEnumerable <Id3Tag> GetAllTags()
        {
            var tags = new Id3Tag[ExistingHandlers.Count];

            for (int i = 0; i < tags.Length; i++)
            {
                Id3Handler handler = ExistingHandlers[i].Handler;
                tags[i] = handler.ReadTag(_stream);
            }
            return(tags);
        }
        public Id3Tag GetTag(Id3Version version, out object additionalData)
        {
            Id3Handler handler = ExistingHandlers.FirstOrDefault(h => h.Version == version);

            if (handler != null)
            {
                return(handler.ReadTag(Stream, out additionalData));
            }
            additionalData = null;
            return(null);
        }
        public Id3Tag GetTag(Id3TagFamily family, out object additionalData)
        {
            Id3Handler familyHandler = ExistingHandlers.FirstOrDefault(handler => handler.Family == family);

            if (familyHandler != null)
            {
                return(familyHandler.ReadTag(Stream, out additionalData));
            }
            additionalData = null;
            return(null);
        }
        /// <summary>
        ///     Deletes the ID3 tag of the specified tag family type from the MP3 data.
        /// </summary>
        /// <param name="family">The ID3 tag family type.</param>
        public void DeleteTag(Id3TagFamily family)
        {
            EnsureWritePermissions(Mp3Messages.NoWritePermissions_CannotDeleteTag);
            Id3Handler foundHandler = ExistingHandlers.FirstOrDefault(handler => handler.Family == family);

            if (foundHandler != null)
            {
                foundHandler.DeleteTag(Stream);
                InvalidateExistingHandlers();
            }
        }
        /// <summary>
        ///     Deletes the ID3 tag of the specified version from the MP3 data.
        /// </summary>
        /// <param name="version">The tag version</param>
        public void DeleteTag(Id3Version version)
        {
            EnsureWritePermissions(Mp3Messages.NoWritePermissions_CannotDeleteTag);
            Id3Handler handler = ExistingHandlers.FirstOrDefault(h => h.Version == version);

            if (handler != null)
            {
                handler.DeleteTag(Stream);
                InvalidateExistingHandlers();
            }
        }
Example #8
0
        /// <summary>
        /// Deletes the ID3 tag of the specified tag family type from the MP3 data.
        /// </summary>
        /// <param name="family">The ID3 tag family type.</param>
        public void DeleteTag(Id3TagFamily family)
        {
            EnsureWritePermissions(Id3Messages.NoWritePermissions_CannotDeleteTag);
            IEnumerable <RegisteredId3Handler> registeredHandlers = ExistingHandlers.GetHandlers(family);
            RegisteredId3Handler registeredHandler = registeredHandlers.FirstOrDefault();

            if (registeredHandler != null)
            {
                Id3Handler handler = registeredHandler.Handler;
                handler.DeleteTag(_stream);
                InvalidateExistingHandlers();
            }
        }
Example #9
0
        /// <summary>
        /// Retrieves an ID3 tag of the specified tag family type - version 2.x or version 1.x.
        /// </summary>
        /// <param name="family">The ID3 tag family type required.</param>
        /// <returns>The ID3 tag of the specified tag family type, or null if it doesn't exist.</returns>
        public Id3Tag GetTag(Id3TagFamily family)
        {
            IEnumerable <RegisteredId3Handler> familyHandlers = ExistingHandlers.GetHandlers(family);

            RegisteredId3Handler familyHandler = familyHandlers.FirstOrDefault();

            if (familyHandler == null)
            {
                return(null);
            }
            Id3Handler handler = familyHandler.Handler;
            Id3Tag     tag     = handler.ReadTag(_stream);

            return(tag);
        }
Example #10
0
        public bool WriteTag(Id3Tag tag, WriteConflictAction conflictAction = WriteConflictAction.NoAction)
        {
            EnsureWritePermissions(Id3Messages.NoWritePermissions_CannotWriteTag);
            if (tag == null)
            {
                throw new ArgumentNullException("tag");
            }

            //The tag should specify major version number
            if (tag.MajorVersion == 0)
            {
                throw new ArgumentException(Id3Messages.MajorTagVersionMissing, "tag");
            }

            //Get any existing handlers from the same family as the tag
            IEnumerable <RegisteredId3Handler> familyHandlers = ExistingHandlers.GetHandlers(tag.Family);

            //If a tag already exists from the same family, but is a different version than the passed tag,
            //delete it if conflictAction is Replace.
            RegisteredId3Handler familyHandler = familyHandlers.FirstOrDefault();

            if (familyHandler != null)
            {
                Id3Handler handler = familyHandler.Handler;
                if (handler.MajorVersion != tag.MajorVersion || handler.MinorVersion != tag.MinorVersion)
                {
                    if (conflictAction == WriteConflictAction.NoAction)
                    {
                        return(false);
                    }
                    if (conflictAction == WriteConflictAction.Replace)
                    {
                        Id3Handler handlerCopy = handler;
                        handlerCopy.DeleteTag(_stream);
                    }
                }
            }

            //Write the tag to the file. The handler will know how to overwrite itself.
            RegisteredId3Handler registeredHandler = RegisteredHandlers.GetHandler(tag.MajorVersion, tag.MinorVersion);
            bool writeSuccessful = registeredHandler.Handler.WriteTag(_stream, tag);

            if (writeSuccessful)
            {
                InvalidateExistingHandlers();
            }
            return(writeSuccessful);
        }
Example #11
0
        public bool WriteTag(Id3Tag tag, WriteConflictAction conflictAction = WriteConflictAction.NoAction)
        {
            if (tag == null)
            {
                throw new ArgumentNullException(nameof(tag));
            }

            EnsureWritePermissions(Mp3Messages.NoWritePermissions_CannotWriteTag);

            //If a tag already exists from the same family, but is a different version than the passed tag,
            //delete it if conflictAction is Replace.
            Id3Handler familyHandler = ExistingHandlers.FirstOrDefault(handler => handler.Family == tag.Family);

            if (familyHandler != null)
            {
                Id3Handler handler = familyHandler;
                if (handler.Version != tag.Version)
                {
                    if (conflictAction == WriteConflictAction.NoAction)
                    {
                        return(false);
                    }
                    if (conflictAction == WriteConflictAction.Replace)
                    {
                        Id3Handler handlerCopy = handler; //TODO: Why did we need a copy of the handler?
                        handlerCopy.DeleteTag(Stream);
                    }
                }
            }

            //Write the tag to the file. The handler will know how to overwrite itself.
            Id3Handler writeHandler    = Id3Handler.GetHandler(tag.Version);
            bool       writeSuccessful = writeHandler.WriteTag(Stream, tag);

            if (writeSuccessful)
            {
                InvalidateExistingHandlers();
            }
            return(writeSuccessful);
        }
Example #12
0
        /// <summary>
        ///     Retrieves an ID3 tag of the specified version number.
        /// </summary>
        /// <param name="version">The tag version number.</param>
        /// <returns>The ID3 tag of the specified version number, or null if it doesn't exist.</returns>
        public Id3Tag GetTag(Id3Version version)
        {
            Id3Handler handler = ExistingHandlers.FirstOrDefault(h => h.Version == version);

            return(handler?.ReadTag(Stream, out _));
        }
Example #13
0
        /// <summary>
        ///     Retrieves an ID3 tag of the specified tag family type - version 2.x or version 1.x.
        /// </summary>
        /// <param name="family">The ID3 tag family type required.</param>
        /// <returns>The ID3 tag of the specified tag family type, or null if it doesn't exist.</returns>
        public Id3Tag GetTag(Id3TagFamily family)
        {
            Id3Handler familyHandler = ExistingHandlers.FirstOrDefault(handler => handler.Family == family);

            return(familyHandler?.ReadTag(Stream, out _));
        }