using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using TagLib; using File = System.IO.File; namespace GarrysMod.AddonCreator.Addon { /// /// Represents a media addon file with the possibility of stripping tags and checking for corruptions. /// public class MinifiedMediaAddonFileInfo : AddonFileInfo { private readonly string _tempFile; private bool _processed; ~MinifiedMediaAddonFileInfo() { File.Delete(_tempFile); } /// /// Creates a new instance using the given addon file. /// /// The addon file, supposedly a media file public MinifiedMediaAddonFileInfo(AddonFileInfo file, string extension) { _tempFile = Path.GetTempFileName(); var dirName = Path.GetDirectoryName(_tempFile); if (dirName == null) throw new InvalidOperationException("Temporary directory is NULL"); // Fix extension, needed for TagLib to detect file format properly var newTempFile = Path.Combine( dirName, Path.GetFileNameWithoutExtension(_tempFile) + "." + extension); File.Move(_tempFile, newTempFile); _tempFile = newTempFile; using (var s = new FileStream(_tempFile, FileMode.OpenOrCreate, FileAccess.Write)) { var buffer = file.GetContents(); s.Write(buffer, 0, buffer.Length); } } /// /// Indicates whether tags should be stripped or not. /// public bool StripTags { get; set; } /// /// Indicates whether to ignore possible file corruption. /// public bool IgnoreCorrupted { get; set; } /// /// Processes the media file, applies any wanted stripping and returns the new file contents. /// /// The contents of the media file with optional applied stripping /// Will be thrown when TagLib detects possible media file corruptions public override byte[] GetContents() { if (_processed) { using (var s = new FileStream(_tempFile, FileMode.Open, FileAccess.Read)) { var buffer = new byte[s.Length]; s.Read(buffer, 0, buffer.Length); return buffer; } } using (var tags = TagLib.File.Create(_tempFile)) { if (tags.PossiblyCorrupt && !IgnoreCorrupted) { throw new CorruptFileException(string.Join("; ", tags.CorruptionReasons)); } if (StripTags) { tags.RemoveTags(TagTypes.AllTags); } tags.Save(); } _processed = true; // This will now return the file contents instead return GetContents(); } } }