<s> from __future__ import unicode_literals <EOL> import contextlib <EOL> import hashlib <EOL> import json <EOL> import os <EOL> from django . core . files . uploadedfile import UploadedFile <EOL> from django . http import HttpResponse , HttpResponseServerError <EOL> from django . utils import six <EOL> from django . utils . encoding import force_bytes , smart_str <EOL> from . models import FileModel <EOL> from . tests import UNICODE_FILENAME , UPLOAD_TO <EOL> from . uploadhandler import ErroringUploadHandler , QuotaUploadHandler <EOL> def file_upload_view ( request ) : <EOL> """<STR_LIT>""" <EOL> form_data = request . POST . copy ( ) <EOL> form_data . update ( request . FILES ) <EOL> if isinstance ( form_data . get ( '<STR_LIT>' ) , UploadedFile ) and isinstance ( form_data [ '<STR_LIT:name>' ] , six . text_type ) : <EOL> if os . path . dirname ( form_data [ '<STR_LIT>' ] . name ) != '<STR_LIT>' : <EOL> return HttpResponseServerError ( ) <EOL> return HttpResponse ( '<STR_LIT>' ) <EOL> else : <EOL> return HttpResponseServerError ( ) <EOL> def file_upload_view_verify ( request ) : <EOL> """<STR_LIT>""" <EOL> form_data = request . POST . copy ( ) <EOL> form_data . update ( request . FILES ) <EOL> for key , value in form_data . items ( ) : <EOL> if key . endswith ( '<STR_LIT>' ) : <EOL> continue <EOL> if key + '<STR_LIT>' not in form_data : <EOL> continue <EOL> submitted_hash = form_data [ key + '<STR_LIT>' ] <EOL> if isinstance ( value , UploadedFile ) : <EOL> new_hash = hashlib . sha1 ( value . read ( ) ) . hexdigest ( ) <EOL> else : <EOL> new_hash = hashlib . sha1 ( force_bytes ( value ) ) . hexdigest ( ) <EOL> if new_hash != submitted_hash : <EOL> return HttpResponseServerError ( ) <EOL> largefile = request . FILES [ '<STR_LIT>' ] <EOL> obj = FileModel ( ) <EOL> obj . testfile . save ( largefile . name , largefile ) <EOL> return HttpResponse ( '<STR_LIT>' ) <EOL> def file_upload_unicode_name ( request ) : <EOL> if not request . FILES [ '<STR_LIT>' ] . name . endswith ( UNICODE_FILENAME ) : <EOL> return HttpResponseServerError ( ) <EOL> response = None <EOL> uni_named_file = request . FILES [ '<STR_LIT>' ] <EOL> obj = FileModel . objects . create ( testfile = uni_named_file ) <EOL> full_name = '<STR_LIT>' % ( UPLOAD_TO , uni_named_file . name ) <EOL> if not os . path . exists ( full_name ) : <EOL> response = HttpResponseServerError ( ) <EOL> obj . delete ( ) <EOL> os . unlink ( full_name ) <EOL> if response : <EOL> return response <EOL> else : <EOL> return HttpResponse ( '<STR_LIT>' ) <EOL> def file_upload_echo ( request ) : <EOL> """<STR_LIT>""" <EOL> r = { k : f . name for k , f in request . FILES . items ( ) } <EOL> return HttpResponse ( json . dumps ( r ) ) <EOL> def file_upload_echo_content ( request ) : <EOL> """<STR_LIT>""" <EOL> def read_and_close ( f ) : <EOL> with contextlib . closing ( f ) : <EOL> return f . read ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> r = { k : read_and_close ( f ) for k , f in request . FILES . items ( ) } <EOL> return HttpResponse ( json . dumps ( r ) ) <EOL> def file_upload_quota ( request ) : <EOL> """<STR_LIT>""" <EOL> request . upload_handlers . insert ( <NUM_LIT:0> , QuotaUploadHandler ( ) ) <EOL> return file_upload_echo ( request ) <EOL> def file_upload_quota_broken ( request ) : <EOL> """<STR_LIT>""" <EOL> response = file_upload_echo ( request ) <EOL> request . upload_handlers . insert ( <NUM_LIT:0> , QuotaUploadHandler ( ) ) <EOL> return response <EOL> def file_upload_getlist_count ( request ) : <EOL> """<STR_LIT>""" <EOL> file_counts = { } <EOL> for key in request . FILES . keys ( ) : <EOL> file_counts [ key ] = len ( request . FILES . getlist ( key ) ) <EOL> return HttpResponse ( json . dumps ( file_counts ) ) <EOL> def file_upload_errors ( request ) : <EOL> request . upload_handlers . insert ( <NUM_LIT:0> , ErroringUploadHandler ( ) ) <EOL> return file_upload_echo ( request ) <EOL> def file_upload_filename_case_view ( request ) : <EOL> """<STR_LIT>""" <EOL> file = request . FILES [ '<STR_LIT>' ] <EOL> obj = FileModel ( ) <EOL> obj . testfile . save ( file . name , file ) <EOL> return HttpResponse ( '<STR_LIT>' % obj . pk ) <EOL> def file_upload_content_type_extra ( request ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> for file_name , uploadedfile in request . FILES . items ( ) : <EOL> params [ file_name ] = { <EOL> k : smart_str ( v ) for k , v in uploadedfile . content_type_extra . items ( ) <EOL> } <EOL> return HttpResponse ( json . dumps ( params ) ) <EOL> def file_upload_fd_closing ( request , access ) : <EOL> if access == '<STR_LIT:t>' : <EOL> request . FILES <EOL> return HttpResponse ( '<STR_LIT>' ) </s>
