[hg] galaxy 2895: Show encoded URL, while still allowing unencod...
details: http://www.bx.psu.edu/hg/galaxy/rev/61684a4d6b9e changeset: 2895:61684a4d6b9e user: Kanwei Li <kanwei@gmail.com> date: Tue Oct 20 15:34:47 2009 -0400 description: Show encoded URL, while still allowing unencoded ids for backward compatibility. Revert to "display" displaying whole file. 6 file(s) affected in this change: lib/galaxy/web/buildapp.py lib/galaxy/web/controllers/dataset.py lib/galaxy/web/controllers/root.py templates/dataset/large_file.mako templates/root/history_common.mako test/base/twilltestcase.py diffs (213 lines): diff -r ea29f33390d3 -r 61684a4d6b9e lib/galaxy/web/buildapp.py --- a/lib/galaxy/web/buildapp.py Tue Oct 20 10:39:30 2009 -0400 +++ b/lib/galaxy/web/buildapp.py Tue Oct 20 15:34:47 2009 -0400 @@ -77,7 +77,7 @@ webapp.add_route( '/async/:tool_id/:data_id/:data_secret', controller='async', action='index', tool_id=None, data_id=None, data_secret=None ) webapp.add_route( '/:controller/:action', action='index' ) webapp.add_route( '/:action', controller='root', action='index' ) - webapp.add_route( '/datasets/:dataset_id/:action/:filename', controller='dataset', action='index', dataset_id=None, filename=None) + webapp.add_route( '/datasets/:encoded_id/:action/:filename', controller='dataset', action='index', encoded_id=None, filename=None) webapp.add_route( '/u/:username/p/:slug', controller='page', action='display_by_username_and_slug' ) webapp.finalize_config() # Wrap the webapp in some useful middleware diff -r ea29f33390d3 -r 61684a4d6b9e lib/galaxy/web/controllers/dataset.py --- a/lib/galaxy/web/controllers/dataset.py Tue Oct 20 10:39:30 2009 -0400 +++ b/lib/galaxy/web/controllers/dataset.py Tue Oct 20 15:34:47 2009 -0400 @@ -199,8 +199,14 @@ return 'This link may not be followed from within Galaxy.' @web.expose - def display(self, trans, dataset_id=None, filename=None, show_all=False, **kwd): + def display(self, trans, encoded_id=None, preview=False, filename=None, to_ext=None, **kwd): """Catches the dataset id and displays file contents as directed""" + + # DEPRECATION: We still support unencoded ids for backward compatibility + try: + dataset_id = int( encoded_id ) + except ValueError: + dataset_id = trans.security.decode_id( encoded_id ) data = trans.app.model.HistoryDatasetAssociation.get( dataset_id ) if not data: raise paste.httpexceptions.HTTPRequestRangeNotSatisfiable( "Invalid reference dataset id: %s." % str( dataset_id ) ) @@ -208,30 +214,47 @@ if trans.app.security_agent.can_access_dataset( roles, data.dataset ): if data.state == trans.model.Dataset.states.UPLOAD: return trans.show_error_message( "Please wait until this dataset finishes uploading before attempting to view it." ) - if filename is None or filename.lower() == "index": - file_path = data.file_name - mime = trans.app.datatypes_registry.get_mimetype_by_extension( data.extension.lower() ) - trans.response.set_content_type(mime) - trans.log_event( "Display dataset id: %s" % str( dataset_id ) ) - - else: + + if filename and filename != "index": file_path = os.path.join( data.extra_files_path, filename ) - mime, encoding = mimetypes.guess_type( file_path ) - if mime is None: - mime = trans.app.datatypes_registry.get_mimetype_by_extension( ".".split( file_path )[-1] ) - trans.response.set_content_type( mime ) + if os.path.exists( file_path ): + mime, encoding = mimetypes.guess_type( file_path ) + if not mime: + try: + mime = trans.app.datatypes_registry.get_mimetype_by_extension( ".".split( file_path )[-1] ) + except: + mime = "txt" - if os.path.exists( file_path ): - max_peek_size = 1000000 # 1 MB - if show_all or os.stat( file_path ).st_size < max_peek_size: + trans.response.set_content_type( mime ) return open( file_path ) else: + return "Could not find '%s' on the extra files path." % filename + + mime = trans.app.datatypes_registry.get_mimetype_by_extension( data.extension.lower() ) + trans.response.set_content_type(mime) + trans.log_event( "Display dataset id: %s" % str( dataset_id ) ) + + if to_ext: # Saving the file + trans.response.headers['Content-Length'] = int( os.stat( data.file_name ).st_size ) + if to_ext[0] != ".": + to_ext = "." + to_ext + valid_chars = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' + fname = data.name + fname = ''.join(c in valid_chars and c or '_' for c in fname)[0:150] + trans.response.headers["Content-Disposition"] = "attachment; filename=GalaxyHistoryItem-%s-[%s]%s" % (data.hid, fname, to_ext) + return open( data.file_name ) + + if os.path.exists( data.file_name ): + max_peek_size = 1000000 # 1 MB + if preview and os.stat( data.file_name ).st_size > max_peek_size: trans.response.set_content_type( "text/html" ) - return trans.fill_template( "/dataset/large_file.mako", - truncated_data = open( file_path ).read(max_peek_size), + return trans.stream_template_mako( "/dataset/large_file.mako", + truncated_data = open( data.file_name ).read(max_peek_size), data = data ) + else: + return open( data.file_name ) else: - raise paste.httpexceptions.HTTPNotFound( "File Not Found (%s)." % ( filename ) ) + raise paste.httpexceptions.HTTPNotFound( "File Not Found (%s)." % data.file_name ) else: return trans.show_error_message( "You are not allowed to access this dataset" ) diff -r ea29f33390d3 -r 61684a4d6b9e lib/galaxy/web/controllers/root.py --- a/lib/galaxy/web/controllers/root.py Tue Oct 20 10:39:30 2009 -0400 +++ b/lib/galaxy/web/controllers/root.py Tue Oct 20 15:34:47 2009 -0400 @@ -139,49 +139,13 @@ @web.expose def display( self, trans, id=None, hid=None, tofile=None, toext=".txt", **kwd ): """ - Returns data directly into the browser. - Sets the mime-type according to the extension + Backward compatibility. """ - if hid is not None: - try: - hid = int( hid ) - except: - return "hid '%s' is invalid" %str( hid ) - history = trans.get_history() - for dataset in history.datasets: - if dataset.hid == hid: - data = dataset - break - else: - raise Exception( "No dataset with hid '%d'" % hid ) + if tofile: + to_ext = toext else: - try: - data = self.app.model.HistoryDatasetAssociation.get( id ) - except: - return "Dataset id '%s' is invalid" %str( id ) - if data: - user, roles = trans.get_user_and_roles() - if trans.app.security_agent.can_access_dataset( roles, data.dataset ): - mime = trans.app.datatypes_registry.get_mimetype_by_extension( data.extension.lower() ) - trans.response.set_content_type(mime) - if tofile: - fStat = os.stat(data.file_name) - trans.response.headers['Content-Length'] = int(fStat.st_size) - if toext[0:1] != ".": - toext = "." + toext - valid_chars = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' - fname = data.name - fname = ''.join(c in valid_chars and c or '_' for c in fname)[0:150] - trans.response.headers["Content-Disposition"] = "attachment; filename=GalaxyHistoryItem-%s-[%s]%s" % (data.hid, fname, toext) - trans.log_event( "Display dataset id: %s" % str(id) ) - try: - return open( data.file_name ) - except: - return "This dataset contains no content" - else: - return "You are not allowed to access this dataset" - else: - return "No dataset with id '%s'" % str( id ) + to_ext = None + return trans.webapp.controllers['dataset'].display( trans, encoded_id=id, to_ext=to_ext ) @web.expose def display_child(self, trans, parent_id=None, designation=None, tofile=None, toext=".txt"): diff -r ea29f33390d3 -r 61684a4d6b9e templates/dataset/large_file.mako --- a/templates/dataset/large_file.mako Tue Oct 20 10:39:30 2009 -0400 +++ b/templates/dataset/large_file.mako Tue Oct 20 15:34:47 2009 -0400 @@ -2,8 +2,8 @@ <div class="warningmessagelarge"> This dataset is large and only the first megabyte is shown below.<br /> - <a href="${h.url_for( controller='dataset', action='display', dataset_id=data.id, show_all=True )}">Show all</a> | - <a href="${h.url_for( controller='root', action='display', id=data.id, tofile='yes', toext=data.ext )}">Save</a> + <a href="${h.url_for( controller='dataset', action='display', encoded_id=trans.security.encode_id( data.id ), filename='' )}">Show all</a> | + <a href="${h.url_for( controller='dataset', action='display', encoded_id=trans.security.encode_id( data.id ), to_ext=data.ext )}">Save</a> </div> <pre> diff -r ea29f33390d3 -r 61684a4d6b9e templates/root/history_common.mako --- a/templates/root/history_common.mako Tue Oct 20 10:39:30 2009 -0400 +++ b/templates/root/history_common.mako Tue Oct 20 15:34:47 2009 -0400 @@ -33,7 +33,7 @@ <img src="${h.url_for('/static/images/pencil_icon_grey.png')}" width='16' height='16' alt='edit attributes' title='edit attributes' class='button edit' border='0'> %endif %else: - <a class="icon-button display" title="display data" href="${h.url_for( controller='dataset', dataset_id=data.id, action='display', filename='index')}" target="galaxy_main"></a> + <a class="icon-button display" title="display data" href="${h.url_for( controller='dataset', action='display', encoded_id=trans.security.encode_id( data.id ), preview=True, filename='' )}" target="galaxy_main"></a> %if user_owns_dataset: <a class="icon-button edit" title="edit attributes" href="${h.url_for( controller='root', action='edit', id=data.id )}" target="galaxy_main"></a> %endif @@ -87,7 +87,7 @@ <div class="info">${_('Info: ')}${data.display_info()}</div> <div> %if data.has_data: - <a href="${h.url_for( controller='root', action='display', id=data.id, tofile='yes', toext=data.ext )}" target="_blank">save</a> + <a href="${h.url_for( controller='dataset', action='display', encoded_id=trans.security.encode_id( data.id ), to_ext=data.ext )}">save</a> %if user_owns_dataset: | <a href="${h.url_for( controller='tool_runner', action='rerun', id=data.id )}" target="galaxy_main">rerun</a> %endif diff -r ea29f33390d3 -r 61684a4d6b9e test/base/twilltestcase.py --- a/test/base/twilltestcase.py Tue Oct 20 10:39:30 2009 -0400 +++ b/test/base/twilltestcase.py Tue Oct 20 15:34:47 2009 -0400 @@ -419,9 +419,9 @@ self.visit_url( "%s/dataset/undelete?id=%s" % ( self.url, hda_id ) ) if check_str: self.check_page_for_string( check_str ) - def display_history_item( self, id, check_str='' ): + def display_history_item( self, hda_id, check_str='' ): """Displays a history item - simulates eye icon click""" - self.visit_url( '%s/datasets/%s/display/index' % ( self.url, id ) ) + self.visit_url( '%s/datasets/%s/display/' % ( self.url, self.security.encode_id( hda_id ) ) ) if check_str: self.check_page_for_string( check_str ) self.home() @@ -554,7 +554,7 @@ def verify_composite_datatype_file_content( self, file_name, hda_id ): local_name = self.get_filename( file_name ) temp_name = self.get_filename( 'temp_%s' % file_name ) - self.visit_url( "%s/datasets/%s/display/%s" % ( self.url, hda_id, file_name ) ) + self.visit_url( "%s/datasets/%s/display/%s" % ( self.url, self.security.encode_id( hda_id ), file_name ) ) data = self.last_page() file( temp_name, 'wb' ).write( data ) try:
participants (1)
-
Greg Von Kuster