diff --git a/NTFServer.go b/NTFServer.go
index f24144a..09da566 100644
--- a/NTFServer.go
+++ b/NTFServer.go
@@ -139,6 +139,9 @@ func NewNTFServer(configFile string, verbose bool) (*NTFServer, error) {
log.Fatalf("Couldn't reconnect upstream for '%s': %s", hubNick, err.Error()) // fatal - inconsistent DB is the only possible cause
}
+ // Slight stagger
+ time.Sleep(50 * time.Millisecond)
+
launchedAny = true
}
@@ -440,9 +443,80 @@ func (this *NTFServer) HandleGroupMessage(update telegram.Update) error {
userID := int64(update.Message.From.ID)
- if update.Message.Sticker != nil && this.contentedMaxBytes > 0 {
+ if this.contentedMaxBytes > 0 {
+ // File upload types
+
+ // Audio
+ if update.Message.Audio != nil {
+ go func() {
+ conUrl, err := this.ContentedUploadSync(update.Message.Audio.FileID, int64(update.Message.Audio.FileSize)) // no thumbnail fallback available
+ this.uploadAsyncComplete(userID, "audio", conUrl, err,
+ fmt.Sprintf("Audio '%s' duration %ds: %s", update.Message.Audio.Title, update.Message.Audio.Duration, update.Message.Caption))
+ }()
+ }
+
+ // Document
+ if update.Message.Document != nil {
+ go func() {
+ conUrl, err := this.ContentedUploadFallbackSync(update.Message.Document.FileID, int64(update.Message.Document.FileSize), update.Message.Document.Thumbnail)
+ this.uploadAsyncComplete(userID, "document", conUrl, err, update.Message.Caption)
+ }()
+ }
+
+ // Photo
+ if update.Message.Photo != nil {
+ go func() {
+ conUrl, err := this.ContentedUploadBestSync(*update.Message.Photo)
+ this.uploadAsyncComplete(userID, "photo", conUrl, err, "")
+ }()
+ }
+
// Sticker
- go this.uploadStickerWorker(userID, update.Message.Sticker)
+ if update.Message.Sticker != nil {
+ go func() {
+ conUrl, err := this.ContentedUploadFallbackSync(update.Message.Sticker.FileID, int64(update.Message.Sticker.FileSize), update.Message.Sticker.Thumbnail)
+ this.uploadAsyncComplete(userID, "sticker", conUrl, err, update.Message.Sticker.Emoji+" "+update.Message.Caption)
+ }()
+ }
+
+ // Video
+ if update.Message.Video != nil {
+ go func() {
+ conUrl, err := this.ContentedUploadFallbackSync(update.Message.Video.FileID, int64(update.Message.Video.FileSize), update.Message.Video.Thumbnail)
+ this.uploadAsyncComplete(userID, "video", conUrl, err, update.Message.Caption)
+ }()
+ }
+
+ // VideoNote
+ if update.Message.VideoNote != nil {
+ go func() {
+ conUrl, err := this.ContentedUploadFallbackSync(update.Message.VideoNote.FileID, int64(update.Message.VideoNote.FileSize), update.Message.VideoNote.Thumbnail)
+ this.uploadAsyncComplete(userID, "videonote", conUrl, err, update.Message.Caption)
+ }()
+ }
+
+ // Voice
+ if update.Message.Voice != nil {
+ go func() {
+ conUrl, err := this.ContentedUploadSync(update.Message.Voice.FileID, int64(update.Message.Voice.FileSize)) // no thumbnail fallback available
+ this.uploadAsyncComplete(userID, "voiceclip", conUrl, err, fmt.Sprintf("Voice clip (duration %ds): ", update.Message.Voice.Duration))
+ }()
+ }
+
+ }
+
+ if update.Message.Location != nil {
+ return this.HubSay(userID, fmt.Sprintf(
+ "Latitude %f Longitude %f %s",
+ update.Message.Location.Latitude, update.Message.Location.Longitude, update.Message.Caption))
+ }
+
+ if update.Message.Venue != nil {
+ return this.HubSay(userID, fmt.Sprintf(
+ "Venue %s - Address %s (Lat %f Long %f) %s",
+ update.Message.Venue.Title, update.Message.Venue.Address,
+ update.Message.Venue.Location.Latitude, update.Message.Venue.Location.Longitude,
+ update.Message.Caption))
}
if len(update.Message.Text) > 0 {
diff --git a/contented.go b/contented.go
index 0807df3..f8a8cdb 100644
--- a/contented.go
+++ b/contented.go
@@ -5,12 +5,14 @@ import (
"encoding/json"
"errors"
"fmt"
+ "html"
"io"
"log"
"mime"
"mime/multipart"
"net/http"
"net/textproto"
+ "strings"
"time"
telegram "github.com/go-telegram-bot-api/telegram-bot-api"
@@ -21,24 +23,45 @@ func (this *NTFServer) ContentedEnabled() bool {
return this.contentedMaxBytes > 0
}
-func (this *NTFServer) ContentedUploadFromSync(main *telegram.File, thumbs *[]telegram.PhotoSize) (string, error) {
-
- // If file fits under size limit, take it
- if int64(main.FileSize) < this.contentedMaxBytes {
- return this.ContentedUploadSync(main.FileID, int64(main.FileSize))
+func (this *NTFServer) uploadAsyncComplete(userID int64, typeName string, conUrl string, err error, textPrefix string) {
+ if err != nil {
+ log.Printf("Upload failed for %s: %s", typeName, err.Error())
+ this.callOnMainThread <- func() {
+ this.GroupChatSayHTML("Can't upload " + typeName + " for native users: " + html.EscapeString(err.Error()) + "")
+ }
+ return
}
- // Otherwise, we'll settle for the highest-res thumbnail we can take
-
- if thumbs == nil || len(*thumbs) == 0 {
- return "", errors.New("The file was too large for the image host server (and no smaller thumbnail is available)")
+ // Upload success
+ if len(textPrefix) > 0 {
+ textPrefix = strings.Trim(textPrefix, ` `) + " "
}
- //
+ this.callOnMainThread <- func() {
+ // n.b. this will fail if the user has disconnected by the time the upload completed
+ this.HubSay(userID, textPrefix+conUrl)
+ }
+
+}
+
+func (this *NTFServer) ContentedUploadFallbackSync(FileID string, FileSize int64, thumb *telegram.PhotoSize) (string, error) {
+ if FileSize < this.contentedMaxBytes {
+ return this.ContentedUploadSync(FileID, FileSize)
+
+ } else if thumb != nil && int64(thumb.FileSize) < this.contentedMaxBytes {
+ return this.ContentedUploadSync(thumb.FileID, int64(thumb.FileSize))
+
+ } else {
+ return "", errors.New("File too big and/or bad thumbnail")
+
+ }
+}
+
+func (this *NTFServer) ContentedUploadBestSync(thumbs []telegram.PhotoSize) (string, error) {
bestKnownIdx := -1
bestKnownMpx := int64(-1)
- for idx, img := range *thumbs {
+ for idx, img := range thumbs {
if int64(img.FileSize) > this.contentedMaxBytes {
continue // no good
}
@@ -52,10 +75,10 @@ func (this *NTFServer) ContentedUploadFromSync(main *telegram.File, thumbs *[]te
}
if bestKnownIdx == -1 {
- return "", errors.New("The file was too large for the image host server (and no smaller thumbnail is available)")
+ return "", errors.New("The file was too large for the image host server")
}
- return this.ContentedUploadSync((*thumbs)[bestKnownIdx].FileID, int64((*thumbs)[bestKnownIdx].FileSize))
+ return this.ContentedUploadSync(thumbs[bestKnownIdx].FileID, int64(thumbs[bestKnownIdx].FileSize))
}
func (this *NTFServer) ContentedUploadSync(fileId string, expectSizeBytes int64) (string, error) {
diff --git a/upload.go b/upload.go
deleted file mode 100644
index 1b27cc3..0000000
--- a/upload.go
+++ /dev/null
@@ -1,43 +0,0 @@
-package main
-
-import (
- "errors"
- "html"
- "log"
-
- telegram "github.com/go-telegram-bot-api/telegram-bot-api"
-)
-
-func (this *NTFServer) uploadStickerWorker(userID int64, s *telegram.Sticker) {
- var conUrl string
- var err error
-
- if int64(s.FileSize) < this.contentedMaxBytes {
- conUrl, err = this.ContentedUploadSync(s.FileID, int64(s.FileSize))
-
- } else if s.Thumbnail != nil && int64(s.Thumbnail.FileSize) < this.contentedMaxBytes {
- conUrl, err = this.ContentedUploadSync(s.Thumbnail.FileID, int64(s.Thumbnail.FileSize))
-
- } else {
- err = errors.New("File too big and/or bad thumbnail")
-
- }
-
- //
-
- if err != nil {
- log.Printf("Sticker upload failed: %s", err.Error())
- this.callOnMainThread <- func() {
- this.GroupChatSayHTML("Can't upload sticker for native users: " + html.EscapeString(err.Error()) + "")
- }
- return
- }
-
- // Sticker upload success
- this.callOnMainThread <- func() {
- // n.b. this will fail if the user has disconnected by the time the upload completed
-
- this.HubSay(userID, s.Emoji+" "+conUrl)
- }
-
-}