diff --git a/Source/ID3TagEditor.swift b/Source/ID3TagEditor.swift index cb15f3b2..12c7b7b7 100644 --- a/Source/ID3TagEditor.swift +++ b/Source/ID3TagEditor.swift @@ -12,6 +12,7 @@ import Foundation */ public class ID3TagEditor { private let id3TagParser: ID3TagParser + private let id3TagCreator: ID3TagCreator private let mp3FileReader: Mp3FileReader private let mp3FileWriter: Mp3FileWriter private let mp3WithID3TagBuilder: Mp3WithID3TagBuilder @@ -21,9 +22,10 @@ public class ID3TagEditor { */ public init() { self.id3TagParser = ID3TagParserFactory.make() + self.id3TagCreator = ID3TagCreatorFactory.make() self.mp3FileReader = Mp3FileReaderFactory.make() self.mp3FileWriter = Mp3FileWriter() - self.mp3WithID3TagBuilder = Mp3WithID3TagBuilder(id3TagCreator: ID3TagCreatorFactory.make(), + self.mp3WithID3TagBuilder = Mp3WithID3TagBuilder(id3TagCreator: id3TagCreator, id3TagConfiguration: ID3TagConfiguration()) } @@ -68,8 +70,8 @@ public class ID3TagEditor { ID3 tag). */ public func write(tag: ID3Tag, to path: String, andSaveTo newPath: String? = nil) throws { - let newId3TagData = try mp3WithID3TagBuilder.build(mp3: Data(), newId3Tag: tag, currentId3Tag: nil) let currentId3Tag = try read(from: path) + let newId3TagData = try id3TagCreator.create(id3Tag: tag) try mp3FileWriter.write(newId3TagData: newId3TagData, currentId3Tag: currentId3Tag, fromPath: path, toPath: newPath ?? path) } diff --git a/Source/Mp3/Mp3FileReader.swift b/Source/Mp3/Mp3FileReader.swift index 6c376f0b..ef8cc159 100644 --- a/Source/Mp3/Mp3FileReader.swift +++ b/Source/Mp3/Mp3FileReader.swift @@ -57,6 +57,7 @@ class Mp3FileReader { } inputStream.open() + defer { inputStream.close() } let headerSize = id3TagConfiguration.headerSize() let header = try read(bytesCount: headerSize, fromStream: inputStream) diff --git a/Tests/Mp3/Mp3FileWriterTest.swift b/Tests/Mp3/Mp3FileWriterTest.swift new file mode 100644 index 00000000..eb2a9775 --- /dev/null +++ b/Tests/Mp3/Mp3FileWriterTest.swift @@ -0,0 +1,37 @@ +// +// Mp3FileWriterTest.swift +// ID3TagEditor +// +// Created by Fabian Zwick on 18.10.24. +// + +import Foundation +import Testing + +@testable import ID3TagEditor + +struct Mp3FileWriterTest { + @Test func testWritingWithCurrentTag() throws { + let newTag = ID32v3TagBuilder() + .title(frame: .init(content: "Test Title")) + .album(frame: .init(content: "Test Album")) + .build() + + let id3TagCreator = ID3TagCreatorFactory.make() + let newId3TagData = try id3TagCreator.create(id3Tag: newTag) + + let fromPath = PathLoader().pathFor(name: "example", fileType: "mp3") + let currentId3Tag = try ID3TagEditor().read(from: fromPath) + + let pathMp3Generated = NSHomeDirectory() + "/testWritingWithCurrentTagAndEqualToPath.mp3" + #expect(throws: Never.self) { try Mp3FileWriter().write(newId3TagData: newId3TagData, currentId3Tag: currentId3Tag, fromPath: fromPath, toPath: pathMp3Generated) } + + let readTag = try ID3TagEditor().read(from: pathMp3Generated) + #expect((readTag?.frames[.title] as? ID3FrameWithStringContent)?.content == "Test Title") + #expect((readTag?.frames[.album] as? ID3FrameWithStringContent)?.content == "Test Album") + } + + static let allTests = [ + ("testWritingWithCurrentTag", testWritingWithCurrentTag) + ] +} diff --git a/Tests/Parse/ID3TagSizeParserTest.swift b/Tests/Parse/ID3TagSizeParserTest.swift index b19d0b40..6c9a038f 100644 --- a/Tests/Parse/ID3TagSizeParserTest.swift +++ b/Tests/Parse/ID3TagSizeParserTest.swift @@ -21,8 +21,14 @@ struct ID3TagSizeParserTest { #expect(ID3TagSizeParser().parse(data: mp3V23) == 245864) } + @Test func parseTagSizeToBeModified() { + let mp3 = NSData(contentsOfFile: PathLoader().pathFor(name: "example-to-be-modified", fileType: "mp3"))! + #expect(ID3TagSizeParser().parse(data: mp3) < mp3.count) + } + static let allTests = [ ("parseTagSizeV2", parseTagSizeV2), - ("parseFrameContentSizeV3", parseTagSizeV3) + ("parseFrameContentSizeV3", parseTagSizeV3), + ("parseTagSizeToBeModified", parseTagSizeToBeModified) ] }