From 4207d8f1aa1bf8480c4b18d51f874b722f30655a Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Tue, 6 Jul 2021 21:29:42 +0300 Subject: [PATCH] =?UTF-8?q?=E2=AC=A5=20Function=20names=20renaming=20?= =?UTF-8?q?=E2=AC=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++-- id3ed.go | 8 +------- v1/id3v10_test.go | 30 +++++++++++++++--------------- v1/id3v11_test.go | 26 +++++++++++++------------- v1/v11read.go | 12 ++++++------ v1/{v11tags.go => v11tag.go} | 4 ++-- v1/v11write.go | 34 +++++++++++++++++----------------- v1/v1read.go | 4 ++-- v1/{v1tags.go => v1tag.go} | 4 ++-- v1/v1write.go | 34 +++++++++++++++++----------------- v2/{v2tags.go => v2tag.go} | 0 11 files changed, 78 insertions(+), 83 deletions(-) rename v1/{v11tags.go => v11tag.go} (68%) rename v1/{v1tags.go => v1tag.go} (72%) rename v2/{v2tags.go => v2tag.go} (100%) diff --git a/README.md b/README.md index 739df0d..ef5d4b0 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Right now it`s capable of reading and writing ID3v1 and ID3v1.1 tags. -ID3v2.x support is still in making. +ID3v2.x support is still in making, but it can read header and v.3~v.4 frames --- @@ -19,8 +19,9 @@ To download package source: ``` go get github.com/Unbewohnte/id3ed/... ``` +will be deprecated in 1.17 as far as I know -To compile package: +To donwload and compile package: ``` go install github.com/Unbewohnte/id3ed/... ``` diff --git a/id3ed.go b/id3ed.go index 3e167f2..eb2e325 100644 --- a/id3ed.go +++ b/id3ed.go @@ -1,17 +1,11 @@ package id3ed // type File struct { -// Filename string // ContainsV1 bool // ContainsV2 bool -// } - -// type Metadata interface { -// Write() error +// ? // } // func Open(path string) (*File, error) { // return &File{}, nil // } - -// func (f *File) GetMetadata() (*Metadata, error) diff --git a/v1/id3v10_test.go b/v1/id3v10_test.go index ac91d45..fe40494 100644 --- a/v1/id3v10_test.go +++ b/v1/id3v10_test.go @@ -8,7 +8,7 @@ import ( var TESTDATAPATH string = filepath.Join("..", "testData") -var TESTv1TAGS = &ID3v1Tags{ +var TESTv1TAG = &ID3v1Tag{ SongName: "testsong", Artist: "testartist", Album: "testalbum", @@ -22,13 +22,13 @@ func TestGetv1Tags(t *testing.T) { if err != nil { t.Errorf("could not open file for testing: %s", err) } - tags, err := Getv1Tags(testfile) + tag, err := Getv1Tag(testfile) if err != nil { - t.Errorf("GetID3v1Tags failed: %s", err) + t.Errorf("GetID3v1Tag failed: %s", err) } - if tags.Comment != "Comment here " { - t.Errorf("GetID3v1Tags failed: expected %s; got %s", "Comment here ", tags.Comment) + if tag.Comment != "Comment here " { + t.Errorf("GetID3v1Tag failed: expected %s; got %s", "Comment here ", tag.Comment) } } @@ -39,26 +39,26 @@ func TestWritev1Tags(t *testing.T) { } defer f.Close() - tags := TESTv1TAGS + tag := TESTv1TAG - // writing tags - err = tags.Write(f) + // writing a tag + err = tag.Write(f) if err != nil { - t.Errorf("WriteID3v1Tags failed: %s", err) + t.Errorf("WriteID3v1Tag failed: %s", err) } - // reading tags - readTags, err := Getv1Tags(f) + // reading a tag + readTags, err := Getv1Tag(f) if err != nil { t.Errorf("%s", err) } if readTags.Album != "testalbum" { - t.Errorf("WriteID3v1Tags failed: expected %s; got %s", "testalbum", readTags.Album) + t.Errorf("WriteID3v1Tag failed: expected %s; got %s", "testalbum", readTags.Album) } if readTags.Year != 727 { - t.Errorf("WriteID3v1Tags failed: expected %d; got %d", 727, readTags.Year) + t.Errorf("WriteID3v1Tag failed: expected %d; got %d", 727, readTags.Year) } } @@ -68,9 +68,9 @@ func TestWriteID3v1ToFile(t *testing.T) { t.Errorf("%s", err) } - tags := TESTv1TAGS + tag := TESTv1TAG - err = tags.WriteToFile(f) + err = tag.WriteToFile(f) if err != nil { t.Errorf("WriteID3v1ToFile failed: %s", err) } diff --git a/v1/id3v11_test.go b/v1/id3v11_test.go index 57b6543..0ba6637 100644 --- a/v1/id3v11_test.go +++ b/v1/id3v11_test.go @@ -7,7 +7,7 @@ import ( "testing" ) -var TESTV11TAGS = &ID3v11Tags{ +var TESTV11TAG = &ID3v11Tag{ SongName: "testsong", Artist: "testartist", Album: "testalbum", @@ -22,18 +22,18 @@ func TestGetID3v11Tags(t *testing.T) { if err != nil { t.Errorf("could not open file for testing: %s", err) } - mp3tags, err := Getv11Tags(testfile) + mp3tags, err := Getv11Tag(testfile) if err != nil { - t.Errorf("GetID3v11Tags failed: %s", err) + t.Errorf("GetID3v11Tag failed: %s", err) } if mp3tags.Artist != "Artist" { fmt.Printf("%v", mp3tags.Artist) - t.Errorf("GetID3v11Tags failed: expected artist %s; got %s", "Artist", mp3tags.Artist) + t.Errorf("GetID3v11Tag failed: expected artist %s; got %s", "Artist", mp3tags.Artist) } if mp3tags.Track != 8 { - t.Errorf("GetID3v11Tags failed: expected track %d; got %d", 8, mp3tags.Track) + t.Errorf("GetID3v11Tag failed: expected track %d; got %d", 8, mp3tags.Track) } } @@ -45,28 +45,28 @@ func TestWriteID3v11Tags(t *testing.T) { } defer f.Close() - tags := TESTV11TAGS + tag := TESTV11TAG - err = tags.Write(f) + err = tag.Write(f) if err != nil { t.Errorf("WriteID3v1Tags failed: %s", err) } - readTags, err := Getv11Tags(f) + readTags, err := Getv11Tag(f) if err != nil { t.Errorf("%s", err) } if readTags.Album != "testalbum" { - t.Errorf("WriteID3v11Tags failed: expected album %s; got %s", "testalbum", readTags.Album) + t.Errorf("WriteID3v11Tag failed: expected album %s; got %s", "testalbum", readTags.Album) } if readTags.Year != 727 { - t.Errorf("WriteID3v11Tags failed: expected year %d; got %d", 727, readTags.Year) + t.Errorf("WriteID3v11Tag failed: expected year %d; got %d", 727, readTags.Year) } if readTags.Track != 10 { - t.Errorf("WriteID3v11Tags failed: expected track %d; got %d", 10, readTags.Track) + t.Errorf("WriteID3v11Tag failed: expected track %d; got %d", 10, readTags.Track) } } @@ -76,9 +76,9 @@ func TestWriteID3v11ToFile(t *testing.T) { t.Errorf("%s", err) } - tags := TESTV11TAGS + tag := TESTV11TAG - err = tags.WriteToFile(f) + err = tag.WriteToFile(f) if err != nil { t.Errorf("WriteID3v1ToFile failed: %s", err) } diff --git a/v1/v11read.go b/v1/v11read.go index a8cbfc9..d60d9a6 100644 --- a/v1/v11read.go +++ b/v1/v11read.go @@ -10,21 +10,21 @@ import ( ) // Retrieves ID3v1.1 field values of provided io.ReadSeeker -func Getv11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) { +func Getv11Tag(rs io.ReadSeeker) (*ID3v11Tag, error) { // set reader to the last 128 bytes _, err := rs.Seek(-int64(ID3v1SIZE), io.SeekEnd) if err != nil { return nil, fmt.Errorf("could not seek: %s", err) } - tag, err := util.Read(rs, 3) + identifier, err := util.Read(rs, 3) if err != nil { return nil, err } - if !bytes.Equal(tag, []byte(ID3v1IDENTIFIER)) { - // no TAG, given file does not use ID3v1 - return nil, fmt.Errorf("does not use ID3v1: expected %s; got %s", ID3v1IDENTIFIER, tag) + if !bytes.Equal(identifier, []byte(ID3v1IDENTIFIER)) { + // no identifier, given file does not use ID3v1 + return nil, fmt.Errorf("does not use ID3v1: expected %s; got %s", ID3v1IDENTIFIER, identifier) } songname, err := util.ReadToString(rs, 30) @@ -85,7 +85,7 @@ func Getv11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) { genre = "" } - return &ID3v11Tags{ + return &ID3v11Tag{ SongName: songname, Artist: artist, Album: album, diff --git a/v1/v11tags.go b/v1/v11tag.go similarity index 68% rename from v1/v11tags.go rename to v1/v11tag.go index ada42df..7b97135 100644 --- a/v1/v11tags.go +++ b/v1/v11tag.go @@ -1,6 +1,6 @@ package v1 -type ID3v11Tags struct { +type ID3v11Tag struct { SongName string Artist string Album string @@ -10,6 +10,6 @@ type ID3v11Tags struct { Genre string } -func (tags *ID3v11Tags) Version() int { +func (*ID3v11Tag) Version() int { return 11 } diff --git a/v1/v11write.go b/v1/v11write.go index 74626ed..b5719c6 100644 --- a/v1/v11write.go +++ b/v1/v11write.go @@ -10,10 +10,10 @@ import ( "github.com/Unbewohnte/id3ed/util" ) -// Writes given ID3v1.1 tags to dst +// Writes given ID3v1.1 tag to dst // NOTE: will not remove already existing ID3v1.1 tag if it`s present, // use ⁕WriteToFile⁕ method if you`re working with REAL mp3 files !!! -func (tags *ID3v11Tags) Write(dst io.WriteSeeker) error { +func (tag *ID3v11Tag) Write(dst io.WriteSeeker) error { dst.Seek(0, io.SeekEnd) // TAG @@ -23,31 +23,31 @@ func (tags *ID3v11Tags) Write(dst io.WriteSeeker) error { } // Song name - err = util.WriteToExtent(dst, []byte(tags.SongName), 30) + err = util.WriteToExtent(dst, []byte(tag.SongName), 30) if err != nil { return fmt.Errorf("could not write to dst: %s", err) } // Artist - err = util.WriteToExtent(dst, []byte(tags.Artist), 30) + err = util.WriteToExtent(dst, []byte(tag.Artist), 30) if err != nil { return fmt.Errorf("could not write to dst: %s", err) } // Album - err = util.WriteToExtent(dst, []byte(tags.Album), 30) + err = util.WriteToExtent(dst, []byte(tag.Album), 30) if err != nil { return fmt.Errorf("could not write to dst: %s", err) } // Year - err = util.WriteToExtent(dst, []byte(fmt.Sprint(tags.Year)), 4) + err = util.WriteToExtent(dst, []byte(fmt.Sprint(tag.Year)), 4) if err != nil { return fmt.Errorf("could not write to dst: %s", err) } // Comment - err = util.WriteToExtent(dst, []byte(tags.Comment), 28) + err = util.WriteToExtent(dst, []byte(tag.Comment), 28) if err != nil { return fmt.Errorf("could not write to dst: %s", err) } @@ -59,14 +59,14 @@ func (tags *ID3v11Tags) Write(dst io.WriteSeeker) error { // Track trackBytes := make([]byte, 1) - binary.PutVarint(trackBytes, int64(tags.Track)) + binary.PutVarint(trackBytes, int64(tag.Track)) _, err = dst.Write(trackBytes) if err != nil { return fmt.Errorf("could not write to dst: %s", err) } //Genre - genreCode := util.GetKey(id3v1genres, tags.Genre) + genreCode := util.GetKey(id3v1genres, tag.Genre) if genreCode == -1 { // if no genre found - encode genre code as 255 genreCode = ID3v1INVALIDGENRE @@ -82,8 +82,8 @@ func (tags *ID3v11Tags) Write(dst io.WriteSeeker) error { return nil } -// Checks for existing ID3v1.1 tag in file, if present - removes it and replaces with provided tags -func (tags *ID3v11Tags) WriteToFile(f *os.File) error { +// Checks for existing ID3v1.1 tag in file, if present - removes it and replaces with provided tag +func (tag *ID3v11Tag) WriteToFile(f *os.File) error { defer f.Close() // check for existing ID3v1.1 tag @@ -92,16 +92,16 @@ func (tags *ID3v11Tags) WriteToFile(f *os.File) error { return fmt.Errorf("could not seek: %s", err) } - tag, err := util.Read(f, 3) + identifier, err := util.Read(f, 3) if err != nil { // return err return err } - if !bytes.Equal(tag, []byte(ID3v1IDENTIFIER)) { - // no existing tag, just write given tags - err = tags.Write(f) + if !bytes.Equal(identifier, []byte(ID3v1IDENTIFIER)) { + // no existing identifier, just write given tag + err = tag.Write(f) if err != nil { return fmt.Errorf("could not write to writer: %s", err) } @@ -119,8 +119,8 @@ func (tags *ID3v11Tags) WriteToFile(f *os.File) error { return fmt.Errorf("could not truncate file %s", err) } - // writing new tags - err = tags.Write(f) + // writing new tag + err = tag.Write(f) if err != nil { return fmt.Errorf("could not write to writer: %s", err) } diff --git a/v1/v1read.go b/v1/v1read.go index 9aafcf2..638ae25 100644 --- a/v1/v1read.go +++ b/v1/v1read.go @@ -10,7 +10,7 @@ import ( ) // Retrieves ID3v1 field values of provided io.ReadSeeker (usually a file) -func Getv1Tags(rs io.ReadSeeker) (*ID3v1Tags, error) { +func Getv1Tag(rs io.ReadSeeker) (*ID3v1Tag, error) { // set reader to the last 128 bytes _, err := rs.Seek(-int64(ID3v1SIZE), io.SeekEnd) if err != nil { @@ -69,7 +69,7 @@ func Getv1Tags(rs io.ReadSeeker) (*ID3v1Tags, error) { genre = "" } - return &ID3v1Tags{ + return &ID3v1Tag{ SongName: songname, Artist: artist, Album: album, diff --git a/v1/v1tags.go b/v1/v1tag.go similarity index 72% rename from v1/v1tags.go rename to v1/v1tag.go index 76c6c6a..311eed0 100644 --- a/v1/v1tags.go +++ b/v1/v1tag.go @@ -2,7 +2,7 @@ package v1 // https://id3.org/ID3v1 - documentation -type ID3v1Tags struct { +type ID3v1Tag struct { SongName string Artist string Album string @@ -11,6 +11,6 @@ type ID3v1Tags struct { Genre string } -func (tags *ID3v1Tags) Version() int { +func (*ID3v1Tag) Version() int { return 10 } diff --git a/v1/v1write.go b/v1/v1write.go index e2146ef..b9e7c9e 100644 --- a/v1/v1write.go +++ b/v1/v1write.go @@ -10,50 +10,50 @@ import ( "github.com/Unbewohnte/id3ed/util" ) -// Writes given ID3v1.0 tags to given io.WriteSeeker. +// Writes given ID3v1.0 tag to given io.WriteSeeker. // NOTE: will not remove already existing ID3v1 tag if it`s present, // use ⁕WriteToFile⁕ method if you`re working with REAL mp3 files !!! -func (tags *ID3v1Tags) Write(dst io.WriteSeeker) error { +func (tag *ID3v1Tag) Write(dst io.WriteSeeker) error { dst.Seek(0, io.SeekEnd) - // TAG + // ID _, err := dst.Write([]byte(ID3v1IDENTIFIER)) if err != nil { return fmt.Errorf("could not write to writer: %s", err) } // Song name - err = util.WriteToExtent(dst, []byte(tags.SongName), 30) + err = util.WriteToExtent(dst, []byte(tag.SongName), 30) if err != nil { return fmt.Errorf("could not write to writer: %s", err) } // Artist - err = util.WriteToExtent(dst, []byte(tags.Artist), 30) + err = util.WriteToExtent(dst, []byte(tag.Artist), 30) if err != nil { return fmt.Errorf("could not write to writer: %s", err) } // Album - err = util.WriteToExtent(dst, []byte(tags.Album), 30) + err = util.WriteToExtent(dst, []byte(tag.Album), 30) if err != nil { return fmt.Errorf("could not write to writer: %s", err) } // Year - err = util.WriteToExtent(dst, []byte(fmt.Sprint(tags.Year)), 4) + err = util.WriteToExtent(dst, []byte(fmt.Sprint(tag.Year)), 4) if err != nil { return fmt.Errorf("could not write to writer: %s", err) } // Comment - err = util.WriteToExtent(dst, []byte(tags.Comment), 30) + err = util.WriteToExtent(dst, []byte(tag.Comment), 30) if err != nil { return fmt.Errorf("could not write to writer: %s", err) } // Genre - genreCode := util.GetKey(id3v1genres, tags.Genre) + genreCode := util.GetKey(id3v1genres, tag.Genre) if genreCode == -1 { // if no genre found - encode genre code as 255 genreCode = ID3v1INVALIDGENRE @@ -69,21 +69,21 @@ func (tags *ID3v1Tags) Write(dst io.WriteSeeker) error { return nil } -// Checks for existing ID3v1 tag in file, if present - removes it and replaces with provided tags -func (tags *ID3v1Tags) WriteToFile(f *os.File) error { +// Checks for existing ID3v1 tag in file, if present - removes it and replaces with provided tag +func (tag *ID3v1Tag) WriteToFile(f *os.File) error { defer f.Close() // check for existing ID3v1 tag f.Seek(-int64(ID3v1SIZE), io.SeekEnd) - tag, err := util.Read(f, 3) + identifier, err := util.Read(f, 3) if err != nil { return err } - if !bytes.Equal(tag, []byte(ID3v1IDENTIFIER)) { - // no existing tag, just write given tags - err = tags.Write(f) + if !bytes.Equal(identifier, []byte(ID3v1IDENTIFIER)) { + // no existing identifier, just write given tag + err = tag.Write(f) if err != nil { return err } @@ -101,8 +101,8 @@ func (tags *ID3v1Tags) WriteToFile(f *os.File) error { return fmt.Errorf("could not truncate file %s", err) } - // writing new tags - err = tags.Write(f) + // writing new tag + err = tag.Write(f) if err != nil { return fmt.Errorf("could not write to writer: %s", err) } diff --git a/v2/v2tags.go b/v2/v2tag.go similarity index 100% rename from v2/v2tags.go rename to v2/v2tag.go