Skip to content

Commit

Permalink
refactor ReadIFFFile to accept io.Reader and file length, update GUI …
Browse files Browse the repository at this point in the history
…to read file data before processing
  • Loading branch information
mattrust committed Dec 27, 2024
1 parent 5122e0d commit eb1c7d4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
22 changes: 3 additions & 19 deletions internal/chunks/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import (
"encoding/binary"
"fmt"
"io"
"os"
"slices"

"fyne.io/fyne/v2"
)

// IFFChunk represents a chunk in an IFF file.
Expand All @@ -34,24 +31,11 @@ type IFFChunk struct {
}

// ReadIFFFile reads an IFF file and returns the root chunk.
// fileLen is the length of the file in bytes.
// In case of an error, the function returns nil and the error.
func ReadIFFFile(reader fyne.URIReadCloser) (*IFFChunk, error) {
// TODO: get rid of fyne.URIReadCloser
var chunk *IFFChunk

file, err := os.Open(reader.URI().Path())
if err != nil {
return nil, err
}
defer file.Close()

fileInfo, err := file.Stat()
if err != nil {
return nil, err
}
fileLen := fileInfo.Size()
func ReadIFFFile(reader io.Reader, fileLen int64) (*IFFChunk, error) {

chunk, err = readChunk(reader, nil, fileLen, 0)
chunk, err := readChunk(reader, nil, fileLen, 0)

return chunk, err
}
Expand Down
13 changes: 12 additions & 1 deletion internal/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
package gui

import (
"bytes"
"io"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
Expand Down Expand Up @@ -58,7 +61,15 @@ func OpenGUI(version string) {

appData.topContainer.Refresh()

appData.chunks, err = chunks.ReadIFFFile(reader)
// read the file to get its length
data, err := io.ReadAll(reader)
if err != nil {
dialog.ShowError(err, appData.win)
return
}

appData.chunks, err = chunks.ReadIFFFile(bytes.NewReader(data),
int64(len(data)))
if err != nil {
dialog.ShowError(err, appData.win)
return
Expand Down

0 comments on commit eb1c7d4

Please sign in to comment.