Skip to content

Commit

Permalink
do not overwritte files when Buffer is not loaded in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Mar 25, 2019
1 parent fc05dfd commit f50c8fb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
3 changes: 2 additions & 1 deletion decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ReadQuotas struct {

// ReadResourceCallback defines a callback that will be called when an external resource should be loaded.
// The string parameter is the URI of the resource.
// If the reader and the error are nil the buffer data won't be loaded into memory.
type ReadResourceCallback = func(string) (io.ReadCloser, error)

// Open will open a glTF or GLB file specified by name and return the Document.
Expand Down Expand Up @@ -149,7 +150,7 @@ func (d *Decoder) decodeBuffer(buffer *Buffer) error {
buffer.Data, err = buffer.marshalData()
} else if err = validateBufferURI(buffer.URI); err == nil {
r, err = d.cb(buffer.URI)
if err == nil {
if r != nil && err == nil {
buffer.Data = make([]uint8, buffer.ByteLength)
_, err = r.Read(buffer.Data)
r.Close()
Expand Down
5 changes: 5 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func (e *Encoder) encodeBuffer(buffer *Buffer) error {
if buffer.IsEmbeddedResource() {
return nil
}

if len(buffer.Data) == 0 {
return nil
}

if err := validateBufferURI(buffer.URI); err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ func saveMemory(doc *Document, asBinary bool) (*Decoder, error) {
return nil, err
}
rcb := func(uri string) (io.ReadCloser, error) {
return ioutil.NopCloser(chunks[uri]), nil
if chunk, ok := chunks[uri]; ok {
return ioutil.NopCloser(chunk), nil
} else {
return nil, nil
}
}
return NewDecoder(buff, rcb), nil
}
Expand Down Expand Up @@ -72,6 +76,7 @@ func TestEncoder_Encode(t *testing.T) {
{Extras: 8.0, Name: "binary", ByteLength: 3, URI: "a.bin", Data: []uint8{1, 2, 3}},
{Extras: 8.0, Name: "embedded", ByteLength: 2, URI: "data:application/octet-stream;base64,YW55ICsgb2xkICYgZGF0YQ==", Data: []byte("any + old & data")},
{Extras: 8.0, Name: "external", ByteLength: 4, URI: "b.bin", Data: []uint8{4, 5, 6, 7}},
{Extras: 8.0, Name: "external", ByteLength: 4, URI: "a.drc"},
}}}, false},
{"withBufView", args{&Document{BufferViews: []BufferView{
{Extras: 8.0, Buffer: 0, ByteOffset: 1, ByteLength: 2, ByteStride: 5, Target: ArrayBuffer},
Expand Down
10 changes: 8 additions & 2 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ type SparseIndices struct {
}

// A Buffer points to binary geometry, animation, or skins.
// If Data length is 0 and the Buffer is an external resource the Data won't be flushed,
// which can be useful when there is no need to load data in memory.
type Buffer struct {
Extensions Extensions `json:"extensions,omitempty"`
Extras interface{} `json:"extras,omitempty"`
Expand All @@ -119,10 +121,14 @@ func (b *Buffer) EmbeddedResource() {
// marshalData decode the buffer from the URI. If the buffer is not en embedded resource the returned array will be empty.
func (b *Buffer) marshalData() ([]uint8, error) {
if !b.IsEmbeddedResource() {
return []uint8{}, nil
return nil, nil
}
startPos := len(mimetypeApplicationOctet) + 1
return base64.StdEncoding.DecodeString(b.URI[startPos:])
sl, err := base64.StdEncoding.DecodeString(b.URI[startPos:])
if len(sl) == 0 || err != nil {
return nil, err
}
return sl, nil
}

// BufferView is a view into a buffer generally representing a subset of the buffer.
Expand Down
6 changes: 3 additions & 3 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,9 @@ func TestBuffer_marshalData(t *testing.T) {
want []uint8
wantErr bool
}{
{"error", &Buffer{URI: "data:application/octet-stream;base64,_"}, []uint8{}, true},
{"external", &Buffer{URI: "http://web.com"}, []uint8{}, false},
{"empty", &Buffer{URI: "data:application/octet-stream;base64,"}, []uint8{}, false},
{"error", &Buffer{URI: "data:application/octet-stream;base64,_"}, nil, true},
{"external", &Buffer{URI: "http://web.com"}, nil, false},
{"empty", &Buffer{URI: "data:application/octet-stream;base64,"}, nil, false},
{"test", &Buffer{URI: "data:application/octet-stream;base64,TEST"}, []uint8{76, 68, 147}, false},
{"complex", &Buffer{URI: "data:application/octet-stream;base64,YW55IGNhcm5hbCBwbGVhcw=="}, []uint8{97, 110, 121, 32, 99, 97, 114, 110, 97, 108, 32, 112, 108, 101, 97, 115}, false},
}
Expand Down

0 comments on commit f50c8fb

Please sign in to comment.