fix: ext issues

This commit is contained in:
Rogee
2024-09-04 19:24:10 +08:00
parent 2925bdc3b8
commit 0b736ee74d
2 changed files with 38 additions and 22 deletions

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"mime" "mime"
"os" "os"
"strings"
"exporter/pkg/errorx" "exporter/pkg/errorx"
@@ -159,6 +160,32 @@ func (t *TClient) savePhoto(ctx context.Context, cfg *DBChannel, photo *tg.Photo
return nil return nil
} }
func (t *TClient) getExtByFilenameAndMimeType(filename, mimeType string) string {
ext := ""
if filename != "" {
ext = filename[strings.LastIndex(filename, "."):]
return ext
}
exts, err := mime.ExtensionsByType(mimeType)
if err != nil {
logger.Error("get extension failed", zap.Error(err), zap.String("mime_type", mimeType))
return ext
}
if len(exts) == 0 {
logger.Warn("no extension found", zap.String("mime_type", mimeType))
switch mimeType {
case "application/rar":
ext = ".rar"
}
} else {
ext = exts[len(exts)-1]
}
return ext
}
// saveDocument // saveDocument
func (t *TClient) saveDocument(ctx context.Context, cfg *DBChannel, doc *tg.Document) (ChannelMessageDocument, error) { func (t *TClient) saveDocument(ctx context.Context, cfg *DBChannel, doc *tg.Document) (ChannelMessageDocument, error) {
location := &tg.InputDocumentFileLocation{ location := &tg.InputDocumentFileLocation{
@@ -166,31 +193,10 @@ func (t *TClient) saveDocument(ctx context.Context, cfg *DBChannel, doc *tg.Docu
AccessHash: doc.GetAccessHash(), AccessHash: doc.GetAccessHash(),
FileReference: doc.GetFileReference(), FileReference: doc.GetFileReference(),
} }
exts, err := mime.ExtensionsByType(doc.GetMimeType())
if err != nil {
logger.Error("get extension failed", zap.Error(err), zap.String("mime_type", doc.GetMimeType()))
return ChannelMessageDocument{}, err
}
if len(exts) == 0 {
logger.Warn("no extension found", zap.String("mime_type", doc.GetMimeType()))
switch doc.GetMimeType() {
case "application/rar":
exts = []string{".rar"}
}
}
ext := exts[len(exts)-1]
saveTo := cfg.Asset(doc.GetID(), ext)
downloader := downloader.NewDownloader()
_, err = downloader.Download(t.Client.API(), location).ToPath(ctx, saveTo)
if err != nil {
os.Remove(saveTo)
logger.Error("download failed", zap.Error(err))
return ChannelMessageDocument{}, err
}
docAttr := doc.GetAttributes() docAttr := doc.GetAttributes()
ext := t.getExtByFilenameAndMimeType("", doc.GetMimeType())
data := ChannelMessageDocument{ data := ChannelMessageDocument{
Ext: ext, Ext: ext,
MimeType: doc.GetMimeType(), MimeType: doc.GetMimeType(),
@@ -201,6 +207,7 @@ func (t *TClient) saveDocument(ctx context.Context, cfg *DBChannel, doc *tg.Docu
switch attr.(type) { switch attr.(type) {
case *tg.DocumentAttributeFilename: case *tg.DocumentAttributeFilename:
data.Filename = attr.(*tg.DocumentAttributeFilename).GetFileName() data.Filename = attr.(*tg.DocumentAttributeFilename).GetFileName()
data.Ext = t.getExtByFilenameAndMimeType("", doc.GetMimeType())
case *tg.DocumentAttributeVideo: case *tg.DocumentAttributeVideo:
m := attr.(*tg.DocumentAttributeVideo) m := attr.(*tg.DocumentAttributeVideo)
data.Video = &ChannelMessageDocumentVideo{ data.Video = &ChannelMessageDocumentVideo{
@@ -213,6 +220,14 @@ func (t *TClient) saveDocument(ctx context.Context, cfg *DBChannel, doc *tg.Docu
} }
} }
saveTo := cfg.Asset(doc.GetID(), ext)
downloader := downloader.NewDownloader()
_, err := downloader.Download(t.Client.API(), location).ToPath(ctx, saveTo)
if err != nil {
os.Remove(saveTo)
logger.Error("download failed", zap.Error(err))
return ChannelMessageDocument{}, err
}
logger.Info("download document success", zap.String("location", saveTo), zap.Any("document", data)) logger.Info("download document success", zap.String("location", saveTo), zap.Any("document", data))
return data, nil return data, nil
} }

View File

@@ -15,6 +15,7 @@ func Test_Mime(t *testing.T) {
// Test code here // Test code here
m := "application/rar" m := "application/rar"
m = "video/mp4" m = "video/mp4"
m = "video/quicktime"
t.Log(mime.ExtensionsByType(m)) t.Log(mime.ExtensionsByType(m))
} }