From 753184816c4171f67b7274ff09faf419b850a91a Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Mon, 10 Mar 2025 20:33:49 +0300 Subject: refactor: handle errors in createTextChunk and remove commented code --- pngmeta/altwriter.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pngmeta/altwriter.go b/pngmeta/altwriter.go index aec972a..3adcb7e 100644 --- a/pngmeta/altwriter.go +++ b/pngmeta/altwriter.go @@ -18,11 +18,7 @@ const ( textChunkType = "tEXt" ) -// // PngEmbed holds the key-value pair to embed into the PNG file. -// type PngEmbed struct { -// Key string -// Value string -// } +// there should be no empty lines inside functions; ai! // WriteToPng embeds the metadata into the specified PNG file and writes the result to outfile. func WriteToPng(metadata *models.CharCardSpec, sourcePath, outfile string) error { @@ -56,7 +52,10 @@ func WriteToPng(metadata *models.CharCardSpec, sourcePath, outfile string) error outputBuffer.Write(chunk) } - newChunk := createTextChunk(embedData) + newChunk, err := createTextChunk(embedData) + if err != nil { + return err + } outputBuffer.Write(newChunk) outputBuffer.Write(iend) @@ -124,7 +123,7 @@ func processChunks(data []byte) ([][]byte, []byte, error) { } // createTextChunk generates a valid tEXt chunk with proper CRC -func createTextChunk(embed PngEmbed) []byte { +func createTextChunk(embed PngEmbed) ([]byte, error) { content := bytes.NewBuffer(nil) content.WriteString(embed.Key) content.WriteByte(0) // Null separator @@ -149,5 +148,5 @@ func createTextChunk(embed PngEmbed) []byte { return nil, fmt.Errorf("error writing CRC: %w", err) } - return chunk.Bytes() + return chunk.Bytes(), nil } -- cgit v1.2.3