Skip to content

Commit

Permalink
bug fixes, code reformat, logging reformats, uploads is now well stab…
Browse files Browse the repository at this point in the history
…le v2.3.12
  • Loading branch information
AmarnathCJD committed Jun 6, 2024
1 parent e29818b commit 935c959
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 27 deletions.
11 changes: 10 additions & 1 deletion mtproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (m *MTProto) ExportNewSender(dcID int, mem bool) (*MTProto, error) {
}
sender, _ := NewMTProto(cfg)
m.Logger.Info("exporting new sender for DC " + strconv.Itoa(dcID))
err = sender.CreateConnection(true)
err = sender.CreateConnection(false)
if err != nil {
return nil, errors.Wrap(err, "creating connection")
}
Expand All @@ -269,6 +269,8 @@ func (m *MTProto) CreateConnection(withLog bool) error {
m.stopRoutines = cancelfunc
if withLog {
m.Logger.Info("Connecting to [" + m.Addr + "] - <Tcp> ...")
} else {
m.Logger.Debug("Connecting to [" + m.Addr + "] - <Tcp> ...")
}
err := m.connect(ctx)
if err != nil {
Expand All @@ -281,7 +283,14 @@ func (m *MTProto) CreateConnection(withLog bool) error {
} else {
m.Logger.Info("Connection to [" + m.Addr + "] - <Tcp> established")
}
} else {
if m.proxy != nil && m.proxy.Host != "" {
m.Logger.Debug("Connection to (~" + m.proxy.Host + ")[" + m.Addr + "] - <Tcp> established")
} else {
m.Logger.Debug("Connection to [" + m.Addr + "] - <Tcp> established")
}
}

m.startReadingResponses(ctx)
if !m.encrypted {
m.Logger.Debug("authKey not found, creating new one")
Expand Down
2 changes: 1 addition & 1 deletion telegram/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "regexp"

const (
ApiVersion = 181
Version = "v2.3.11"
Version = "v2.3.12"

LogDebug = "debug"
LogInfo = "info"
Expand Down
16 changes: 7 additions & 9 deletions telegram/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,15 +602,13 @@ func gatherVideoMetadata(path string, attrs []DocumentAttribute) ([]DocumentAttr
}

lines := strings.Split(strings.TrimSpace(string(out)), "\n")
if len(lines) == 3 {
dur, _ = strconv.ParseFloat(strings.TrimSpace(lines[2]), 64)
width, _ = strconv.ParseInt(strings.TrimSpace(lines[0]), 10, 32)
height, _ = strconv.ParseInt(strings.TrimSpace(lines[1]), 10, 32)
} else {
width, _ = strconv.ParseInt(strings.TrimSpace(lines[0]), 10, 32)
height, _ = strconv.ParseInt(strings.TrimSpace(lines[1]), 10, 32)
if len(lines) > 4 {
dur, _ = strconv.ParseFloat(strings.TrimSpace(lines[4]), 64)
for i, line := range lines {
if i == 0 {
width, _ = strconv.ParseInt(strings.TrimSpace(line), 10, 32)
} else if i == 1 {
height, _ = strconv.ParseInt(strings.TrimSpace(line), 10, 32)
} else if i == len(lines)-1 {
dur, _ = strconv.ParseFloat(strings.TrimSpace(line), 64)
}
}

Expand Down
35 changes: 19 additions & 16 deletions telegram/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (c *Client) UploadFile(src interface{}, Opts ...*UploadOptions) (InputFile,
}
}

c.Logger.Debug(fmt.Sprintf("expected workers: %d, preallocated workers: %d", numWorkers, sendersPreallocated))
for i := sendersPreallocated; i < numWorkers; i++ {
x, _ := c.CreateExportedSender(c.GetDC())
// go c.AddNewExportedSenderToMap(c.GetDC(), x) TODO: Implement this
Expand All @@ -130,20 +131,16 @@ func (c *Client) UploadFile(src interface{}, Opts ...*UploadOptions) (InputFile,
go func(i int, part []byte, p int) {
defer wg.Done()
uploadStartPoint:
c.Logger.Debug("Uploading part", p, "with size", len(part), "in KB:", len(part)/1024, "to", i)
c.Logger.Debug(fmt.Sprintf("uploading part %d/%d in chunks of %d", p, totalParts, len(part)/1024))
if !IsFsBig {
_, err = sender[i].c.UploadSaveFilePart(fileId, int32(p), part)
hash.Write(part)
} else {
_, err = sender[i].c.UploadSaveBigFilePart(fileId, int32(p), int32(totalParts), part)
}
if err != nil {
if matchError(err, "FLOOD_WAIT_") {
if waitTime := getFloodWait(err); waitTime > 0 {
c.Logger.Warn("flood wait", waitTime, "seconds, waiting...")
time.Sleep(time.Duration(waitTime) * time.Second)
goto uploadStartPoint
}
if handleIfFlood(err, c) {
goto uploadStartPoint
}
c.Logger.Error(err)
}
Expand All @@ -170,29 +167,23 @@ func (c *Client) UploadFile(src interface{}, Opts ...*UploadOptions) (InputFile,
}

lastPartUploadStartPoint:
c.Logger.Debug("Uploading last part", totalParts-1, "with size", len(part), "in KB:", len(part)/1024)
c.Logger.Debug(fmt.Sprintf("uploading last part %d/%d in chunks of %d", totalParts-1, totalParts, len(part)/1024))
if !IsFsBig {
_, err = c.UploadSaveFilePart(fileId, int32(totalParts)-1, part)
} else {
_, err = c.UploadSaveBigFilePart(fileId, int32(totalParts)-1, int32(totalParts), part)
}

if err != nil {
if matchError(err, "FLOOD_WAIT_") {
if waitTime := getFloodWait(err); waitTime > 0 {
c.Logger.Warn("flood wait", waitTime, "seconds, waiting...")
time.Sleep(time.Duration(waitTime) * time.Second)
goto lastPartUploadStartPoint
}
if handleIfFlood(err, c) {
goto lastPartUploadStartPoint
}
c.Logger.Error(err)
}

if opts.ProgressCallback != nil {
go opts.ProgressCallback(int32(totalParts), int32(totalParts))
}

c.Logger.Debug("Uploaded last part", totalParts-1, "with size", len(part), "in KB:", len(part)/1024)
}

wg.Wait()
Expand Down Expand Up @@ -225,6 +216,18 @@ func (c *Client) UploadFile(src interface{}, Opts ...*UploadOptions) (InputFile,
}, nil
}

func handleIfFlood(err error, c *Client) bool {
if matchError(err, "FLOOD_WAIT_") {
if waitTime := getFloodWait(err); waitTime > 0 {
c.Logger.Warn("flood wait", waitTime, "seconds, waiting...")
time.Sleep(time.Duration(waitTime) * time.Second)
return true
}
}

return false
}

func prettifyFileName(file string) string {
return filepath.Base(file)
}
Expand Down

0 comments on commit 935c959

Please sign in to comment.