本文整理汇总了Python中io.BufferedWriter.close方法的典型用法代码示例。如果您正苦于以下问题:Python BufferedWriter.close方法的具体用法?Python BufferedWriter.close怎么用?Python BufferedWriter.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.BufferedWriter
的用法示例。
在下文中一共展示了BufferedWriter.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PANDAUploadBackend
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
class PANDAUploadBackend(AbstractUploadBackend):
"""
Customized backend to handle AJAX uploads.
"""
def update_filename(self, request, filename):
"""
Verify that the filename is unique, if it isn't append and iterate
a counter until it is.
"""
self._original_filename = filename
filename = self._original_filename
root, ext = os.path.splitext(self._original_filename)
path = os.path.join(settings.MEDIA_ROOT, filename)
i = 1
while os.path.exists(path):
filename = '%s%i%s' % (root, i, ext)
path = os.path.join(settings.MEDIA_ROOT, filename)
i += 1
return filename
def setup(self, filename):
"""
Open the destination file for writing.
"""
self._path = os.path.join(settings.MEDIA_ROOT, filename)
try:
os.makedirs(os.path.realpath(os.path.dirname(self._path)))
except:
pass
self._dest = BufferedWriter(FileIO(self._path, "w"))
def upload_chunk(self, chunk):
"""
Write a chunk of data to the destination.
"""
self._dest.write(chunk)
def upload_complete(self, request, filename):
"""
Close the destination file and create an Upload object in the
database recording its existence.
"""
self._dest.close()
root, ext = os.path.splitext(filename)
path = os.path.join(settings.MEDIA_ROOT, filename)
size = os.path.getsize(path)
upload = Upload.objects.create(
filename=filename,
original_filename=self._original_filename,
size=size)
return { 'id': upload.id }
示例2: AbstractUploadBackend
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
class AbstractUploadBackend(object):
BUFFER_SIZE = 10485760 # 10MB
def __init__(self, **kwargs):
self._timedir = get_date_directory()
self.__dict__.update(kwargs)
def update_filename(self, request, filename):
"""Returns a new name for the file being uploaded."""
self.oldname = filename
ext = os.path.splitext(filename)[1]
return md5(filename.encode('utf8')).hexdigest() + ext
def upload_chunk(self, chunk):
"""Called when a string was read from the client, responsible for
writing that string to the destination file."""
self._dest.write(chunk)
def max_size(self):
"""
Checking file max size
"""
if int(self._dest.tell()) > self.upload_size:
self._dest.close()
os.remove(self._path)
return True
def upload(self, uploaded, filename, raw_data):
try:
if raw_data:
# File was uploaded via ajax, and is streaming in.
chunk = uploaded.read(self.BUFFER_SIZE)
while len(chunk) > 0:
self.upload_chunk(chunk)
if self.max_size():
return False
chunk = uploaded.read(self.BUFFER_SIZE)
else:
# File was uploaded via a POST, and is here.
for chunk in uploaded.chunks():
self.upload_chunk(chunk)
if self.max_size():
return False
return True
except:
# things went badly.
return False
def setup(self, filename):
self._path = os.path.join(settings.MEDIA_ROOT, self.upload_dir, self._timedir, filename)
try:
os.makedirs(os.path.realpath(os.path.dirname(self._path)))
except:
pass
self._dest = BufferedWriter(FileIO(self._path, "w"))
def upload_complete(self, request, filename):
path = self.upload_dir + "/" + self._timedir + "/" + filename
self._dest.close()
return {"path": path, 'oldname': self.oldname}
示例3: LocalUploadBackend
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
class LocalUploadBackend(AbstractUploadBackend):
UPLOAD_DIR = ajaxuploader_settings.UPLOAD_DIRECTORY
# TODO: allow this to be overridden per-widget/view
def setup(self, filename):
self._relative_path = os.path.normpath(
os.path.join(
force_unicode(
datetime.datetime.now().strftime( # allow %Y, %s, etc
smart_str(self.UPLOAD_DIR))),
filename))
self._path = os.path.join(settings.MEDIA_ROOT, self._relative_path)
try:
os.makedirs(os.path.realpath(os.path.dirname(self._path)))
except:
pass
self._dest = BufferedWriter(FileIO(self._path, "w"))
def upload_chunk(self, chunk):
self._dest.write(chunk)
def upload_complete(self, request, filename):
self._dest.close()
return {"path": self._relative_path}
def update_filename(self, request, filename):
return ajaxuploader_settings.SANITIZE_FILENAME(filename)
示例4: LocalUploadBackend
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
class LocalUploadBackend(AbstractUploadBackend):
UPLOAD_DIR = getattr(settings, "UPLOAD_DIR", "uploads")
def setup(self, filename, *args, **kwargs):
self._path = os.path.join(
settings.MEDIA_ROOT, self.UPLOAD_DIR, filename)
try:
os.makedirs(os.path.realpath(os.path.dirname(self._path)))
except:
pass
self._dest = BufferedWriter(FileIO(self._path, "w"))
def upload_chunk(self, chunk, *args, **kwargs):
self._dest.write(chunk)
def upload_complete(self, request, filename, *args, **kwargs):
path = settings.MEDIA_URL + self.UPLOAD_DIR + "/" + filename
self._dest.close()
return {"path": path}
def update_filename(self, request, filename, *args, **kwargs):
"""
Returns a new name for the file being uploaded.
Ensure file with name doesn't exist, and if it does,
create a unique filename to avoid overwriting
"""
filename = os.path.basename(filename)
self._dir = os.path.join(
settings.MEDIA_ROOT, self.UPLOAD_DIR)
unique_filename = False
filename_suffix = 0
# Check if file at filename exists
if os.path.isfile(os.path.join(self._dir, filename)):
while not unique_filename:
try:
if filename_suffix == 0:
open(os.path.join(self._dir, filename))
else:
filename_no_extension, extension = os.path.splitext(filename)
open(os.path.join(self._dir, filename_no_extension + str(filename_suffix) + extension))
filename_suffix += 1
except IOError:
unique_filename = True
if filename_suffix == 0:
return filename
else:
return filename_no_extension + str(filename_suffix) + extension
@property
def path(self):
"""
Return a path of file uploaded
"""
return self._path
示例5: chunked_download
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
def chunked_download(node_id: str, file: io.BufferedWriter, **kwargs):
"""Keyword args:
offset: byte offset
length: total length, equal to end - 1
write_callback
"""
ok_codes = [http.PARTIAL_CONTENT]
write_callback = kwargs.get('write_callback', None)
length = kwargs.get('length', 100 * 1024 ** 4)
pgo = progress.Progress()
chunk_start = kwargs.get('offset', 0)
retries = 0
while chunk_start < length:
chunk_end = chunk_start + CHUNK_SIZE - 1
if chunk_end >= length:
chunk_end = length - 1
if retries >= CHUNK_MAX_RETRY:
raise RequestError(RequestError.CODE.FAILED_SUBREQUEST,
'[acd_cli] Downloading chunk failed multiple times.')
r = BackOffRequest.get(get_content_url() + 'nodes/' + node_id + '/content', stream=True,
acc_codes=ok_codes,
headers={'Range': 'bytes=%d-%d' % (chunk_start, chunk_end)})
logger.debug('Range %d-%d' % (chunk_start, chunk_end))
# this should only happen at the end of unknown-length downloads
if r.status_code == http.REQUESTED_RANGE_NOT_SATISFIABLE:
logger.debug('Invalid byte range requested %d-%d' % (chunk_start, chunk_end))
break
if r.status_code not in ok_codes:
r.close()
retries += 1
logging.debug('Chunk [%d-%d], retry %d.' % (retries, chunk_start, chunk_end))
continue
try:
curr_ln = 0
for chunk in r.iter_content(chunk_size=FS_RW_CHUNK_SZ):
if chunk: # filter out keep-alive new chunks
file.write(chunk)
file.flush()
if write_callback:
write_callback(chunk)
curr_ln += len(chunk)
pgo.print_progress(length, curr_ln + chunk_start)
chunk_start += CHUNK_SIZE
retries = 0
r.close()
except (ConnectionError, ReadTimeoutError) as e:
file.close()
raise RequestError(RequestError.CODE.READ_TIMEOUT, '[acd_cli] Timeout. ' + e.__str__())
print() # break progress line
return
示例6: LocalUploadBackend
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
class LocalUploadBackend(AbstractUploadBackend):
UPLOAD_DIR = "uploads"
def setup(self, filename):
self._path = os.path.join(
settings.MEDIA_ROOT, self.UPLOAD_DIR, filename)
try:
os.makedirs(os.path.realpath(os.path.dirname(self._path)))
except:
pass
self._dest = BufferedWriter(FileIO(self._path, "w"))
def upload_chunk(self, chunk):
self._dest.write(chunk)
def upload_complete(self, request, filename):
path = settings.MEDIA_URL + self.UPLOAD_DIR + "/" + filename
self._dest.close()
return {"path": path}
def update_filename(self, request, filename):
"""
Returns a new name for the file being uploaded.
Ensure file with name doesn't exist, and if it does,
create a unique filename to avoid overwriting
"""
self._dir = os.path.join(
settings.MEDIA_ROOT, self.UPLOAD_DIR)
unique_filename = False
filename_suffix = 0
print "orig filename: " + os.path.join(self._dir, filename)
# Check if file at filename exists
if os.path.isfile(os.path.join(self._dir, filename)):
while not unique_filename:
try:
if filename_suffix == 0:
open(os.path.join(self._dir, filename))
else:
filename_no_extension, extension = os.path.splitext(filename)
print "filename all ready exists. Trying " + filename_no_extension + str(filename_suffix) + extension
open(os.path.join(self._dir, filename_no_extension + str(filename_suffix) + extension))
filename_suffix += 1
except IOError:
unique_filename = True
if filename_suffix == 0:
print "using filename: " + os.path.join(self._dir, filename)
return filename
else:
print "using filename: " + filename_no_extension + str(filename_suffix) + extension
return filename_no_extension + str(filename_suffix) + extension
示例7: LocalUploadBackend
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
class LocalUploadBackend(AbstractUploadBackend):
UPLOAD_DIR = "uploads"
def setup(self, filename):
self._path = os.path.join(
settings.MEDIA_ROOT, self.UPLOAD_DIR, filename)
try:
os.makedirs(os.path.realpath(os.path.dirname(self._path)))
except:
pass
self._dest = BufferedWriter(FileIO(self._path, "w"))
def upload_chunk(self, chunk):
self._dest.write(chunk)
def upload_complete(self, request, filename, **kwargs):
path = settings.MEDIA_URL + self.UPLOAD_DIR + "/" + filename
self._dest.close()
return {"path": path}
示例8: LocalUploadBackend
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
class LocalUploadBackend(AbstractUploadBackend):
UPLOAD_DIR = 'tmp'
def update_filename(self, request, filename):
name, ext = os.path.splitext(filename)
return slughifi(name) + ext
def setup(self, filename):
self._path = os.path.join(self.UPLOAD_DIR, filename)
self.path = default_storage.save(self._path, ContentFile(''))
self._abs_path = default_storage.path(self.path)
self._dest = BufferedWriter(FileIO(self._abs_path, "w"))
def upload_chunk(self, chunk):
self._dest.write(chunk)
def upload_complete(self, request, filename):
self._dest.close()
context = {'thumbnail_path': self._path, 'file_name': filename, }
thumbnail = render_to_string('ajaxupload/includes/thumbnail.html', context)
return {"path": self._path, 'thumbnail': thumbnail}
示例9: FileDocumentLocalUploadBackend
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
class FileDocumentLocalUploadBackend(LocalUploadBackend):
def upload_to(self):
d = datetime.datetime.now()
return d.strftime('filedocument/%Y/%m/%d')
def setup(self, filename, *args, **kwargs):
self._path = os.path.join(
settings.MEDIA_ROOT, self.upload_to(), filename)
try:
os.makedirs(os.path.realpath(os.path.dirname(self._path)))
except:
pass
self._dest = BufferedWriter(FileIO(self._path, "w"))
def upload_complete(self, request, filename, *args, **kwargs):
cur_agent = kwargs['cur_agent']
permissions = kwargs['permissions']
relative_path = self.upload_to() + "/" + filename
full_path = settings.MEDIA_URL + relative_path
name = filename
# auto categorize images
image_extensions = ('.jpg', '.jpeg', '.png', '.gif',)
if filename.endswith(image_extensions):
new_item = ImageDocument(name=name, datafile=relative_path)
else:
new_item = FileDocument(name=name, datafile=relative_path)
# link to item
new_item.save_versioned(action_agent=cur_agent, initial_permissions=permissions)
self._dest.close()
return {
"path": full_path,
"url": new_item.get_absolute_url(),
"name": new_item.name,
}
示例10: MyBaseUploadBackend
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
class MyBaseUploadBackend(AbstractUploadBackend):
def __init__(self, dirname, **kwargs):
super(MyBaseUploadBackend, self).__init__(**kwargs)
self.report_id = None
def set_report_id(self, report_id):
self.report_id = report_id
try_number = 1
while True:
try:
self.quast_session = QuastSession.objects.get(report_id=self.report_id)
return True
except QuastSession.DoesNotExist:
logger.error('No quast session with report_id=%s' % self.report_id)
return False
except OperationalError:
logger.error(traceback.format_exc())
try_number += 1
logger.error('Retrying. Try number ' + str(try_number))
def setup(self, filename):
dirpath = self.quast_session.get_contigs_dirpath()
logger.info('filename is %s' % filename)
logger.info('contigs dirpath is %s' % dirpath)
if not os.path.exists(dirpath):
logger.error("contigs directory doesn't exist")
return False
fpath = os.path.join(dirpath, filename)
self._path = fpath
self._dest = BufferedWriter(FileIO(self._path, 'w'))
return True
def upload_chunk(self, chunk):
self._dest.write(chunk)
def upload_complete(self, request, filename):
self._dest.close()
file_index = "%x" % random.getrandbits(128)
c_fn = ContigsFile(fname=filename, file_index=file_index)
c_fn.save()
qc = QuastSession_ContigsFile(contigs_file=c_fn, quast_session=self.quast_session)
qc.save()
logger.info('%s' % filename)
return {
'file_index': file_index,
}
def update_filename(self, request, filename):
dirpath = self.quast_session.get_contigs_dirpath()
logger.info('contigs dirpath is %s' % dirpath)
fpath = os.path.join(dirpath, filename)
logger.info('file path is %s' % fpath)
i = 2
base_fpath = fpath
base_filename = filename
while os.path.isfile(fpath):
fpath = str(base_fpath) + '__' + str(i)
filename = str(base_filename) + '__' + str(i)
i += 1
return filename
def remove(self, request):
if 'fileIndex' not in request.GET:
logger.error('Request.GET must contain "fileIndex"')
return False, 'Request.GET must contain "fileIndex"'
file_index = request.GET['fileIndex']
try:
contigs_file = self.quast_session.contigs_files.get(file_index=file_index)
except ContigsFile.DoesNotExist:
logger.error('No file with such index %d in this quast_session' % file_index)
return False, 'No file with such index'
success, msg = self.__remove(contigs_file)
return success, msg
# if contigs_file.user_session != self.user_session:
# logger.error('This file (%s) belongs to session %s, this session is %s'
# % (fname, str(contigs_file.user_session ), str(self.user_session.session_key)))
# return False, 'This file does not belong to this session'
def __remove(self, contigs_file):
fname = contigs_file.fname
contigs_fpath = os.path.join(self.quast_session.get_contigs_dirpath(), fname)
if os.path.isfile(contigs_fpath):
try:
os.remove(contigs_fpath)
except IOError as e:
logger.error('IOError when removing "%s", fileIndex=%d": %s' % (fname, file_index, e.message))
#.........这里部分代码省略.........
示例11: LocalUploadBackend
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
class LocalUploadBackend(AbstractUploadBackend):
#UPLOAD_DIR = "uploads"
# The below key must be synchronized with the implementing project
# Used to store an array of unclaimed file_pks in the django session
# So they can be claimed later when the anon user authenticates
#SESSION_UNCLAIMED_FILES_KEY = KarmaSettings.SESSION_UNCLAIMED_FILES_KEY
# When a file is uploaded anonymously,
# What username should we assign ownership to?
# This is important because File.save
# behavior will not set awarded_karma to True
# until an owner is assigned who has username != this
#DEFAULT_UPLOADER_USERNAME = KarmaSettings.DEFAULT_UPLOADER_USERNAME
def setup(self, filename):
self._path = os.path.join(
settings.MEDIA_ROOT, filename)
try:
os.makedirs(os.path.realpath(os.path.dirname(self._path)))
except:
pass
self._dest = BufferedWriter(FileIO(self._path, "w"))
def upload_chunk(self, chunk):
self._dest.write(chunk)
def upload(self, uploaded, filename, raw_data):
""" :raw_data: is 0/1 """
try:
if raw_data:
# File was uploaded via ajax, and is streaming in.
chunk = uploaded.read(self.BUFFER_SIZE)
while len(chunk) > 0:
self.upload_chunk(chunk)
chunk = uploaded.read(self.BUFFER_SIZE)
else:
# File was uploaded via a POST, and is here.
for chunk in uploaded.chunks():
self.upload_chunk(chunk)
return True
except:
# things went badly.
return False
def upload_complete(self, request, filename, upload):
path = settings.MEDIA_URL + "/" + filename
self._dest.close()
self._dir = settings.MEDIA_ROOT
# Avoid File.objects.create, as this will try to make
# Another file copy at FileField's 'upload_to' dir
print "creating note"
note = Note()
note.name = filename
note.note_file = os.path.join(self._dir, filename)
note.course_id = request.GET['course_id']
note.draft = True # Pending approval from user
print "saving note"
note.save()
# FIXME: Make get or create
print "setting up session vars"
#import ipdb; ipdb.set_trace()
if 'uploaded_files' in request.session:
request.session['uploaded_files'].append(note.pk)
else:
request.session['uploaded_files'] = [note.pk]
# Asynchronously process document with Google Documents API
print "upload_complete, firing task"
tasks.process_document.delay(note)
return {'note_url': note.get_absolute_url()}
def update_filename(self, request, filename):
"""
Returns a new name for the file being uploaded.
Ensure file with name doesn't exist, and if it does,
create a unique filename to avoid overwriting
"""
self._dir = settings.MEDIA_ROOT
unique_filename = False
filename_suffix = 0
# Check if file at filename exists
if os.path.isfile(os.path.join(self._dir, filename)):
while not unique_filename:
try:
if filename_suffix == 0:
open(os.path.join(self._dir, filename))
else:
filename_no_extension, extension = os.path.splitext(filename)
#print "filename all ready exists. Trying " + filename_no_extension + str(filename_suffix) + extension
open(os.path.join(self._dir, filename_no_extension + str(filename_suffix) + extension))
filename_suffix += 1
except IOError:
unique_filename = True
if filename_suffix == 0:
#.........这里部分代码省略.........
示例12: __init__
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
#.........这里部分代码省略.........
"""Writes a floating point value (4 bytes)"""
self.writer.write(pack('<f', value))
self.written_count += 4
def write_double(self, value):
"""Writes a floating point value (8 bytes)"""
self.writer.write(pack('<d', value))
self.written_count += 8
def write_large_int(self, value, bits, signed=True):
"""Writes a n-bits long integer value"""
self.writer.write(
int.to_bytes(
value, length=bits // 8, byteorder='little', signed=signed))
self.written_count += bits // 8
def write(self, data):
"""Writes the given bytes array"""
self.writer.write(data)
self.written_count += len(data)
# endregion
# region Telegram custom writing
def tgwrite_bytes(self, data):
"""Write bytes by using Telegram guidelines"""
if len(data) < 254:
padding = (len(data) + 1) % 4
if padding != 0:
padding = 4 - padding
self.write(bytes([len(data)]))
self.write(data)
else:
padding = len(data) % 4
if padding != 0:
padding = 4 - padding
self.write(bytes([254]))
self.write(bytes([len(data) % 256]))
self.write(bytes([(len(data) >> 8) % 256]))
self.write(bytes([(len(data) >> 16) % 256]))
self.write(data)
self.write(bytes(padding))
def tgwrite_string(self, string):
"""Write a string by using Telegram guidelines"""
self.tgwrite_bytes(string.encode('utf-8'))
def tgwrite_bool(self, boolean):
"""Write a boolean value by using Telegram guidelines"""
# boolTrue boolFalse
self.write_int(0x997275b5 if boolean else 0xbc799737, signed=False)
def tgwrite_date(self, datetime):
"""Converts a Python datetime object into Unix time (used by Telegram) and writes it"""
value = 0 if datetime is None else int(datetime.timestamp())
self.write_int(value)
def tgwrite_object(self, tlobject):
"""Writes a Telegram object"""
tlobject.on_send(self)
def tgwrite_vector(self, vector):
"""Writes a vector of Telegram objects"""
self.write_int(0x1cb5c415, signed=False) # Vector's constructor ID
self.write_int(len(vector))
for item in vector:
self.tgwrite_object(item)
# endregion
def flush(self):
"""Flush the current stream to "update" changes"""
self.writer.flush()
def close(self):
"""Close the current stream"""
self.writer.close()
def get_bytes(self, flush=True):
"""Get the current bytes array content from the buffer, optionally flushing first"""
if flush:
self.writer.flush()
return self.writer.raw.getvalue()
def get_written_bytes_count(self):
"""Gets the count of bytes written in the buffer.
This may NOT be equal to the stream length if one was provided when initializing the writer"""
return self.written_count
# with block
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
示例13: convert_with_google_drive
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
def convert_with_google_drive(note):
""" Upload a local note and download HTML
using Google Drive
:note: a File model instance # FIXME
"""
# TODO: set the permission of the file to permissive so we can use the
# gdrive_url to serve files directly to users
# Get file_type and encoding of uploaded file
# i.e: file_type = 'text/plain', encoding = None
(file_type, encoding) = mimetypes.guess_type(note.note_file.path)
if file_type != None:
media = MediaFileUpload(note.note_file.path, mimetype=file_type,
chunksize=1024*1024, resumable=True)
else:
media = MediaFileUpload(note.note_file.path,
chunksize=1024*1024, resumable=True)
auth = DriveAuth.objects.filter(email=GOOGLE_USER).all()[0]
creds = auth.transform_to_cred()
creds, auth = check_and_refresh(creds, auth)
service, http = build_api_service(creds)
# get the file extension
filename, extension = os.path.splitext(note.note_file.path)
file_dict = upload_to_gdrive(service, media, filename, extension)
content_dict = download_from_gdrive(file_dict, http, extension)
# Get a new copy of the file from the database with the new metadata from filemeta
new_note = Note.objects.get(id=note.id)
if extension.lower() == '.pdf':
new_note.file_type = 'pdf'
elif extension.lower() in ['.ppt', '.pptx']:
new_note.file_type = 'ppt'
now = datetime.datetime.utcnow()
# create a folder path to store the ppt > pdf file with year and month folders
nonce_path = '/ppt_pdf/%s/%s/' % (now.year, now.month)
_path = filename + '.pdf'
try:
# If those folders don't exist, create them
os.makedirs(os.path.realpath(os.path.dirname(_path)))
except:
print "we failed to create those directories"
_writer = BufferedWriter(FileIO(_path, "w"))
_writer.write(content_dict['pdf'])
_writer.close()
new_note.pdf_file = _path
else:
# PPT files do not have this export ability
new_note.gdrive_url = file_dict[u'exportLinks']['application/vnd.oasis.opendocument.text']
new_note.html = content_dict['html']
new_note.text = content_dict['text']
# before we save new html, sanitize a tags in note.html
#new_note.sanitize_html(save=False)
#FIXME: ^^^ disabled until we can get html out of an Etree html element
# Finally, save whatever data we got back from google
new_note.save()
示例14: LocalUploadBackend
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
class LocalUploadBackend(AbstractUploadBackend):
UPLOAD_DIR = "uploads"
def setup(self, filename, *args, **kwargs):
self._path = os.path.join(
settings.MEDIA_ROOT, self.UPLOAD_DIR, filename)
try:
os.makedirs(os.path.realpath(os.path.dirname(self._path)))
except:
pass
self._dest = BufferedWriter(FileIO(self._path, "w"))
def upload_chunk(self, chunk, *args, **kwargs):
self._dest.write(chunk)
def upload_complete(self, request, filename, *args, **kwargs):
path = settings.MEDIA_URL + self.UPLOAD_DIR + "/" + filename
self._dest.close()
return {"path": path}
def update_filename(self, request, filename, *args, **kwargs):
"""
Returns a new name for the file being uploaded.
Ensure file with name doesn't exist, and if it does,
create a unique filename to avoid overwriting
"""
self._dir = os.path.join(
settings.MEDIA_ROOT, self.UPLOAD_DIR)
unique_filename = False
filename_suffix = 0
# Check if file at filename exists
if os.path.isfile(os.path.join(self._dir, filename)):
while not unique_filename:
try:
if filename_suffix == 0:
open(os.path.join(self._dir, filename))
else:
filename_no_extension, extension = os.path.splitext(filename)
open(os.path.join(self._dir, filename_no_extension + str(filename_suffix) + extension))
filename_suffix += 1
except IOError:
unique_filename = True
if filename_suffix == 0:
return filename
else:
return filename_no_extension + str(filename_suffix) + extension
def resize_for_display(self, filename, width, height):
upload_dir_path = os.path.join(settings.MEDIA_ROOT, self.UPLOAD_DIR) + "/"
original_path = upload_dir_path + filename
filename_no_extension, extension = os.path.splitext(filename)
need_ratio = float(width) / float(height)
im = Image.open(original_path)
real_width, real_height = [float(x) for x in im.size]
real_ratio = real_width / real_height
if real_width > width or real_height > height:
if real_ratio > need_ratio:
displayed_width = width
displayed_height = int(width / real_ratio)
else:
displayed_height = height
displayed_width = int(height * real_ratio)
resized_im = im.resize((displayed_width, displayed_height))
displayed_filename = '%s_displayed%s' % (filename_no_extension, extension)
resized_im.save(upload_dir_path + displayed_filename)
displayed_path = settings.MEDIA_URL + self.UPLOAD_DIR + "/" + displayed_filename
else:
displayed_path = settings.MEDIA_URL + self.UPLOAD_DIR + "/" + filename
return {'displayed_path': displayed_path, 'true_size': im.size}
示例15: UploadStorage
# 需要导入模块: from io import BufferedWriter [as 别名]
# 或者: from io.BufferedWriter import close [as 别名]
class UploadStorage(object):
BUFFER_SIZE = 10485760 # 10MB
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
self.size = None
def set_size(self, width, height):
self.size = int(width), int(height)
#logger.debug(self.size)
def setup(self, filename, upload_to):
""" Creates the filename on the system, along with the required folder structure. """
self.filename = filename
self.upload_to = upload_to
#logger.debug('File: '+self.filename)
self._path = self.update_filename()
#logger.debug('Dir: '+self._dir)
#logger.debug('Path: '+self._path)
#logger.debug(os.path.realpath(os.path.dirname(self._path)))
try:
os.makedirs(os.path.realpath(os.path.dirname(self._path)))
except:
pass
self._dest = BufferedWriter(FileIO(self._path, "w"))
def upload_chunk(self, chunk, *args, **kwargs):
self._dest.write(chunk)
def upload_complete(self):
path = settings.MEDIA_URL + "/" + self.upload_to + "/" + self.filename
self._dest.close()
return {"path": path}
def update_filename(self):
"""
Returns a new name for the file being uploaded.
Ensure file with name doesn't exist, and if it does,
create a unique filename to avoid overwriting
"""
unique_filename = False
filename_suffix = 0
# remove trailing current folder if given
if self.upload_to[:2] == './':
self.upload_to = self.upload_to[2:]
# format upload path with date formats
self.upload_to = time.strftime(self.upload_to)
self._dir = os.path.join(settings.MEDIA_ROOT, self.upload_to)
#logger.debug('Upload to: '+self._dir)
# Check if file at filename exists)
if os.path.isfile(os.path.join(self._dir, self.filename)):
#logger.debug('this file already exists')
while not unique_filename:
try:
if filename_suffix == 0:
open(os.path.join(self._dir, self.filename))
else:
filename_no_extension, extension = os.path.splitext(self.filename)
open(os.path.join(self._dir, "{}_{}{}".format(filename_no_extension, str(filename_suffix), extension)))
filename_suffix += 1
except IOError:
unique_filename = True
if filename_suffix > 0:
self.filename = "{}_{}{}".format(filename_no_extension, str(filename_suffix), extension)
return os.path.join(self._dir, self.filename)
def upload(self, uploaded, raw_data):
try:
if raw_data:
# File was uploaded via ajax, and is streaming in.
chunk = uploaded.read(self.BUFFER_SIZE)
while len(chunk) > 0:
self.upload_chunk(chunk)
chunk = uploaded.read(self.BUFFER_SIZE)
else:
# File was uploaded via a POST, and is here.
for chunk in uploaded.chunks():
self.upload_chunk(chunk)
# make sure the file is closed
self._dest.close()
# file has been uploaded
self.filename = os.path.join(settings.MEDIA_URL, self.upload_to, self.filename)
image = Image.open(self._path)
#logger.debug("{} {} {}".format(image.format, image.size, image.mode))
# resize image
if self.size:
#logger.debug(self.size)
image = image.convert('RGBA')
# the image is resized using the minimum dimension
width, height = self.size
if image.size[0] < image.size[1]:
# the height is bigger than the width
# we set the maximum height to the original height,
# so that the image fits the width
height = image.size[1]
elif image.size[0] > image.size[1]:
#.........这里部分代码省略.........