Unbewohnte
3 years ago
19 changed files with 478 additions and 303 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,49 @@
|
||||
package v1 |
||||
|
||||
import ( |
||||
"os" |
||||
"path/filepath" |
||||
"testing" |
||||
) |
||||
|
||||
var TESTv1TAG = &ID3v1Tag{ |
||||
SongName: "testsong", |
||||
Artist: "testartist", |
||||
Album: "testalbum", |
||||
Year: 727, |
||||
Comment: "testcomment", |
||||
Genre: "Blues", |
||||
HasEnhancedTag: true, |
||||
EnhancedTag: EnhancedID3v1Tag{ |
||||
Artist: "ARRRTIST", |
||||
Album: "ALLLLBUUUM", |
||||
SongName: "NAME", |
||||
}, |
||||
} |
||||
|
||||
func TestReadv1Tag(t *testing.T) { |
||||
testfile, err := os.OpenFile(filepath.Join(TESTDATAPATH, "testreadv1.mp3"), os.O_CREATE|os.O_RDONLY, os.ModePerm) |
||||
if err != nil { |
||||
t.Errorf("could not open file for testing: %s", err) |
||||
} |
||||
tag, err := Readv1Tag(testfile) |
||||
if err != nil { |
||||
t.Errorf("GetID3v1Tag failed: %s", err) |
||||
} |
||||
|
||||
if tag.version != V1_1 { |
||||
t.Errorf("GetID3v1Tag failed: expected version to be %s; got %s", V1_1, tag.version) |
||||
} |
||||
|
||||
if tag.Comment != "testcomment" { |
||||
t.Errorf("GetID3v1Tag failed: expected comment to be %s; got %s", "testcomment", tag.Comment) |
||||
} |
||||
|
||||
if tag.Genre != id3v1genres[0] { |
||||
t.Errorf("GetID3v1Tag failed: expected genre to be %s; got %s", id3v1genres[0], tag.Genre) |
||||
} |
||||
|
||||
if tag.Track != 8 { |
||||
t.Errorf("GetID3v1Tag failed: expected track number to be %d; got %d", 8, tag.Track) |
||||
} |
||||
} |
@ -1,90 +0,0 @@
|
||||
package v1 |
||||
|
||||
import ( |
||||
"os" |
||||
"path/filepath" |
||||
"testing" |
||||
) |
||||
|
||||
var TESTDATAPATH string = filepath.Join("..", "testData") |
||||
|
||||
var TESTv1TAG = &ID3v1Tag{ |
||||
SongName: "testsong", |
||||
Artist: "testartist", |
||||
Album: "testalbum", |
||||
Year: 727, |
||||
Comment: "testcomment", |
||||
Genre: "Blues", |
||||
} |
||||
|
||||
func TestReadv1Tag(t *testing.T) { |
||||
testfile, err := os.OpenFile(filepath.Join(TESTDATAPATH, "testreadv1.mp3"), os.O_CREATE|os.O_RDONLY, os.ModePerm) |
||||
if err != nil { |
||||
t.Errorf("could not open file for testing: %s", err) |
||||
} |
||||
tag, err := Readv1Tag(testfile) |
||||
if err != nil { |
||||
t.Errorf("GetID3v1Tag failed: %s", err) |
||||
} |
||||
|
||||
if tag.version != V1_1 { |
||||
t.Errorf("GetID3v1Tag failed: expected version to be %s; got %s", V1_1, tag.version) |
||||
} |
||||
|
||||
if tag.Comment != "Comment here " { |
||||
t.Errorf("GetID3v1Tag failed: expected comment to be %s; got %s", "Comment here ", tag.Comment) |
||||
} |
||||
|
||||
if tag.Genre != "Soundtrack" { |
||||
t.Errorf("GetID3v1Tag failed: expected genre to be %s; got %s", "Soundtrack", tag.Genre) |
||||
} |
||||
|
||||
if tag.Track != 8 { |
||||
t.Errorf("GetID3v1Tag failed: expected track number to be %d; got %d", 8, tag.Track) |
||||
} |
||||
} |
||||
|
||||
// func TestWritev1Tags(t *testing.T) {
|
||||
// f, err := os.OpenFile(filepath.Join(TESTDATAPATH, "testwritev1.mp3"), os.O_CREATE|os.O_RDWR, os.ModePerm)
|
||||
// if err != nil {
|
||||
// t.Errorf("%s", err)
|
||||
// }
|
||||
// defer f.Close()
|
||||
|
||||
// tag := TESTv1TAG
|
||||
|
||||
// // writing a tag
|
||||
// err = tag.write(f)
|
||||
// if err != nil {
|
||||
// t.Errorf("WriteID3v1Tag failed: %s", err)
|
||||
// }
|
||||
|
||||
// // reading a tag
|
||||
// readTag, err := Readv1Tag(f)
|
||||
// if err != nil {
|
||||
// t.Errorf("%s", err)
|
||||
// }
|
||||
|
||||
// if readTag.Album != "testalbum" {
|
||||
// t.Errorf("WriteID3v1Tag failed: expected %s; got %s", "testalbum", readTag.Album)
|
||||
// }
|
||||
|
||||
// if readTag.Year != 727 {
|
||||
// t.Errorf("WriteID3v1Tag failed: expected %d; got %d", 727, readTag.Year)
|
||||
// }
|
||||
// }
|
||||
|
||||
func TestWriteID3v1ToFile(t *testing.T) { |
||||
f, err := os.OpenFile(filepath.Join(TESTDATAPATH, "testwritev1.mp3"), os.O_CREATE|os.O_RDWR, os.ModePerm) |
||||
if err != nil { |
||||
t.Errorf("%s", err) |
||||
} |
||||
|
||||
tag := TESTv1TAG |
||||
|
||||
err = tag.WriteToFile(f) |
||||
if err != nil { |
||||
t.Errorf("WriteID3v1ToFile failed: %s", err) |
||||
} |
||||
|
||||
} |
@ -0,0 +1,24 @@
|
||||
package v1 |
||||
|
||||
import ( |
||||
"os" |
||||
"path/filepath" |
||||
"testing" |
||||
) |
||||
|
||||
var TESTDATAPATH string = filepath.Join("..", "testData") |
||||
|
||||
func TestWriteID3v1ToFile(t *testing.T) { |
||||
f, err := os.OpenFile(filepath.Join(TESTDATAPATH, "testwritev1.mp3"), os.O_CREATE|os.O_RDWR, os.ModePerm) |
||||
if err != nil { |
||||
t.Errorf("%s", err) |
||||
} |
||||
|
||||
tag := TESTv1TAG |
||||
|
||||
err = tag.WriteToFile(f) |
||||
if err != nil { |
||||
t.Errorf("WriteID3v1ToFile failed: %s", err) |
||||
} |
||||
|
||||
} |
@ -1,28 +1,107 @@
|
||||
package v2 |
||||
|
||||
import ( |
||||
"fmt" |
||||
"io" |
||||
) |
||||
|
||||
// Writes ID3v2Tag to ws
|
||||
func (tag *ID3v2Tag) write(ws io.WriteSeeker) error { |
||||
_, err := ws.Seek(0, io.SeekStart) |
||||
if err != nil { |
||||
return fmt.Errorf("could not seek: %s", err) |
||||
} |
||||
// func (tag *ID3v2Tag) write(ws io.WriteSeeker) error {
|
||||
// _, err := ws.Seek(0, io.SeekStart)
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("could not seek: %s", err)
|
||||
// }
|
||||
|
||||
// write header
|
||||
ws.Write(tag.Header.toBytes()) |
||||
// // write header
|
||||
// ws.Write(tag.Header.toBytes())
|
||||
|
||||
// write frames
|
||||
for _, frame := range tag.Frames { |
||||
ws.Write(frame.toBytes(tag.Header.Version)) |
||||
} |
||||
// // write frames
|
||||
// for _, frame := range tag.Frames {
|
||||
// ws.Write(frame.toBytes(tag.Header.Version()))
|
||||
// }
|
||||
|
||||
return nil |
||||
} |
||||
// return nil
|
||||
// }
|
||||
|
||||
// // Writes ID3v2Tag to file, removing already existing tag if found
|
||||
// func (tag *ID3v2Tag) WriteToFile(f *os.File) error {
|
||||
// defer f.Close()
|
||||
|
||||
// _, err := f.Seek(0, io.SeekStart)
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("could not seek: %s", err)
|
||||
// }
|
||||
|
||||
// // check for existing tag
|
||||
// possibleHeaderID, err := util.ReadToString(f, 3)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// if possibleHeaderID != HEADERIDENTIFIER {
|
||||
// // No existing tag, just write what we have
|
||||
// // and exit
|
||||
// tag.write(f)
|
||||
|
||||
// return nil
|
||||
// }
|
||||
// // there is an existing tag, remove it
|
||||
// // and write a new one
|
||||
|
||||
// // get size of the existing tag
|
||||
// existingHeader, err := readHeader(f)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// existingHSize := existingHeader.Size()
|
||||
|
||||
// // cannot truncate just the existing tag with f.Truncate(),
|
||||
// // so we need to improvise and have a temporary copy of the mp3,
|
||||
// // wipe the original file, write our tag and place the actual
|
||||
// // music without the old tag from the temporary copy.
|
||||
|
||||
// // create a temporary file
|
||||
// temporaryDir := os.TempDir()
|
||||
// tmpF, err := os.CreateTemp(temporaryDir, fmt.Sprintf("%s_TEMP", filepath.Base(f.Name())))
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// defer tmpF.Close()
|
||||
// // remove it afterwards
|
||||
// defer os.Remove(filepath.Join(temporaryDir, tmpF.Name()))
|
||||
|
||||
// tmpFStats, err := tmpF.Stat()
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// // copy contents from the original mp3 to a temporary one
|
||||
// _, err = io.Copy(tmpF, f)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// // fully remove contents from the original file
|
||||
// err = f.Truncate(0)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// // write our tag
|
||||
// tag.write(f)
|
||||
|
||||
// // read all contents of the temporary file, except the existing tag
|
||||
// tmpF.Seek(int64(existingHSize), io.SeekStart)
|
||||
|
||||
// musicDataSize := uint64(tmpFStats.Size() - int64(existingHSize))
|
||||
|
||||
// musicData, err := util.Read(tmpF, musicDataSize)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// // and write them into the original file, which
|
||||
// // contains only the new tag
|
||||
// _, err = f.Write(musicData)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// return nil
|
||||
// }
|
||||
|
Loading…
Reference in new issue