contented: replace octet-stream mime types with autodetected one, if possible; add timestamp to filenames

This commit is contained in:
mappu 2018-06-09 12:19:27 +12:00
parent 9bab40e4c6
commit d984e3df4c

View File

@ -11,8 +11,10 @@ import (
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"net/textproto" "net/textproto"
"time"
telegram "github.com/go-telegram-bot-api/telegram-bot-api" telegram "github.com/go-telegram-bot-api/telegram-bot-api"
filetype "gopkg.in/h2non/filetype.v1"
) )
func (this *NTFServer) ContentedEnabled() bool { func (this *NTFServer) ContentedEnabled() bool {
@ -79,19 +81,41 @@ func (this *NTFServer) ContentedUploadSync(fileId string, expectSizeBytes int64)
} }
defer resp.Body.Close() defer resp.Body.Close()
// Determine a suitable pseudo-filename from the telegram content-type
fileContentType := resp.Header.Get("Content-Type") // is it blank? who knows?
if this.verbose { if this.verbose {
log.Printf("Downloaded telegram file response headers: %#v\n", resp.Header) log.Printf("Downloaded telegram file response headers: %#v\n", resp.Header)
} }
fileName := "telegram-upload" // Download
fileBuff := bytes.Buffer{}
_, err = io.CopyN(&fileBuff, resp.Body, expectSizeBytes) // CopyN asserts either err!=nil, or copiedBytes == expectSizeBytes. So we're safe
if err != nil {
return "", fmt.Errorf("CopyN: %s", err.Error())
}
// Determine a suitable pseudo-filename from the telegram content-type
// Telegram seems to always supply application/octet-stream
// If Telegram gives us something else, trust it; but otherwise, we can probably do better
fileName := fmt.Sprintf("telegram-upload-%d", time.Now().Unix())
fileContentType := resp.Header.Get("Content-Type") // is it blank? who knows?
if fileContentType == `application/octet-stream` || fileContentType == `` {
// Autodetect something better, if possible
if contentTypeMatches, err := filetype.Match(fileBuff.Bytes()); err == nil {
fileContentType = contentTypeMatches.MIME.Value
fileName += `.` + contentTypeMatches.Extension // no leading period
}
} else {
// We were supplied a good mime type, just need to find a good file extension
if fileExts, err := mime.ExtensionsByType(fileContentType); err == nil && len(fileExts) > 0 { if fileExts, err := mime.ExtensionsByType(fileContentType); err == nil && len(fileExts) > 0 {
fileName += fileExts[0] // Just pick the first one. They all contain a leading period fileName += fileExts[0] // Just pick the first one. They all contain a leading period
} }
}
// Contented uploads are in multipart mime format // Contented uploads are in multipart mime format
uploadBody := bytes.Buffer{} uploadBody := bytes.Buffer{}
formValues := multipart.NewWriter(&uploadBody) formValues := multipart.NewWriter(&uploadBody)
@ -105,10 +129,9 @@ func (this *NTFServer) ContentedUploadSync(fileId string, expectSizeBytes int64)
return "", err return "", err
} }
// Download _, err = io.CopyN(filePart, bytes.NewReader(fileBuff.Bytes()), expectSizeBytes)
_, err = io.CopyN(filePart, resp.Body, expectSizeBytes) // CopyN asserts either err!=nil, or copiedBytes == expectSizeBytes. So we're safe
if err != nil { if err != nil {
return "", fmt.Errorf("CopyN: %s", err.Error()) return "", err
} }
// Upload // Upload