Skip to content

Commit 1dbe6f9

Browse files
committed
format toWasm/fromWasm
1 parent 20fc504 commit 1dbe6f9

File tree

4 files changed

+56
-55
lines changed

4 files changed

+56
-55
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func main() {
8383
// Image metadata (without reading actual image data)
8484
for i, img := range properties.Images {
8585
fmt.Printf("Image %d - Type: %s, Description: %s, MIME type: %s\n",
86-
i, img.Type, img.Description, img.MimeType)
86+
i, img.Type, img.Description, img.MIMEType)
8787
}
8888
}
8989
```

taglib.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,27 @@ taglib_file_read_properties(const char *filename) {
106106
const auto &pictures = file.complexProperties("PICTURE");
107107

108108
props->imageMetadata = nullptr;
109-
if (!pictures.isEmpty()) {
110-
size_t len = pictures.size();
111-
char **imageMetadata =
112-
static_cast<char **>(malloc(sizeof(char *) * (len + 1)));
113-
if (imageMetadata) {
114-
size_t i = 0;
115-
for (const auto &p : pictures) {
116-
TagLib::String type = p["pictureType"].toString();
117-
TagLib::String desc = p["description"].toString();
118-
TagLib::String mime = p["mimeType"].toString();
119-
TagLib::String row = type + "\t" + desc + "\t" + mime;
120-
imageMetadata[i] = to_char_array(row);
121-
i++;
122-
}
123-
imageMetadata[len] = nullptr;
124-
props->imageMetadata = imageMetadata;
125-
}
109+
if (pictures.isEmpty())
110+
return props;
111+
112+
size_t len = pictures.size();
113+
char **imageMetadata =
114+
static_cast<char **>(malloc(sizeof(char *) * (len + 1)));
115+
if (!imageMetadata)
116+
return props;
117+
118+
size_t i = 0;
119+
for (const auto &p : pictures) {
120+
TagLib::String type = p["pictureType"].toString();
121+
TagLib::String desc = p["description"].toString();
122+
TagLib::String mime = p["mimeType"].toString();
123+
TagLib::String row = type + "\t" + desc + "\t" + mime;
124+
imageMetadata[i] = to_char_array(row);
125+
i++;
126126
}
127+
imageMetadata[len] = nullptr;
128+
129+
props->imageMetadata = imageMetadata;
127130

128131
return props;
129132
}

taglib.go

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ type ImageDesc struct {
188188
Type string
189189
// Description is a textual description of the image
190190
Description string
191-
// MimeType is the MIME type of the image (e.g., "image/jpeg")
192-
MimeType string
191+
// MIMEType is the MIME type of the image (e.g., "image/jpeg")
192+
MIMEType string
193193
}
194194

195195
// ReadProperties reads the audio properties from a file at the given path.
@@ -215,13 +215,14 @@ func ReadProperties(path string) (Properties, error) {
215215
var images []ImageDesc
216216
for _, row := range raw.imageDescs {
217217
parts := strings.SplitN(row, "\t", 3)
218-
if len(parts) == 3 {
219-
images = append(images, ImageDesc{
220-
Type: parts[0],
221-
Description: parts[1],
222-
MimeType: parts[2],
223-
})
218+
if len(parts) != 3 {
219+
continue
224220
}
221+
images = append(images, ImageDesc{
222+
Type: parts[0],
223+
Description: parts[1],
224+
MIMEType: parts[2],
225+
})
225226
}
226227

227228
return Properties{
@@ -280,7 +281,7 @@ func ReadImage(path string) ([]byte, error) {
280281
func WriteImage(path string, image []byte) error {
281282
mimeType := ""
282283
if image != nil {
283-
mimeType = detectImageMimeType(image)
284+
mimeType = detectImageMIME(image)
284285
}
285286
return WriteImageOptions(path, image, 0, "Front Cover", "Added by go-taglib", mimeType)
286287
}
@@ -430,82 +431,78 @@ func (m *module) malloc(size uint32) uint32 {
430431
}
431432

432433
type wasmArg interface {
433-
toWasm(*module) uint64
434+
encode(*module) uint64
434435
}
435436

436437
type wasmResult interface {
437-
fromWasm(*module, uint64)
438+
decode(*module, uint64)
438439
}
439440

440441
type wasmBool bool
441442

442-
func (b wasmBool) toWasm(*module) uint64 {
443+
func (b wasmBool) encode(*module) uint64 {
443444
if b {
444445
return 1
445446
}
446447
return 0
447448
}
448449

449-
func (b *wasmBool) fromWasm(_ *module, val uint64) {
450+
func (b *wasmBool) decode(_ *module, val uint64) {
450451
*b = val == 1
451452
}
452453

453454
type wasmInt int
454455

455-
func (i wasmInt) toWasm(*module) uint64 { return uint64(i) }
456-
457-
func (i *wasmInt) fromWasm(_ *module, val uint64) {
456+
func (i wasmInt) encode(*module) uint64 { return uint64(i) }
457+
func (i *wasmInt) decode(_ *module, val uint64) {
458458
*i = wasmInt(val)
459459
}
460460

461461
type wasmUint8 uint8
462462

463-
func (u wasmUint8) toWasm(*module) uint64 { return uint64(u) }
463+
func (u wasmUint8) encode(*module) uint64 { return uint64(u) }
464464

465465
type wasmUint32 uint32
466466

467-
func (u wasmUint32) toWasm(*module) uint64 { return uint64(u) }
468-
469-
func (u *wasmUint32) fromWasm(_ *module, val uint64) {
467+
func (u wasmUint32) encode(*module) uint64 { return uint64(u) }
468+
func (u *wasmUint32) decode(_ *module, val uint64) {
470469
*u = wasmUint32(val)
471470
}
472471

473472
type wasmString string
474473

475-
func (s wasmString) toWasm(m *module) uint64 {
474+
func (s wasmString) encode(m *module) uint64 {
476475
b := append([]byte(s), 0)
477476
ptr := m.malloc(uint32(len(b)))
478477
if !m.mod.Memory().Write(ptr, b) {
479478
panic("failed to write to mod.module.Memory()")
480479
}
481480
return uint64(ptr)
482481
}
483-
484-
func (s *wasmString) fromWasm(m *module, val uint64) {
482+
func (s *wasmString) decode(m *module, val uint64) {
485483
if val != 0 {
486484
*s = wasmString(readString(m, uint32(val)))
487485
}
488486
}
489487

490488
type wasmBytes []byte
491489

492-
func (b wasmBytes) toWasm(m *module) uint64 {
490+
func (b wasmBytes) encode(m *module) uint64 {
493491
ptr := m.malloc(uint32(len(b)))
494492
if !m.mod.Memory().Write(ptr, b) {
495493
panic("failed to write to mod.module.Memory()")
496494
}
497495
return uint64(ptr)
498496
}
499-
500-
func (b *wasmBytes) fromWasm(m *module, val uint64) {
497+
func (b *wasmBytes) decode(m *module, val uint64) {
501498
if val != 0 {
502499
*b = readBytes(m, uint32(val))
503500
}
504501
}
505502

506503
type wasmStrings []string
507504

508-
func (s wasmStrings) toWasm(m *module) uint64 {
505+
func (s wasmStrings) encode(m *module) uint64 {
509506
arrayPtr := m.malloc(uint32((len(s) + 1) * 4))
510507
for i, str := range s {
511508
b := append([]byte(str), 0)
@@ -522,8 +519,7 @@ func (s wasmStrings) toWasm(m *module) uint64 {
522519
}
523520
return uint64(arrayPtr)
524521
}
525-
526-
func (s *wasmStrings) fromWasm(m *module, val uint64) {
522+
func (s *wasmStrings) decode(m *module, val uint64) {
527523
if val != 0 {
528524
*s = readStrings(m, uint32(val))
529525
}
@@ -537,7 +533,7 @@ type wasmFileProperties struct {
537533
imageDescs []string
538534
}
539535

540-
func (f *wasmFileProperties) fromWasm(m *module, val uint64) {
536+
func (f *wasmFileProperties) decode(m *module, val uint64) {
541537
if val == 0 {
542538
return
543539
}
@@ -557,7 +553,7 @@ func (f *wasmFileProperties) fromWasm(m *module, val uint64) {
557553
func (m *module) call(name string, dest wasmResult, args ...wasmArg) error {
558554
params := make([]uint64, 0, len(args))
559555
for _, a := range args {
560-
params = append(params, a.toWasm(m))
556+
params = append(params, a.encode(m))
561557
}
562558

563559
results, err := m.mod.ExportedFunction(name).Call(context.Background(), params...)
@@ -568,7 +564,7 @@ func (m *module) call(name string, dest wasmResult, args ...wasmArg) error {
568564
return nil
569565
}
570566

571-
dest.fromWasm(m, results[0])
567+
dest.decode(m, results[0])
572568
return nil
573569
}
574570

@@ -604,6 +600,7 @@ func readString(m *module, ptr uint32) string {
604600
if i := bytes.IndexByte(buf, 0); i >= 0 {
605601
return string(buf[:i])
606602
}
603+
607604
for {
608605
next, ok := m.mod.Memory().Read(ptr+size, size)
609606
if !ok {
@@ -634,9 +631,10 @@ func readBytes(m *module, ptr uint32) []byte {
634631
panic("memory error")
635632
}
636633

637-
// copy the data, "this returns a view of the underlying memory, not a copy." per api.memory.read docs
634+
// copy the data, "this returns a view of the underlying memory, not a copy" per api.Memory.Read docs
638635
ret = make([]byte, size)
639636
copy(ret, b)
637+
640638
return ret
641639
}
642640

@@ -645,9 +643,9 @@ func wasmPath(p string) string {
645643
return filepath.ToSlash(p)
646644
}
647645

648-
// detectImageMimeType detects image MIME type from magic bytes.
646+
// detectImageMIME detects image MIME type from magic bytes.
649647
// Adapted from Go's net/http package to avoid the dependency.
650-
func detectImageMimeType(data []byte) string {
648+
func detectImageMIME(data []byte) string {
651649
if len(data) < 2 {
652650
return ""
653651
}

taglib_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ func TestProperties(t *testing.T) {
235235
eq(t, len(properties.Images), 2)
236236
eq(t, properties.Images[0].Type, "Front Cover")
237237
eq(t, properties.Images[0].Description, "The first image")
238-
eq(t, properties.Images[0].MimeType, "image/png")
238+
eq(t, properties.Images[0].MIMEType, "image/png")
239239
eq(t, properties.Images[1].Type, "Lead Artist")
240240
eq(t, properties.Images[1].Description, "The second image")
241-
eq(t, properties.Images[1].MimeType, "image/jpeg")
241+
eq(t, properties.Images[1].MIMEType, "image/jpeg")
242242
}
243243

244244
func TestMultiOpen(t *testing.T) {

0 commit comments

Comments
 (0)