def mk_filename(pattern, response, sock, url, overwrite=False): name = get_filename(sock, url) if not name: if isinstance(response.fileid, bytes): name = response.fileid.decode("ascii", "ignore") else: name = response.fileid fs_encoding = sys.getfilesystemencoding() if fs_encoding is None: fs_encoding = "ascii" name = slugify(name) if not name: name = "file" fname = pattern.format(file=name, **dict(response)) if not overwrite and os.path.exists(fname): fname = replacement_filename(fname) dir_ = os.path.abspath(os.path.dirname(fname)) if not os.path.exists(dir_): os.makedirs(dir_) return fname
def mk_filename(pattern, response, sock, url, overwrite=False): name = get_filename(sock, url) if not name: if not isinstance(response.fileid, unicode): name = unicode(response.fileid, "ascii", "ignore") else: name = response.fileid fs_encoding = sys.getfilesystemencoding() if fs_encoding is None: fs_encoding = "ascii" name = slugify(name).encode(fs_encoding, "ignore") if not name: name = "file" fname = pattern.format(file=name, **dict(response)) if not overwrite and os.path.exists(fname): fname = replacement_filename(fname) dir_ = os.path.abspath(os.path.dirname(fname)) if not os.path.exists(dir_): os.makedirs(dir_) return fname
def mk_filename(pattern, queryresponse, resp, url): name = None url_filename = url.split('/')[-1] if resp: name = resp.headers.get("Content-Disposition", url_filename) if name: name = get_content_disposition(name) if not name: if isinstance(queryresponse.fileid, bytes): name = queryresponse.fileid.decode("ascii", "ignore") else: name = queryresponse.fileid fs_encoding = sys.getfilesystemencoding() if fs_encoding is None: fs_encoding = "ascii" name = slugify(name) if not name: name = "file" fname = pattern.format(file=name, **serialize_object(queryresponse)) return fname
def mk_filename(pattern, queryresponse, resp, url): """ Generate the best possible (or least-worse) filename for a VSO download. * Use the ``content-disposition`` header. * Use `fileid` to generate a file name if content-disposition fails * If everything else fails use the last segment of the URL and hope. """ name = None if resp: cdheader = resp.headers.get("Content-Disposition", None) if cdheader: value, params = cgi.parse_header(cdheader) name = params.get('filename', "") # Work around https://github.com/sunpy/sunpy/issues/3372 if name.count('"') >= 2: name = name.split('"')[1] if name is None: # Advice from the VSO is to fallback to providerid + fileid # As it's possible multiple providers give the same fileid. # However, I haven't implemented this yet as it would be a breaking # change to the filenames we expect. # I don't know if we still need this bytes check in Python 3 only # land, but I don't dare remove it. if isinstance(queryresponse.fileid, bytes): fileid = queryresponse.fileid.decode("ascii", "ignore") else: fileid = queryresponse.fileid # Some providers make fileid a path # Some also don't specify a file extension, but not a lot we can do # about that. name = fileid.split("/")[-1] # If somehow we have got this far with an empty string, fallback to url segment if not name: name = url.split('/')[-1] # Remove any not-filename appropriate characters name = slugify(name) # If absolutely everything else fails make a filename based on download time if not name: name = f"vso_file_{datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')}" fname = pattern.format(file=name, **serialize_object(queryresponse)) return fname
def mk_filename(pattern, queryresponserow, resp, url): """ Generate the best possible (or least-worse) filename for a VSO download. * Use the ``content-disposition`` header. * Use ``fileid`` to generate a file name if content-disposition fails * If everything else fails use the last segment of the URL and hope. """ name = None if resp: cdheader = resp.headers.get("Content-Disposition", None) if cdheader: _, params = cgi.parse_header(cdheader) name = params.get('filename', "") # Work around https://github.com/sunpy/sunpy/issues/3372 if name.count('"') >= 2: name = name.split('"')[1] # This is a hack to to prevent IRIS data from being labelled as XML files if name is None and "VOEvent_IRIS" not in queryresponserow['fileid']: # Advice from the VSO is to fallback to providerid + fileid for a filename # As it's possible multiple providers give the same fileid. # However, I haven't implemented this yet as it would be a breaking # change to the filenames we expect. fileid = queryresponserow['fileid'] # Some providers make fileid a path # Some also don't specify a file extension, but not a lot we can do # about that. name = fileid.split("/")[-1] # If somehow we have got this far with an empty string, fallback to url segment if not name: name = url.split('/')[-1] # Remove any not-filename appropriate characters name = slugify(name) # If absolutely everything else fails make a filename based on download time if not name: name = f"vso_file_{datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')}" fname = pattern.format(file=name, **queryresponserow.response_block_map) return fname
provideritem=list(providers.values())) @staticmethod def mk_filename(pattern, response, sock, url, overwrite=False): name = get_filename(sock, url) if not name: if not isinstance(response.fileid, text_type): name = six.u(response.fileid, "ascii", "ignore") else: name = response.fileid fs_encoding = sys.getfilesystemencoding() if fs_encoding is None: fs_encoding = "ascii" name = slugify(name) if six.PY2: name = name.encode(fs_encoding, "ignore") if not name: name = "file" fname = pattern.format(file=name, **dict(response)) if not overwrite and os.path.exists(fname): fname = replacement_filename(fname) dir_ = os.path.abspath(os.path.dirname(fname)) if not os.path.exists(dir_): os.makedirs(dir_)