diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2022-03-09 22:15:56 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2022-03-09 22:15:56 +0300 |
commit | 699d75552abf35b243e9f589b24c7d880a8c7423 (patch) | |
tree | 2d2ab7a5082fcd7235504dc74170c14b0bb2cc84 /telegram/utils.go | |
parent | 4307f85a048e7f6783f112b9f243588e305a16fc (diff) |
Move downloaded files to a permanent location
Diffstat (limited to 'telegram/utils.go')
-rw-r--r-- | telegram/utils.go | 83 |
1 files changed, 59 insertions, 24 deletions
diff --git a/telegram/utils.go b/telegram/utils.go index d9773c0..6c16a74 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -2,11 +2,11 @@ package telegram import ( "crypto/sha1" - "crypto/sha256" "fmt" "github.com/pkg/errors" "io" "os" + osUser "os/user" "path/filepath" "regexp" "strconv" @@ -355,21 +355,56 @@ func (c *Client) formatForward(fwd *client.MessageForwardInfo) string { return "Unknown forward type" } -func (c *Client) formatContent(file *client.File, filename string) string { - if file == nil { +func (c *Client) formatFile(file *client.File) string { + if file == nil || file.Local == nil || file.Remote == nil { return "" } - return fmt.Sprintf( - "%s (%v kbytes) | %s", - filename, - file.Size/1024, - c.formatFilePath(c.content.Link, file.Remote.UniqueId, filepath.Ext(file.Local.Path)), - ) -} + var link string + var src string + + if c.content.Path != "" && c.content.Link != "" { + src = file.Local.Path // source path + _, err := os.Stat(src) + if err != nil { + return "" + } -func (c *Client) formatFilePath(basedir string, id string, ext string) string { - return fmt.Sprintf("%s/%x%s", basedir, sha256.Sum256([]byte(id)), ext) + basename := file.Remote.UniqueId + filepath.Ext(src) + dest := c.content.Path + "/" + basename // destination path + link = c.content.Link + "/" + basename // download link + + // move + err = os.Rename(src, dest) + if err != nil { + linkErr := err.(*os.LinkError) + if linkErr.Err.Error() == "file exists" { + log.Warn(err.Error()) + } else { + log.Errorf("File moving error: %v", err) + return "<ERROR>" + } + } + // chown + if c.content.User != "" { + user, err := osUser.Lookup(c.content.User) + if err == nil { + uid, err := strconv.ParseInt(user.Uid, 10, 0) + if err == nil { + err = os.Chown(dest, int(uid), -1) + if err != nil { + log.Errorf("Chown error: %v", err) + } + } else { + log.Errorf("Broken uid: %v", err) + } + } else { + log.Errorf("Wrong user name for chown: %v", err) + } + } + } + + return fmt.Sprintf("%s (%v kbytes) | %s", filepath.Base(src), file.Size/1024, link) } func (c *Client) formatBantime(hours int64) int32 { @@ -573,44 +608,44 @@ func (c *Client) messageToText(message *client.Message, preview bool) string { return fmt.Sprintf("unknown message (%s)", message.Content.MessageContentType()) } -func (c *Client) contentToFilename(content client.MessageContent) (*client.File, string) { +func (c *Client) contentToFile(content client.MessageContent) *client.File { if content == nil { - return nil, "" + return nil } switch content.MessageContentType() { case client.TypeMessageSticker: sticker, _ := content.(*client.MessageSticker) - return sticker.Sticker.Sticker, "sticker.webp" + return sticker.Sticker.Sticker case client.TypeMessageVoiceNote: voice, _ := content.(*client.MessageVoiceNote) - return voice.VoiceNote.Voice, fmt.Sprintf("voice note (%v s.).oga", voice.VoiceNote.Duration) + return voice.VoiceNote.Voice case client.TypeMessageVideoNote: video, _ := content.(*client.MessageVideoNote) - return video.VideoNote.Video, fmt.Sprintf("video note (%v s.).mp4", video.VideoNote.Duration) + return video.VideoNote.Video case client.TypeMessageAnimation: animation, _ := content.(*client.MessageAnimation) - return animation.Animation.Animation, "animation.mp4" + return animation.Animation.Animation case client.TypeMessagePhoto: photo, _ := content.(*client.MessagePhoto) sizes := photo.Photo.Sizes if len(sizes) >= 1 { file := sizes[len(sizes)-1].Photo - return file, strconv.FormatInt(int64(file.Id), 10) + ".jpg" + return file } - return nil, "" + return nil case client.TypeMessageAudio: audio, _ := content.(*client.MessageAudio) - return audio.Audio.Audio, filepath.Base(audio.Audio.Audio.Local.Path) + return audio.Audio.Audio case client.TypeMessageVideo: video, _ := content.(*client.MessageVideo) - return video.Video.Video, filepath.Base(video.Video.Video.Local.Path) + return video.Video.Video case client.TypeMessageDocument: document, _ := content.(*client.MessageDocument) - return document.Document.Document, filepath.Base(document.Document.Document.Local.Path) + return document.Document.Document } - return nil, "" + return nil } func (c *Client) messageToPrefix(message *client.Message, fileString string) string { |