cleaner failures when media fails to load or draw.

This commit is contained in:
fiatjaf
2024-01-12 11:00:52 -03:00
parent 8c5fa1aba8
commit a9c84f86d7
2 changed files with 18 additions and 5 deletions

View File

@@ -733,11 +733,14 @@ func drawShapedBlockAt(
func drawImageAt(img draw.Image, imageUrl string, startY int) int { func drawImageAt(img draw.Image, imageUrl string, startY int) int {
resp, err := http.Get(imageUrl) resp, err := http.Get(imageUrl)
if err != nil { if err != nil {
return startY return -1
} }
defer resp.Body.Close() defer resp.Body.Close()
srcImg, _, err := image.Decode(resp.Body) srcImg, _, err := image.Decode(resp.Body)
if err != nil {
return -1
}
// Resize the fetched image to fit the width of the destination image (img) // Resize the fetched image to fit the width of the destination image (img)
width := img.Bounds().Dx() width := img.Bounds().Dx()
@@ -756,13 +759,16 @@ func drawVideoAt(img draw.Image, videoUrl string, startY int) int {
cmd.Stdout = nil cmd.Stdout = nil
cmd.Stderr = nil cmd.Stderr = nil
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return startY return -1
} }
frame, _ := os.Open(tempImagePath) frame, _ := os.Open(tempImagePath)
defer os.Remove(tempImagePath) defer os.Remove(tempImagePath)
defer frame.Close() defer frame.Close()
imgData, _, _ := image.Decode(frame) imgData, _, err := image.Decode(frame)
if err != nil {
return -1
}
width := img.Bounds().Dx() width := img.Bounds().Dx()
resizedFrame := resize.Resize(uint(width), 0, imgData, resize.Lanczos3) resizedFrame := resize.Resize(uint(width), 0, imgData, resize.Lanczos3)

View File

@@ -217,8 +217,15 @@ func drawParagraphs(paragraphs []string, fontSize int, width, height int) (image
if i == 0 { if i == 0 {
yPos = 0 yPos = 0
} }
yPos = drawMediaAt(img, paragraph, yPos) next := drawMediaAt(img, paragraph, yPos)
continue if next != -1 {
yPos = next
// this means the media picture was successfully drawn
continue
}
// if we reach here that means we didn't draw anything, so proceed to
// draw the text
} }
rawText := []rune(paragraph) rawText := []rune(paragraph)