MCPcopy Create free account
hub / github.com/blues/note / hubReadFile

Function hubReadFile

lib/hubreq.go:1397–1468  ·  view source on GitHub ↗

Read a file range, with message fields being used/overloaded as follows: NotefileIDs is the filename Since is the offset Until is the length NoteID is the file type MaxChanges is an indication (a maximum size on device) to return the binary as a binary payload

(ctx context.Context, session *HubSession, req notehubMessage, rsp *notehubMessage, event EventFunc)

Source from the content-addressed store, hash-verified

1395// NoteID is the file type
1396// MaxChanges is an indication (a maximum size on device) to return the binary as a binary payload
1397func hubReadFile(ctx context.Context, session *HubSession, req notehubMessage, rsp *notehubMessage, event EventFunc) (err error) {
1398 // If callback not set, this function can't function
1399 if fnHubReadFile == nil {
1400 err = fmt.Errorf("hub is lacking the capability to read an uploaded file")
1401 return
1402 }
1403
1404 // Open the box
1405 box, _, appUID, err2 := openHubNoteboxForDevice(ctx, session, req.DeviceUID, req.DeviceSN, req.ProductUID, req.DeviceEndpointID, event)
1406 if err2 != nil {
1407 err = err2
1408 return
1409 }
1410
1411 // Get the request parameters, which we overload onto other fields in the protobuf
1412 filename := req.NotefileIDs
1413 offset := int32(req.Since)
1414 length := int32(req.Until)
1415 filetype := notehub.ParseUploadType(req.NoteID)
1416 returnUncompressedBinary := req.MaxChanges != 0
1417
1418 // Perform the read, and always do it compressed because the device firmware does a decompress.
1419 getInfo := offset == 0
1420 metadata, payload, err2 := fnHubReadFile(ctx, appUID, filetype, filename, offset, length, !returnUncompressedBinary)
1421 if err2 != nil {
1422 err = err2
1423 box.Close(ctx)
1424 return
1425 }
1426
1427 // locally we have bogus firmware that's created for testing the UI.
1428 // if any device tries to update to this firmware, always return an error
1429 if metadata.Firmware != nil && metadata.Firmware.Description == notehub.TestFirmwareString {
1430 return fmt.Errorf("this firmware is invalid and created for testing purposes only")
1431 }
1432
1433 // If we'd like the binary returned uncompressed as the response payload, do so
1434 if returnUncompressedBinary {
1435 rsp.SetPayload(payload)
1436 payload = nil
1437 }
1438
1439 // Create a note within a new notefile in order to return the result
1440 newNotefile := CreateNotefile(false)
1441 var xnote note.Note
1442 if getInfo {
1443 if bodyEncoded, err2 := note.JSONMarshal(metadata); err2 != nil {
1444 return err2
1445 } else {
1446 xnote, err = note.CreateNote(bodyEncoded, payload)
1447 }
1448 } else {
1449 xnote, err = note.CreateNote(nil, payload)
1450 }
1451 if err != nil {
1452 box.Close(ctx)
1453 return
1454 }

Callers 1

processRequestFunction · 0.85

Calls 6

openHubNoteboxForDeviceFunction · 0.85
CreateNotefileFunction · 0.85
SetPayloadMethod · 0.80
SetNotefileMethod · 0.80
CloseMethod · 0.45
AddNoteMethod · 0.45

Tested by

no test coverage detected