Skip to content

Commit

Permalink
Consolidate usage of 'valid_chars' in the application.
Browse files Browse the repository at this point in the history
  • Loading branch information
dannon committed Aug 5, 2016
1 parent e219dfe commit f87b08e
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 36 deletions.
5 changes: 2 additions & 3 deletions lib/galaxy/datatypes/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from galaxy.datatypes.metadata import MetadataElement, MetadataParameter, ListParameter, DictParameter
from galaxy.datatypes import metadata
from galaxy.util import nice_size, sqlite, which
from galaxy.util import nice_size, sqlite, which, FILENAME_VALID_CHARS
from . import data, dataproviders


Expand Down Expand Up @@ -78,8 +78,7 @@ def display_data(self, trans, dataset, preview=False, filename=None, to_ext=None
trans.log_event( "Display dataset id: %s" % str( dataset.id ) )
trans.response.headers['Content-Length'] = int( os.stat( dataset.file_name ).st_size )
to_ext = dataset.extension
valid_chars = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
fname = ''.join(c in valid_chars and c or '_' for c in dataset.name)[0:150]
fname = ''.join(c in FILENAME_VALID_CHARS and c or '_' for c in dataset.name)[0:150]
trans.response.set_content_type( "application/octet-stream" ) # force octet-stream so Safari doesn't append mime extensions to filename
trans.response.headers["Content-Disposition"] = 'attachment; filename="Galaxy%s-[%s].%s"' % (dataset.hid, fname, to_ext)
return open( dataset.file_name )
Expand Down
10 changes: 4 additions & 6 deletions lib/galaxy/datatypes/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from galaxy import util
from galaxy.datatypes.metadata import MetadataElement # import directly to maintain ease of use in Datatype class definitions
from galaxy.util import FILENAME_VALID_CHARS
from galaxy.util import inflector
from galaxy.util import unicodify
from galaxy.util.bunch import Bunch
Expand Down Expand Up @@ -236,9 +237,8 @@ def _archive_main_file(self, archive, display_name, data_filename):
def _archive_composite_dataset( self, trans, data=None, **kwd ):
# save a composite object into a compressed archive for downloading
params = util.Params( kwd )
valid_chars = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
outfname = data.name[0:150]
outfname = ''.join(c in valid_chars and c or '_' for c in outfname)
outfname = ''.join(c in FILENAME_VALID_CHARS and c or '_' for c in outfname)
if params.do_action is None:
params.do_action = 'zip' # default
msg = util.restore_text( params.get( 'msg', '' ) )
Expand Down Expand Up @@ -308,8 +308,7 @@ def _archive_composite_dataset( self, trans, data=None, **kwd ):

def _serve_raw(self, trans, dataset, to_ext):
trans.response.headers['Content-Length'] = int( os.stat( dataset.file_name ).st_size )
valid_chars = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
fname = ''.join(c in valid_chars and c or '_' for c in dataset.name)[0:150]
fname = ''.join(c in FILENAME_VALID_CHARS and c or '_' for c in dataset.name)[0:150]
trans.response.set_content_type( "application/octet-stream" ) # force octet-stream so Safari doesn't append mime extensions to filename
trans.response.headers["Content-Disposition"] = 'attachment; filename="Galaxy%s-[%s].%s"' % (dataset.hid, fname, to_ext)
return open( dataset.file_name )
Expand Down Expand Up @@ -358,8 +357,7 @@ def display_data(self, trans, data, preview=False, filename=None, to_ext=None, *
trans.response.headers['Content-Length'] = int( os.stat( data.file_name ).st_size )
if not to_ext:
to_ext = data.extension
valid_chars = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
fname = ''.join(c in valid_chars and c or '_' for c in data.name)[0:150]
fname = ''.join(c in FILENAME_VALID_CHARS and c or '_' for c in data.name)[0:150]
trans.response.set_content_type( "application/octet-stream" ) # force octet-stream so Safari doesn't append mime extensions to filename
trans.response.headers["Content-Disposition"] = 'attachment; filename="Galaxy%s-[%s].%s"' % (data.hid, fname, to_ext)
return open( data.file_name )
Expand Down
7 changes: 4 additions & 3 deletions lib/galaxy/datatypes/neo4j.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import sys
import shutil
import os
from galaxy.datatypes.images import Html

from galaxy.datatypes.data import Data
from galaxy.datatypes.images import Html
from galaxy.util import FILENAME_VALID_CHARS

gal_Log = logging.getLogger(__name__)
verbose = True
Expand Down Expand Up @@ -69,8 +71,7 @@ def display_data(self, trans, data, preview=False, filename=None,
dir_name = str(os.path.dirname(trans.app.object_store.get_filename(data.dataset))) + neo4j_dir_name

# generate unique filename for this dataset
valid_chars = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
fname = ''.join(c in valid_chars and c or '_' for c in data.name)[0:150]
fname = ''.join(c in FILENAME_VALID_CHARS and c or '_' for c in data.name)[0:150]

# zip the target directory (dir_name) using the fname
shutil.make_archive(fname, 'zip', dir_name)
Expand Down
7 changes: 4 additions & 3 deletions lib/galaxy/tools/imp_exp/export_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
import tarfile
from json import dumps, loads

from galaxy.util import FILENAME_VALID_CHARS


def get_dataset_filename( name, ext, hid ):
"""
Builds a filename for a dataset using its name an extension.
"""
valid_chars = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
base = ''.join( c in valid_chars and c or '_' for c in name )
base = ''.join( c in FILENAME_VALID_CHARS and c or '_' for c in name )
return base + "_%s.%s" % (hid, ext)


Expand Down Expand Up @@ -67,7 +68,7 @@ def create_archive( history_attrs_file, datasets_attrs_file, jobs_attrs_file, ou
dataset_extra_files_path = 'datasets/extra_files_path_%s' % dataset_hid
for fname in file_list:
history_archive.add( os.path.join( extra_files_path, fname ),
arcname=( os.path.join( dataset_extra_files_path, fname ) ) )
arcname=( os.path.join( dataset_extra_files_path, fname ) ) )
dataset_attrs[ 'extra_files_path' ] = dataset_extra_files_path
else:
dataset_attrs[ 'extra_files_path' ] = ''
Expand Down
1 change: 1 addition & 0 deletions lib/galaxy/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def list_map(f, input):
DEFAULT_ENCODING = os.environ.get('GALAXY_DEFAULT_ENCODING', 'utf-8')
NULL_CHAR = '\000'
BINARY_CHARS = [ NULL_CHAR ]
FILENAME_VALID_CHARS = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'


def remove_protocol_from_url( url ):
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/webapps/galaxy/api/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,7 @@ def get_metadata_file( self, trans, history_content_id, history_id, metadata_fil
try:
hda = self.hda_manager.get_accessible( decoded_content_id, trans.user )
file_ext = hda.metadata.spec.get(metadata_file).get("file_ext", metadata_file)
valid_chars = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
fname = ''.join(c in valid_chars and c or '_' for c in hda.name)[0:150]
fname = ''.join(c in util.FILENAME_VALID_CHARS and c or '_' for c in hda.name)[0:150]
trans.response.headers["Content-Type"] = "application/octet-stream"
trans.response.headers["Content-Disposition"] = 'attachment; filename="Galaxy%s-[%s].%s"' % (hda.hid, fname, file_ext)
return open(hda.metadata.get(metadata_file).file_name)
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/webapps/galaxy/api/lda_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,9 +709,8 @@ def traverse( folder ):
fStat = os.stat( dataset.file_name )
trans.response.set_content_type( ldda.get_mime() )
trans.response.headers[ 'Content-Length' ] = int( fStat.st_size )
valid_chars = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
fname = ldda.name
fname = ''.join( c in valid_chars and c or '_' for c in fname )[ 0:150 ]
fname = ''.join( c in util.FILENAME_VALID_CHARS and c or '_' for c in fname )[ 0:150 ]
trans.response.headers[ "Content-Disposition" ] = 'attachment; filename="%s"' % fname
try:
return open( dataset.file_name )
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/webapps/galaxy/controllers/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ def get_metadata_file(self, trans, hda_id, metadata_name):
if not data or not self._can_access_dataset( trans, data ):
return trans.show_error_message( "You are not allowed to access this dataset" )

valid_chars = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
fname = ''.join(c in valid_chars and c or '_' for c in data.name)[0:150]
fname = ''.join(c in util.FILENAME_VALID_CHARS and c or '_' for c in data.name)[0:150]

file_ext = data.metadata.spec.get(metadata_name).get("file_ext", metadata_name)
trans.response.headers["Content-Type"] = "application/octet-stream"
Expand Down
5 changes: 2 additions & 3 deletions lib/galaxy/webapps/galaxy/controllers/library_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from galaxy import util, web
from galaxy.security import Action
from galaxy.tools.actions import upload_common
from galaxy.util import inflector, unicodify
from galaxy.util import inflector, unicodify, FILENAME_VALID_CHARS
from galaxy.util.streamball import StreamBall
from galaxy.web.base.controller import BaseUIController, UsesFormDefinitionsMixin, UsesExtendedMetadataMixin, UsesLibraryMixinItems
from galaxy.web.form_builder import AddressField, CheckboxField, SelectField, build_select_field
Expand Down Expand Up @@ -1519,9 +1519,8 @@ def download_dataset_from_folder( self, trans, cntrller, id, library_id=None, **
trans.response.set_content_type( ldda.get_mime() )
fStat = os.stat( ldda.file_name )
trans.response.headers[ 'Content-Length' ] = int( fStat.st_size )
valid_chars = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
fname = ldda.name
fname = ''.join( c in valid_chars and c or '_' for c in fname )[ 0:150 ]
fname = ''.join( c in FILENAME_VALID_CHARS and c or '_' for c in fname )[ 0:150 ]
trans.response.headers[ "Content-Disposition" ] = 'attachment; filename="%s"' % fname
try:
return open( ldda.file_name )
Expand Down
5 changes: 2 additions & 3 deletions lib/galaxy/webapps/galaxy/controllers/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from galaxy import web
from galaxy import util
from galaxy.util import listify, Params, string_as_bool
from galaxy.util import listify, Params, string_as_bool, FILENAME_VALID_CHARS

from galaxy.web.base import controller
from galaxy.model.item_attrs import UsesAnnotations
Expand Down Expand Up @@ -217,9 +217,8 @@ def display( self, trans, id=None, hid=None, tofile=None, toext=".txt", encoded_
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]
fname = ''.join(c in FILENAME_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:
Expand Down
5 changes: 2 additions & 3 deletions lib/galaxy/webapps/galaxy/controllers/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from galaxy.managers import workflows
from galaxy.model.item_attrs import UsesItemRatings
from galaxy.model.mapping import desc
from galaxy.util import unicodify
from galaxy.util import unicodify, FILENAME_VALID_CHARS
from galaxy.util.sanitize_html import sanitize_html
from galaxy.web import error, url_for
from galaxy.web.base.controller import BaseUIController, SharableMixin, UsesStoredWorkflowMixin
Expand Down Expand Up @@ -815,9 +815,8 @@ def export_to_file( self, trans, id ):
# This workflow has a tool that's missing from the distribution
trans.response.status = 400
return "Workflow cannot be exported due to missing tools."
valid_chars = '.,^_-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
sname = stored.name
sname = ''.join(c in valid_chars and c or '_' for c in sname)[0:150]
sname = ''.join(c in FILENAME_VALID_CHARS and c or '_' for c in sname)[0:150]
trans.response.headers["Content-Disposition"] = 'attachment; filename="Galaxy-Workflow-%s.ga"' % ( sname )
trans.response.set_content_type( 'application/galaxy-archive' )
return stored_dict
Expand Down
5 changes: 2 additions & 3 deletions tools/genomespace/genomespace_file_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import urllib2
import urlparse

from galaxy.util import FILENAME_VALID_CHARS
from galaxy.datatypes import sniff
from galaxy.datatypes.registry import Registry

Expand Down Expand Up @@ -46,8 +47,6 @@
GENOMESPACE_UNKNOWN_FORMAT_KEY = 'unknown'
GENOMESPACE_FORMAT_IDENTIFIER_UNKNOWN = None

VALID_CHARS = '.-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '


def chunk_write( source_stream, target_stream, source_method="read", target_method="write" ):
source_method = getattr( source_stream, source_method )
Expand Down Expand Up @@ -166,7 +165,7 @@ def download_from_genomespace_file_browser( json_parameter_file, genomespace_sit
metadata_dict = None
original_filename = filename
if output_filename is None:
filename = ''.join( c in VALID_CHARS and c or '-' for c in filename )
filename = ''.join( c in FILENAME_VALID_CHARS and c or '-' for c in filename )
while filename in used_filenames:
filename = "-%s" % filename
used_filenames.append( filename )
Expand Down
5 changes: 2 additions & 3 deletions tools/genomespace/genomespace_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from galaxy.datatypes import sniff
from galaxy.datatypes.registry import Registry
from galaxy.util import FILENAME_VALID_CHARS

GENOMESPACE_API_VERSION_STRING = "v1.0"
GENOMESPACE_SERVER_URL_PROPERTIES = "https://dm.genomespace.org/config/%s/serverurl.properties" % ( GENOMESPACE_API_VERSION_STRING )
Expand Down Expand Up @@ -45,8 +46,6 @@
'gmt': 'gmt',
'gct': 'gct'}

VALID_CHARS = '.-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '


def chunk_write( source_stream, target_stream, source_method="read", target_method="write" ):
source_method = getattr( source_stream, source_method )
Expand Down Expand Up @@ -192,7 +191,7 @@ def download_from_genomespace_importer( username, token, json_parameter_file, ge
# if using tmp file, move the file to the new file path dir to get scooped up later
if using_temp_file:
original_filename = filename
filename = ''.join( c in VALID_CHARS and c or '-' for c in filename )
filename = ''.join( c in FILENAME_VALID_CHARS and c or '-' for c in filename )
while filename in used_filenames:
filename = "-%s" % filename
used_filenames.append( filename )
Expand Down

0 comments on commit f87b08e

Please sign in to comment.