<s> from __future__ import absolute_import , unicode_literals <EOL> import os <EOL> from django . http . multipartparser import ( <EOL> ChunkIter , <EOL> SkipFile , <EOL> StopFutureHandlers , <EOL> StopUpload , <EOL> exhaust , <EOL> ) <EOL> from django . template . defaultfilters import slugify as slugify_django <EOL> from django . utils . encoding import force_text <EOL> from django . utils . text import get_valid_filename as get_valid_filename_django <EOL> from unidecode import unidecode <EOL> class UploadException ( Exception ) : <EOL> pass <EOL> def handle_upload ( request ) : <EOL> if not request . method == "<STR_LIT:POST>" : <EOL> raise UploadException ( "<STR_LIT>" ) <EOL> if request . is_ajax ( ) : <EOL> is_raw = True <EOL> filename = request . GET . get ( '<STR_LIT>' , False ) or request . GET . get ( '<STR_LIT:filename>' , False ) or '<STR_LIT>' <EOL> try : <EOL> content_length = int ( request . META [ '<STR_LIT>' ] ) <EOL> except ( IndexError , TypeError , ValueError ) : <EOL> content_length = None <EOL> if content_length < <NUM_LIT:0> : <EOL> raise UploadException ( "<STR_LIT>" % content_length ) <EOL> upload_handlers = request . upload_handlers <EOL> for handler in upload_handlers : <EOL> handler . handle_raw_input ( request , <EOL> request . META , <EOL> content_length , <EOL> None , <EOL> None ) <EOL> pass <EOL> possible_sizes = [ x . chunk_size for x in upload_handlers if x . chunk_size ] <EOL> chunk_size = min ( [ <NUM_LIT:2> ** <NUM_LIT> - <NUM_LIT:4> ] + possible_sizes ) <EOL> stream = ChunkIter ( request , chunk_size ) <EOL> counters = [ <NUM_LIT:0> ] * len ( upload_handlers ) <EOL> try : <EOL> for handler in upload_handlers : <EOL> try : <EOL> handler . new_file ( None , filename , <EOL> None , content_length , None ) <EOL> except StopFutureHandlers : <EOL> break <EOL> for chunk in stream : <EOL> for i , handler in enumerate ( upload_handlers ) : <EOL> chunk_length = len ( chunk ) <EOL> chunk = handler . receive_data_chunk ( chunk , <EOL> counters [ i ] ) <EOL> counters [ i ] += chunk_length <EOL> if chunk is None : <EOL> break <EOL> except SkipFile : <EOL> exhaust ( stream ) <EOL> except StopUpload as e : <EOL> if not e . connection_reset : <EOL> exhaust ( request ) <EOL> else : <EOL> exhaust ( request ) <EOL> for handler in upload_handlers : <EOL> retval = handler . upload_complete ( ) <EOL> if retval : <EOL> break <EOL> for i , handler in enumerate ( upload_handlers ) : <EOL> file_obj = handler . file_complete ( counters [ i ] ) <EOL> if file_obj : <EOL> upload = file_obj <EOL> break <EOL> else : <EOL> if len ( request . FILES ) == <NUM_LIT:1> : <EOL> upload , filename , is_raw = handle_request_files_upload ( request ) <EOL> else : <EOL> raise UploadException ( "<STR_LIT>" ) <EOL> return upload , filename , is_raw <EOL> def handle_request_files_upload ( request ) : <EOL> """<STR_LIT>""" <EOL> is_raw = False <EOL> upload = list ( request . FILES . values ( ) ) [ <NUM_LIT:0> ] <EOL> filename = upload . name <EOL> return upload , filename , is_raw <EOL> def slugify ( string ) : <EOL> return slugify_django ( unidecode ( force_text ( string ) ) ) <EOL> def get_valid_filename ( s ) : <EOL> """<STR_LIT>""" <EOL> s = get_valid_filename_django ( s ) <EOL> filename , ext = os . path . splitext ( s ) <EOL> filename = slugify ( filename ) <EOL> ext = slugify ( ext ) <EOL> if ext : <EOL> return "<STR_LIT>" % ( filename , ext ) <EOL> else : <EOL> return "<STR_LIT:%s>" % ( filename , ) </s>
