From 8c20aaa30d2ca7483e56c2a3cb95e83a051013ea Mon Sep 17 00:00:00 2001
From: Bohdan Horbeshko <bodqhrohro@gmail.com>
Date: Mon, 31 Jan 2022 02:05:42 -0500
Subject: Fix links to not-yet-downloaded files

---
 telegram/handlers.go | 13 +++++--------
 telegram/utils.go    | 16 +++++++++-------
 2 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/telegram/handlers.go b/telegram/handlers.go
index cf8b3d8..63f981f 100644
--- a/telegram/handlers.go
+++ b/telegram/handlers.go
@@ -1,7 +1,6 @@
 package telegram
 
 import (
-	"crypto/sha256"
 	"fmt"
 	"os"
 	"path/filepath"
@@ -192,7 +191,10 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
 
 		// download file(s)
 		if file != nil && !file.Local.IsDownloadingCompleted {
-			c.DownloadFile(file.Id, 10, false)
+			newFile, err := c.DownloadFile(file.Id, 10, true)
+			if err == nil {
+				file = newFile
+			}
 		}
 		// OTR support (I do not know why would you need it, seriously)
 		if !strings.HasPrefix(text, "?OTR") {
@@ -254,12 +256,7 @@ func (c *Client) updateFile(update *client.UpdateFile) {
 
 	err := os.Symlink(
 		update.File.Local.Path,
-		fmt.Sprintf(
-			"%s/%s%s",
-			c.content.Path,
-			fmt.Sprintf("%x", sha256.Sum256([]byte(update.File.Remote.Id))),
-			filepath.Ext(update.File.Local.Path),
-		),
+		c.formatFilePath(c.content.Path, update.File.Remote.Id, filepath.Ext(update.File.Local.Path)),
 	)
 	if err != nil {
 		linkErr := err.(*os.LinkError)
diff --git a/telegram/utils.go b/telegram/utils.go
index be48497..4de55c6 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -303,15 +303,17 @@ func (c *Client) formatContent(file *client.File, filename string) string {
 	}
 
 	return fmt.Sprintf(
-		"%s (%v kbytes) | %s/%s%s",
+		"%s (%v kbytes) | %s",
 		filename,
 		file.Size/1024,
-		c.content.Link,
-		fmt.Sprintf("%x", sha256.Sum256([]byte(file.Remote.Id))),
-		filepath.Ext(filename),
+		c.formatFilePath(c.content.Link, file.Remote.Id, filepath.Ext(file.Local.Path)),
 	)
 }
 
+func (c *Client) formatFilePath(basedir string, id string, ext string) string {
+	return fmt.Sprintf("%s/%x%s", basedir, sha256.Sum256([]byte(id)), ext)
+}
+
 func (c *Client) formatRestrict(ban bool, hours int64) client.ChatMemberStatus {
 	var until int32
 	if hours != 0 {
@@ -453,13 +455,13 @@ func (c *Client) contentToFilename(content client.MessageContent) (*client.File,
 		return nil, ""
 	case client.TypeMessageAudio:
 		audio, _ := content.(*client.MessageAudio)
-		return audio.Audio.Audio, audio.Audio.FileName
+		return audio.Audio.Audio, filepath.Base(audio.Audio.Audio.Local.Path)
 	case client.TypeMessageVideo:
 		video, _ := content.(*client.MessageVideo)
-		return video.Video.Video, video.Video.FileName
+		return video.Video.Video, filepath.Base(video.Video.Video.Local.Path)
 	case client.TypeMessageDocument:
 		document, _ := content.(*client.MessageDocument)
-		return document.Document.Document, document.Document.FileName
+		return document.Document.Document, filepath.Base(document.Document.Document.Local.Path)
 	}
 
 	return nil, ""
-- 
cgit v1.2.3