mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 12:30:19 +08:00
feat: fix file name length problem (#1534)
This commit is contained in:
@ -180,6 +180,7 @@ func (c *ApiController) UploadResource() {
|
|||||||
fileType, _ = util.GetOwnerAndNameFromId(mimeType)
|
fileType, _ = util.GetOwnerAndNameFromId(mimeType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fullFilePath = object.GetTruncatedPath(provider, fullFilePath, 175)
|
||||||
if tag != "avatar" && tag != "termsOfUse" {
|
if tag != "avatar" && tag != "termsOfUse" {
|
||||||
ext := filepath.Ext(filepath.Base(fullFilePath))
|
ext := filepath.Ext(filepath.Base(fullFilePath))
|
||||||
index := len(fullFilePath) - len(ext)
|
index := len(fullFilePath) - len(ext)
|
||||||
|
@ -18,6 +18,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/casdoor/casdoor/conf"
|
"github.com/casdoor/casdoor/conf"
|
||||||
@ -54,6 +55,25 @@ func escapePath(path string) string {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetTruncatedPath(provider *Provider, fullFilePath string, limit int) string {
|
||||||
|
pathPrefix := util.UrlJoin(util.GetUrlPath(provider.Domain), provider.PathPrefix)
|
||||||
|
|
||||||
|
dir, file := filepath.Split(fullFilePath)
|
||||||
|
ext := filepath.Ext(file)
|
||||||
|
fileName := strings.TrimSuffix(file, ext)
|
||||||
|
for {
|
||||||
|
escapedString := escapePath(escapePath(fullFilePath))
|
||||||
|
if len(escapedString) < limit-len(pathPrefix) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
rs := []rune(fileName)
|
||||||
|
fileName = string(rs[0 : len(rs)-1])
|
||||||
|
fullFilePath = dir + fileName + ext
|
||||||
|
}
|
||||||
|
|
||||||
|
return fullFilePath
|
||||||
|
}
|
||||||
|
|
||||||
func GetUploadFileUrl(provider *Provider, fullFilePath string, hasTimestamp bool) (string, string) {
|
func GetUploadFileUrl(provider *Provider, fullFilePath string, hasTimestamp bool) (string, string) {
|
||||||
escapedPath := util.UrlJoin(provider.PathPrefix, escapePath(fullFilePath))
|
escapedPath := util.UrlJoin(provider.PathPrefix, escapePath(fullFilePath))
|
||||||
objectKey := util.UrlJoin(util.GetUrlPath(provider.Domain), escapedPath)
|
objectKey := util.UrlJoin(util.GetUrlPath(provider.Domain), escapedPath)
|
||||||
|
Reference in New Issue
Block a user