<s> """<STR_LIT>""" <EOL> from flask import request , url_for , make_response <EOL> from werkzeug import secure_filename , SharedDataMiddleware <EOL> import os <EOL> import random <EOL> import datetime <EOL> class CKEditor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def gen_rnd_filename ( self ) : <EOL> """<STR_LIT>""" <EOL> filename_prefix = datetime . datetime . now ( ) . strftime ( "<STR_LIT>" ) <EOL> return "<STR_LIT>" % ( filename_prefix , str ( random . randrange ( <NUM_LIT:1000> , <NUM_LIT> ) ) ) <EOL> def upload ( self , name = None , path = None , folder = '<STR_LIT>' , allowed_extensions = None , max_size = None ) : <EOL> """<STR_LIT>""" <EOL> error = '<STR_LIT>' <EOL> url = '<STR_LIT>' <EOL> callback = request . args . get ( "<STR_LIT>" ) <EOL> if request . method == '<STR_LIT:POST>' and '<STR_LIT>' in request . files : <EOL> fileobj = request . files [ '<STR_LIT>' ] <EOL> fname , fext = os . path . splitext ( fileobj . filename ) <EOL> if allowed_extensions and fext not in allowed_extensions : <EOL> error = '<STR_LIT>' % fext <EOL> return error <EOL> rnd_name = '<STR_LIT>' % ( self . gen_rnd_filename ( ) , fext ) <EOL> if not path : <EOL> filepath = os . path . join ( name . static_folder , '<STR_LIT>' , rnd_name ) <EOL> else : <EOL> filepath = path <EOL> app . add_url_rule ( '<STR_LIT>' , '<STR_LIT>' , <EOL> build_only = True ) <EOL> app . wsgi_app = SharedDataMiddleware ( app . wsgi_app , { <EOL> '<STR_LIT>' : path <EOL> } ) <EOL> dirname = os . path . dirname ( filepath ) <EOL> if not os . path . exists ( dirname ) : <EOL> try : <EOL> os . makedirs ( dirname ) <EOL> except : <EOL> error = '<STR_LIT>' % filepath <EOL> elif not os . access ( dirname , os . W_OK ) : <EOL> error = '<STR_LIT>' % filepath <EOL> if not error : <EOL> fileobj . save ( filepath ) <EOL> url = url_for ( '<STR_LIT>' , filename = '<STR_LIT>' % ( '<STR_LIT>' , rnd_name ) ) <EOL> else : <EOL> error = '<STR_LIT>' <EOL> res = """<STR_LIT>""" % ( callback , url , error ) <EOL> response = make_response ( res ) <EOL> response . headers [ "<STR_LIT:Content-Type>" ] = "<STR_LIT>" <EOL> return response </s>
