<s> import pkg_resources <EOL> import mimetypes <EOL> import colander <EOL> import deform . schema <EOL> from pyramid . httpexceptions import HTTPFound <EOL> from pyramid . response import Response <EOL> from pyramid . security import NO_PERMISSION_REQUIRED <EOL> from . . form import FormView <EOL> from . . file import ( <EOL> FilePropertiesSchema , <EOL> FileUploadTempStore , <EOL> file_upload_widget , <EOL> file_name_node , <EOL> USE_MAGIC , <EOL> ) <EOL> from . . interfaces import ( <EOL> IFile , <EOL> IFolder , <EOL> ) <EOL> from . . sdi import mgmt_view <EOL> @ mgmt_view ( <EOL> context = IFile , <EOL> name = '<STR_LIT>' , <EOL> permission = '<STR_LIT>' , <EOL> tab_condition = False , <EOL> http_cache = <NUM_LIT:0> , <EOL> ) <EOL> def view_file ( context , request ) : <EOL> return context . get_response ( request = request ) <EOL> @ mgmt_view ( <EOL> context = IFile , <EOL> name = '<STR_LIT>' , <EOL> tab_title = '<STR_LIT>' , <EOL> permission = '<STR_LIT>' <EOL> ) <EOL> def view_tab ( context , request ) : <EOL> return HTTPFound ( location = request . sdiapi . mgmt_path ( context ) ) <EOL> class AddFileSchema ( FilePropertiesSchema ) : <EOL> file = colander . SchemaNode ( <EOL> deform . schema . FileData ( ) , <EOL> widget = file_upload_widget , <EOL> missing = colander . null , <EOL> ) <EOL> @ colander . deferred <EOL> def name_or_file ( node , kw ) : <EOL> def _name_or_file ( node , struct ) : <EOL> if not struct [ '<STR_LIT:file>' ] and not struct [ '<STR_LIT:name>' ] : <EOL> raise colander . Invalid ( node , '<STR_LIT>' ) <EOL> if not struct [ '<STR_LIT:name>' ] : <EOL> filename = struct [ '<STR_LIT:file>' ] . get ( '<STR_LIT:filename>' ) <EOL> if filename : <EOL> name_node = file_name_node . bind ( <EOL> context = kw [ '<STR_LIT>' ] , request = kw [ '<STR_LIT>' ] <EOL> ) <EOL> name_node . validator ( node [ '<STR_LIT:file>' ] , filename ) <EOL> else : <EOL> raise colander . Invalid ( <EOL> node , <EOL> '<STR_LIT>' <EOL> ) <EOL> return _name_or_file <EOL> @ mgmt_view ( <EOL> context = IFolder , <EOL> name = '<STR_LIT>' , <EOL> tab_title = '<STR_LIT>' , <EOL> permission = '<STR_LIT>' , <EOL> renderer = '<STR_LIT>' , <EOL> addable_content = '<STR_LIT>' , <EOL> tab_condition = False <EOL> ) <EOL> class AddFileView ( FormView ) : <EOL> title = '<STR_LIT>' <EOL> schema = AddFileSchema ( validator = name_or_file ) . clone ( ) <EOL> schema [ '<STR_LIT:name>' ] . missing = colander . null <EOL> schema [ '<STR_LIT>' ] . missing = colander . null <EOL> buttons = ( '<STR_LIT>' , ) <EOL> def _makeob ( self , stream , title , mimetype ) : <EOL> return self . request . registry . content . create ( <EOL> '<STR_LIT>' , <EOL> stream = stream , <EOL> mimetype = mimetype , <EOL> title = title , <EOL> ) <EOL> def add_success ( self , appstruct ) : <EOL> name = appstruct [ '<STR_LIT:name>' ] <EOL> title = appstruct [ '<STR_LIT:title>' ] or None <EOL> filedata = appstruct [ '<STR_LIT:file>' ] <EOL> mimetype = appstruct [ '<STR_LIT>' ] or USE_MAGIC <EOL> stream = None <EOL> filename = None <EOL> if filedata : <EOL> filename = filedata [ '<STR_LIT:filename>' ] <EOL> stream = filedata [ '<STR_LIT>' ] <EOL> if stream : <EOL> stream . seek ( <NUM_LIT:0> ) <EOL> else : <EOL> stream = None <EOL> name = name or filename <EOL> fileob = self . _makeob ( stream , title , mimetype ) <EOL> self . context [ name ] = fileob <EOL> tmpstore = FileUploadTempStore ( self . request ) <EOL> tmpstore . clear ( ) <EOL> return HTTPFound ( self . request . sdiapi . mgmt_path ( self . context ) ) <EOL> onepixel = pkg_resources . resource_filename ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ mgmt_view ( <EOL> name = '<STR_LIT>' , <EOL> tab_condition = False , <EOL> permission = NO_PERMISSION_REQUIRED <EOL> ) <EOL> def preview_image_upload ( request ) : <EOL> uid = request . subpath [ <NUM_LIT:0> ] <EOL> tempstore = FileUploadTempStore ( request ) <EOL> filedata = tempstore . get ( uid , { } ) <EOL> fp = filedata . get ( '<STR_LIT>' ) <EOL> filename = '<STR_LIT>' <EOL> if fp is not None : <EOL> fp . seek ( <NUM_LIT:0> ) <EOL> filename = filedata [ '<STR_LIT:filename>' ] <EOL> mimetype = mimetypes . guess_type ( filename , strict = False ) [ <NUM_LIT:0> ] <EOL> if not mimetype or not mimetype . startswith ( '<STR_LIT>' ) : <EOL> mimetype = '<STR_LIT>' <EOL> fp = open ( onepixel , '<STR_LIT:rb>' ) <EOL> response = Response ( content_type = mimetype , app_iter = fp ) <EOL> return response </s>
