mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2026-10-10 04:53:09 +08:00
fix(archive): prefer utf-8 for non-EFS zip names (#2557)
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/KirCute/zip"
|
||||
"github.com/OpenListTeam/OpenList/v4/internal/archive/tool"
|
||||
@@ -101,6 +102,9 @@ func decodeName(name string, efs bool) string {
|
||||
if efs {
|
||||
return name
|
||||
}
|
||||
if utf8.ValidString(name) {
|
||||
return name
|
||||
}
|
||||
enc, err := ianaindex.IANA.Encoding(setting.GetStr(conf.NonEFSZipEncoding))
|
||||
if err != nil {
|
||||
return name
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package zip
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/OpenListTeam/OpenList/v4/internal/conf"
|
||||
"github.com/OpenListTeam/OpenList/v4/internal/model"
|
||||
"github.com/OpenListTeam/OpenList/v4/internal/op"
|
||||
"github.com/OpenListTeam/OpenList/v4/internal/setting"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func setNonEFSZipEncoding(value string) {
|
||||
op.Cache.SetSetting(conf.NonEFSZipEncoding, &model.SettingItem{
|
||||
Key: conf.NonEFSZipEncoding,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
|
||||
func TestDecodeNamePrefersValidUTF8WhenEFSDisabled(t *testing.T) {
|
||||
setNonEFSZipEncoding("IBM437")
|
||||
|
||||
name := "中文.txt"
|
||||
require.Equal(t, name, decodeName(name, false))
|
||||
// Ensure the setting still exists to verify we did not bypass config due to missing setup.
|
||||
require.Equal(t, "IBM437", setting.GetStr(conf.NonEFSZipEncoding))
|
||||
}
|
||||
|
||||
func TestDecodeNameFallsBackToConfiguredEncoding(t *testing.T) {
|
||||
setNonEFSZipEncoding("GB18030")
|
||||
|
||||
name := string([]byte{0xd6, 0xd0, 0xce, 0xc4, '.', 't', 'x', 't'})
|
||||
require.Equal(t, "中文.txt", decodeName(name, false))
|
||||
}
|
||||
|
||||
func TestDecodeNameRespectsEFSFlag(t *testing.T) {
|
||||
setNonEFSZipEncoding("GB18030")
|
||||
|
||||
name := "utf8-name.txt"
|
||||
require.Equal(t, name, decodeName(name, true))
|
||||
}
|
||||
Reference in New Issue
Block a user