Skip to content

Commit

Permalink
feat: enhance file upload progress reporting and update regex for joi…
Browse files Browse the repository at this point in the history
…n links
  • Loading branch information
AmarnathCJD committed Nov 28, 2024
1 parent 0656f8b commit 9ddf1db
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
31 changes: 26 additions & 5 deletions examples/progress/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package examples
// https://gist.github.com/AmarnathCJD/bfceefe9efd1a079ab151da54ef3bba2

import (
"fmt"
"strings"

"github.com/amarnathcjd/gogram/telegram"
)

Expand All @@ -25,12 +28,30 @@ func main() {
chat, _ := client.ResolvePeer("chatId")
m, _ := client.SendMessage(chat, "Starting File Upload...")

var pm = telegram.NewProgressManager(5)
pm.Edit(func(a, b int64) {
client.EditMessage(chat, m.ID, pm.GetStats(a))
})
var pm = telegram.NewProgressManager(5) // 5: Update every 5 seconds

client.SendMedia(chat, "<file-name>", &telegram.MediaOptions{
ProgressManager: pm,
ProgressManager: pm.WithEdit(MediaDownloadProgress("<file-name>", m, pm)),
})
}

func MediaDownloadProgress(fname string, editMsg *telegram.NewMessage, pm *telegram.ProgressManager) func(atotalBytes, currentBytes int64) {
return func(totalBytes int64, currentBytes int64) {
text := ""
text += "<b>📄 Name:</b> <code>%s</code>\n"
text += "<b>💾 File Size:</b> <code>%.2f MiB</code>\n"
text += "<b>⌛️ ETA:</b> <code>%s</code>\n"
text += "<b>⏱ Speed:</b> <code>%s</code>\n"
text += "<b>⚙️ Progress:</b> %s <code>%.2f%%</code>"

size := float64(totalBytes) / 1024 / 1024
eta := pm.GetETA(currentBytes)
speed := pm.GetSpeed(currentBytes)
percent := pm.GetProgress(currentBytes)

progressbar := strings.Repeat("■", int(percent/10)) + strings.Repeat("□", 10-int(percent/10))

message := fmt.Sprintf(text, fname, size, eta, speed, progressbar, percent)
editMsg.Edit(message)
}
}
2 changes: 1 addition & 1 deletion telegram/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const (

var (
USERNAME_RE = regexp.MustCompile(`(?i)@|(?:https?://)?(?:www\.)?(?:telegram\.(?:me|dog)|t\.me)/(@|\+|joinchat/)?`)
TG_JOIN_RE = regexp.MustCompile(`(?i)tg://join\?invite=([a-z0-9_\-]{22})`)
TG_JOIN_RE = regexp.MustCompile(`^(?:https?://)?(?:www\.)?t(?:elegram)?\.(?:org|me|dog)/(?:joinchat/|\+)([\w-]+)$`)
)

var (
Expand Down
18 changes: 12 additions & 6 deletions telegram/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ func (c *Client) DownloadChunk(media any, start int, end int, chunkSize int) ([]
type ProgressManager struct {
startTime int64
editInterval int
editFunc func(a, b int64)
editFunc func(totalSize, currentSize int64)
totalSize int64
lastPerc float64
}
Expand All @@ -696,8 +696,13 @@ func NewProgressManager(editInterval int) *ProgressManager {
}
}

// a: total size, b: current size
func (pm *ProgressManager) Edit(editFunc func(a, b int64)) {
// WithEdit sets the edit function for the progress manager.
func (pm *ProgressManager) WithEdit(editFunc func(totalSize, currentSize int64)) *ProgressManager {
pm.editFunc = editFunc
return pm
}

func (pm *ProgressManager) Edit(editFunc func(totalSize, currentSize int64)) {
pm.editFunc = editFunc
}

Expand All @@ -711,9 +716,10 @@ func (pm *ProgressManager) PrintFunc() func(a, b int64) {
}
}

func (pm *ProgressManager) EditFunc(msg *NewMessage) func(a, b int64) {
// specify the message to edit
func (pm *ProgressManager) WithMessage(msg *NewMessage) func(a, b int64) {
return func(a, b int64) {
_, _ = msg.Client.EditMessage(msg.Peer, msg.ID, pm.GetStats(b))
msg.Edit(pm.GetStats(b))
}
}

Expand Down Expand Up @@ -756,7 +762,7 @@ func (pm *ProgressManager) GetStats(currentBytes int64) string {
}

func (pm *ProgressManager) GenProgressBar(b int64) string {
barLength := 50
barLength := 20
progress := int((pm.GetProgress(b) / 100) * float64(barLength))
bar := "["

Expand Down

0 comments on commit 9ddf1db

Please sign in to comment.