Fix links to not-yet-downloaded files

This commit is contained in:
Bohdan Horbeshko 2022-01-31 02:05:42 -05:00
parent f052d633ac
commit 8c20aaa30d
2 changed files with 14 additions and 15 deletions

View file

@ -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)

View file

@ -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, ""