Preserve uploaded file extensions

This commit is contained in:
richjohnrambo
2026-09-07 16:13:20 +08:00
parent 35da3025a3
commit 8a9e202b56
8 changed files with 139 additions and 14 deletions
+9 -6
View File
@@ -27,6 +27,7 @@ import (
"github.com/tinode/chat/pbx"
"github.com/tinode/chat/server/auth"
"github.com/tinode/chat/server/logs"
"github.com/tinode/chat/server/media"
"github.com/tinode/chat/server/store"
"github.com/tinode/chat/server/store/types"
"google.golang.org/grpc/peer"
@@ -310,15 +311,16 @@ func largeFileReceiveHTTP(wrt http.ResponseWriter, req *http.Request) {
return
}
mimeType := http.DetectContentType(buff)
detectedMimeType := http.DetectContentType(buff)
clientContentType := ""
// If DetectContentType fails, see if client-provided content type can be used.
if mimeType == "application/octet-stream" {
if detectedMimeType == "application/octet-stream" {
if userContentType, params, err := mime.ParseMediaType(header.Header.Get("Content-Type")); err == nil {
// Make sure the content-type is legit.
for _, allowed := range allowedMimeTypes {
if strings.HasPrefix(userContentType, allowed) {
if userContentType = mime.FormatMediaType(userContentType, params); userContentType != "" {
mimeType = userContentType
clientContentType = userContentType
}
break
}
@@ -330,9 +332,10 @@ func largeFileReceiveHTTP(wrt http.ResponseWriter, req *http.Request) {
ObjHeader: types.ObjHeader{
Id: store.Store.GetUidString(),
},
TenantID: principal.TenantID,
User: principal.UID.String(),
MimeType: mimeType,
TenantID: principal.TenantID,
User: principal.UID.String(),
MimeType: media.MimeTypeForUpload(detectedMimeType, clientContentType, header.Filename),
OriginalName: header.Filename,
}
fdef.InitTimes()