From 135aad7d3430b674b99ee7dc62f7069bb293b062 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Mon, 12 Jul 2021 14:44:32 +0300 Subject: [PATCH] =?UTF-8?q?=E2=96=A9=20Now=20easier=20to=20use=20!=20?= =?UTF-8?q?=E2=96=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- id3ed.go | 79 +++++++++++++++++++++++++++++++++++---- id3ed_test.go | 45 ++++++++++++++++++++++ testData/testwritev1.mp3 | Bin 2849 -> 167 bytes v1/read.go | 8 ++-- v1/tag.go | 2 +- v1/v1_test.go | 62 +++++++++++++++--------------- v1/write.go | 7 +++- v2/frame.go | 26 ++++++++++++- v2/frame_test.go | 27 ++----------- v2/header.go | 6 ++- v2/header_test.go | 4 +- v2/read.go | 13 ++++--- 12 files changed, 200 insertions(+), 79 deletions(-) create mode 100644 id3ed_test.go diff --git a/id3ed.go b/id3ed.go index eb2e325..738317b 100644 --- a/id3ed.go +++ b/id3ed.go @@ -1,11 +1,76 @@ package id3ed -// type File struct { -// ContainsV1 bool -// ContainsV2 bool -// ? -// } +import ( + "fmt" + "os" + + v1 "github.com/Unbewohnte/id3ed/v1" + v2 "github.com/Unbewohnte/id3ed/v2" +) + +type File struct { + path string + ContainsID3v1 bool + ContainsID3v2 bool + ID3v1Tag *v1.ID3v1Tag + ID3v2Tag *v2.ID3v2Tag +} + +// Opens file specified by `path`, reads all ID3 tags that +// it can find and returns a *File +func Open(path string) (*File, error) { + fhandler, err := os.Open(path) + if err != nil { + return nil, fmt.Errorf("could not open: %s", err) + } + defer fhandler.Close() + + var file File + + file.path = path // saving path to the file for future writing + + v1tag, err := v1.Readv1Tag(fhandler) + switch err { + case nil: + file.ContainsID3v1 = true + case v1.ErrDoesNotUseID3v1: + file.ContainsID3v1 = false + default: + return nil, fmt.Errorf("could not read ID3v1 tag from file: %s", err) + } + file.ID3v1Tag = v1tag + + v2tag, err := v2.ReadV2Tag(fhandler) + switch err { + case nil: + file.ContainsID3v2 = true + case v2.ErrDoesNotUseID3v2: + file.ContainsID3v2 = false + default: + return nil, fmt.Errorf("could not read ID3v2 tag from file: %s", err) + } + file.ID3v2Tag = v2tag + + return &file, nil +} + +// Writes given ID3v1 tag to file +func (f *File) WriteID3v1(tag *v1.ID3v1Tag) error { + fhandler, err := os.OpenFile(f.path, os.O_RDWR, os.ModePerm) + if err != nil { + return fmt.Errorf("could not read a file: %s", err) + } + defer fhandler.Close() + + err = tag.WriteToFile(fhandler) + if err != nil { + return fmt.Errorf("could not write ID3v1 to file: %s", err) + } + + return nil +} -// func Open(path string) (*File, error) { -// return &File{}, nil +// still not implemented +// func (f *File) WriteID3v2(tag *v2.ID3v2Tag) error { +// return nil // } diff --git a/id3ed_test.go b/id3ed_test.go new file mode 100644 index 0000000..f97be9b --- /dev/null +++ b/id3ed_test.go @@ -0,0 +1,45 @@ +package id3ed + +import ( + "path/filepath" + "testing" + + v1 "github.com/Unbewohnte/id3ed/v1" +) + +var TESTDATAPATH string = "testData" + +func TestOpen(t *testing.T) { + file, err := Open(filepath.Join(TESTDATAPATH, "testreadv2.mp3")) + if err != nil { + t.Errorf("Open failed: %s", err) + } + + if file.ContainsID3v1 { + t.Error("Open failed: expected testing file to not contain ID3v1") + } + + if !file.ContainsID3v2 { + t.Error("Open failed: expected testing file to contain ID3v2") + } +} + +func TestWriteID3v1(t *testing.T) { + file, err := Open(filepath.Join(TESTDATAPATH, "testwritev1.mp3")) + if err != nil { + t.Errorf("Open failed: %s", err) + } + v1tag := &v1.ID3v1Tag{ + SongName: "testsong", + Artist: "testartist", + Album: "testalbum", + Year: 727, + Comment: "testcomment", + Genre: "Blues", + } + + err = file.WriteID3v1(v1tag) + if err != nil { + t.Errorf("WriteID3v1 failed: %s", err) + } +} diff --git a/testData/testwritev1.mp3 b/testData/testwritev1.mp3 index 0f5e9ce3a45a8d91962d06545a5a12bfdb05f957..064e8daeb1ece17c884abb9f08781edeacf30c65 100644 GIT binary patch delta 9 QcmZ1|ww!T-+QbEo01?##$N&HU literal 2849 zcmeH|F>b^l5Jjmu#Xmr*G+U?HG?CI+O1raW9FTxXU|1`+58m3n3q$+o&lBQFR7ng+ zO7L9Rswc%N`KI>HP-(MgXDwxiu;5SH&7ri6-xicXSa^kU)%pV67Qn6{M)WS><8y*6 zaP23Cnfmzj_FiqPjIYw$lUze&Bvvo3qd*S{4z(kD-_s*VE7tbX6CnB9?4B*9qM_