1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-16 15:19:06 +00:00

wrap tempfile.mkstemp to work on windows

The path returned by tempfile.mkstemp is encoded in system locale,
since we need to pass it through rpc, we need it to be encoded in UTF-8
This commit is contained in:
lins05
2013-04-25 13:43:09 +08:00
parent 11ec7ed43f
commit aa0dc23b0f
2 changed files with 14 additions and 3 deletions

View File

@@ -7,6 +7,8 @@ import urllib2
import uuid
import logging
import json
import tempfile
import locale
from django.contrib.sites.models import RequestSite
from django.db import IntegrityError
@@ -855,5 +857,15 @@ def redirect_to_login(request):
tup = login_url, redirect_field_name, path
return HttpResponseRedirect('%s?%s=%s' % tup)
def mkstemp():
'''Returns (fd, filepath), the same as tempfile.mkstemp, except the
filepath is encoded in UTF-8
'''
fd, path = tempfile.mkstemp()
system_encoding = locale.getdefaultlocale()[1]
path_utf8 = path.decode(system_encoding).encode('UTF-8')
return fd, path_utf8
# Move to here to avoid circular import.
from base.models import FileContributors, UserStarredFiles, DirFilesLastModifiedInfo, FileLastModifiedInfo

View File

@@ -7,7 +7,6 @@ view_trash_file, view_snapshot_file
import os
import simplejson as json
import stat
import tempfile
import urllib
import urllib2
import chardet
@@ -38,7 +37,7 @@ from seahub.wiki.models import WikiDoesNotExist, WikiPageMissing
from seahub.utils import get_httpserver_root, show_delete_days, render_error, \
get_file_type_and_ext, gen_file_get_url, gen_shared_link, is_file_starred, \
get_file_contributors, get_ccnetapplet_root, render_permission_error, \
is_textual_file, show_delete_days
is_textual_file, show_delete_days, mkstemp
from seahub.utils.file_types import (IMAGE, PDF, IMAGE, DOCUMENT, MARKDOWN, \
TEXT, SF)
from seahub.settings import FILE_ENCODING_LIST, FILE_PREVIEW_MAX_SIZE, \
@@ -570,7 +569,7 @@ def file_edit_submit(request, repo_id):
content = content.encode(encoding)
# first dump the file content to a tmp file, then update the file
fd, tmpfile = tempfile.mkstemp()
fd, tmpfile = mkstemp()
def remove_tmp_file():
try:
os.remove(tmpfile)