~bayindirh/nudge

65c2cb0aacec8d5fdb0af160a0234c49f28e16a7 — Hakan Bayindir 7 months ago 5d06036
feat: Read file to memory as a byte array.

This small commit replaces the file open code with reading code which
actually reads the file contents into a byte array. Next step is to
BASE64 encode it and, attach to the notification itself.
2 files changed, 9 insertions(+), 6 deletions(-)

M CHANGELOG.md
M src/nudge.go
M CHANGELOG.md => CHANGELOG.md +3 -0
@@ 2,6 2,9 @@

**Note:** Please add newest entries on top. Use ISO date format YYYY-MM-DD and markdown formatting.

## 2024-02-07
- feat: Read file to memory as a byte array.

## 2024-01-28
- feat: Continue implementing file attachement logic.
- feat: Start calling the `attachImageToNotification` function.

M src/nudge.go => src/nudge.go +6 -6
@@ 496,17 496,17 @@ func attachImageToNotification(notificationToSend *Notification, validConfigurat
		logger.Fatalf("File %s is bigger than %d bytes, which is the upper limit imposed by Pushover API. Try with a smaller image file.", fileInformation.Name(), validConfigurationOptions.maximumFileSizeInBytes)
	}
	
	// Let's try adding the image.
	imageFile, err := os.Open(notificationToSend.imageAttachmentPath)
	
	defer imageFile.Close() // Let's close the file automatically.
	// Let's read the file into a byte array, so we can convert it to BASE64.
	fileContents := make([]byte, fileInformation.Size())

	fileContents, err = os.ReadFile(notificationToSend.imageAttachmentPath)
	
	// If we can't open the file, we catch the problem here.
	if err != nil {
		logger.Fatalf("Opening file returned an error (error is %s).", err)
		logger.Fatalf("Reading file returned an error (error is %s).", err)
	}
	
	logger.Debugf("The file %s has been opened successfully.", fileInformation.Name())
	logger.Debugf("The file %s has been read successfully.", fileInformation.Name())
}

// This function pretty prints application state.