Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix windows encode issue. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 73 additions & 8 deletions elFinder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# coding=UTF-8
#!/usr/bin/env python
#
# Connector for elFinder File Manager
Expand All @@ -10,6 +11,7 @@
import re
import shutil
import time
import sys
from datetime import datetime

class connector():
Expand Down Expand Up @@ -117,6 +119,13 @@ class connector():
def __init__(self, opts):
for opt in opts:
self._options[opt] = opts.get(opt)

# append by Kidwind
# 解决 Windows 平台乱码问题,解决关键在于 os.listdir 根据传入字符串的类型(str或unicode)
# 决定返回目录列表的字符串类型。
self._options['URL'] = self.__toUnicode(self._options['URL'])
self._options['root'] = self.__toUnicode(self._options['root'])
# end append

self._response['debug'] = {}
self._options['URL'] = self.__checkUtf8(self._options['URL'])
Expand Down Expand Up @@ -175,11 +184,11 @@ def run(self, httpRequest = []):
cmd = self._commands[self._request['cmd']]
func = getattr(self, '_' + self.__class__.__name__ + cmd, None)
if callable(func):
try:
#try:
func()
except Exception, e:
self._response['error'] = 'Command Failed'
self.__debug('exception', str(e))
#except Exception, e:
# self._response['error'] = 'Command Failed'
# self.__debug('exception', str(e))
else:
self._response['error'] = 'Unknown command'
else:
Expand Down Expand Up @@ -1057,7 +1066,7 @@ def __archive(self):

curCwd = os.getcwd()
os.chdir(curDir)
self.__runSubProcess(cmd)
self.__runSubProcess(self.__cmdCompatibility(cmd)) # change by Kidwind
os.chdir(curCwd)

if os.path.exists(archivePath):
Expand Down Expand Up @@ -1098,7 +1107,7 @@ def __extract(self):

curCwd = os.getcwd()
os.chdir(curDir)
ret = self.__runSubProcess(cmd)
ret = self.__runSubProcess(self.__cmdCompatibility(cmd)) # change by Kidwind
os.chdir(curCwd)

if ret:
Expand Down Expand Up @@ -1312,7 +1321,7 @@ def __isAllowed(self, path, access):
def __hash(self, path):
"""Hash of the path"""
m = hashlib.md5()
m.update(path)
m.update(self.__unicodeToUtf8(path)) # change by Kidwind 解决 md5 无法处理 unicode 类型的字符。
return str(m.hexdigest())


Expand Down Expand Up @@ -1492,11 +1501,67 @@ def __runSubProcess(self, cmd, validReturn = [0]):


def __checkUtf8(self, name):
# append By Kidwind
# 解决当传入 name 为unicode对象时的错误。
if isinstance(name, unicode):
return name
# end append By Kidwind
try:
name.decode('utf-8')
except UnicodeDecodeError:
name = unicode(name, 'utf-8', 'replace')
self.__debug('invalid encoding', name)
#name += ' (invalid encoding)'
return name


# append by Kidwind
def __toUnicode(self, name):
u'''
if name is not unicode object, use file system encoding convert name to unicode object and return.
'''
if isinstance(name, unicode):
return name
return unicode(name, sys.getfilesystemencoding(), 'replace')

def __unicodeToUtf8(self, name):
u'''
if name is unicode object, convert to utf8, otherwise return original name.
'''
if not isinstance(name, unicode):
return name
return name.encode('utf8')

def __toLocalEncodeStr(self, name):
u'''
if name is unicode object, convert to file system encoding, otherwise return original name.
'''
if not isinstance(name, unicode):
return name
return name.encode(sys.getfilesystemencoding())

def __cmdCompatibility(self, cmd):
u'''
only for windows.
see: http://stackoverflow.com/questions/1910275/unicode-filenames-on-windows-with-python-subprocess-popen
Known Issues:Can not process non-native language's archive and extract.
i don't know how to completely resolved this bug.
'''
if not isWindowsPlatform():
return cmd
newCmd = []
for c in cmd:
newCmd.append(self.__toLocalEncodeStr(c))
return newCmd
# end append

# append by Kidwind
_platform = None
def getPlatform():
global _platform
if _platform is None:
_platform = sys.platform
return _platform

def isWindowsPlatform():
return getPlatform() == 'win32'
# end append