Exemple #1
0
    async def download(
            self,
            path: FigsharePath,  # type: ignore
            range: Tuple[int, int] = None,
            **kwargs) -> streams.ResponseStreamReader:
        """Download the file identified by ``path`` from this project.

        :param FigsharePath path: FigsharePath to file you want to download
        :rtype streams.ResponseStreamReader:
        """
        if not path.is_file:
            raise exceptions.NotFoundError(str(path))

        file_metadata = await self.metadata(path)
        download_url = file_metadata.extra['downloadUrl']  # type: ignore
        if download_url is None:
            raise exceptions.DownloadError('Download not available',
                                           code=HTTPStatus.FORBIDDEN)

        logger.debug('requested-range:: {}'.format(range))
        params = {} if file_metadata.is_public else {
            'token': self.token
        }  # type: ignore
        resp = await self.make_request(
            'GET',
            download_url,
            range=range,
            params=params,
        )
        if resp.status == 404:
            await resp.release()
            raise exceptions.DownloadError('Download not available',
                                           code=HTTPStatus.FORBIDDEN)

        return streams.ResponseStreamReader(resp)
Exemple #2
0
    async def download(
            self,  # type: ignore
            path: WaterButlerPath,
            revision: str = None,
            range: Tuple[int, int] = None,
            **kwargs) -> streams.ResponseStreamReader:
        if path.identifier is None:
            raise exceptions.DownloadError('"{}" not found'.format(str(path)),
                                           code=404)

        query = {}
        if revision and revision != path.identifier:
            query['version'] = revision

        logger.debug('request-range:: {}'.format(range))
        resp = await self.make_request(
            'GET',
            self.build_url('files', path.identifier, 'content', **query),
            headers={'Accept-Encoding': ''},
            range=range,
            expects=(200, 206),
            throws=exceptions.DownloadError,
        )
        logger.debug('download-headers:: {}'.format([(x, resp.headers[x])
                                                     for x in resp.headers]))

        return streams.ResponseStreamReader(resp)
Exemple #3
0
    def download(self, path, revision=None, **kwargs):
        if not os.path.exists(path.full_path):
            raise exceptions.DownloadError(
                'Could not retrieve file \'{0}\''.format(path),
                code=404,
            )

        file_pointer = open(path.full_path, 'rb')
        return streams.FileStreamReader(file_pointer)
Exemple #4
0
    async def download(self,
                       path,
                       accept_url=False,
                       revision=None,
                       range=None,
                       **kwargs):
        """Returns a ResponseWrapper (Stream) for the specified path
        raises FileNotFoundError if the status from S3 is not 200

        :param str path: Path to the key you want to download
        :param dict \*\*kwargs: Additional arguments that are ignored
        :rtype: :class:`waterbutler.core.streams.ResponseStreamReader`
        :raises: :class:`waterbutler.core.exceptions.DownloadError`
        """

        await self._check_region()

        if not path.is_file:
            raise exceptions.DownloadError('No file specified for download',
                                           code=400)

        if not revision or revision.lower() == 'latest':
            query_parameters = None
        else:
            query_parameters = {'versionId': revision}

        if kwargs.get('displayName'):
            response_headers = {
                'response-content-disposition':
                'attachment; filename*=UTF-8\'\'{}'.format(
                    parse.quote(kwargs['displayName']))
            }
        else:
            response_headers = {'response-content-disposition': 'attachment'}

        url = functools.partial(self.bucket.new_key(path.path).generate_url,
                                settings.TEMP_URL_SECS,
                                query_parameters=query_parameters,
                                response_headers=response_headers)

        if accept_url:
            return url()

        resp = await self.make_request(
            'GET',
            url,
            range=range,
            expects=(
                200,
                206,
            ),
            throws=exceptions.DownloadError,
        )

        return streams.ResponseStreamReader(resp)
Exemple #5
0
    async def download(self, path, **kwargs):
        """Download the file identified by ``path`` from this project.

        :param FigsharePath path: FigsharePath to file you want to download
        :rtype ResponseStreamReader:
        """
        if not path.is_file:
            raise exceptions.NotFoundError(str(path))

        file_metadata = await self.metadata(path)
        download_url = file_metadata.extra['downloadUrl']
        if download_url is None:
            raise exceptions.DownloadError('Download not available', code=HTTPStatus.FORBIDDEN)

        params = {} if file_metadata.is_public else {'token': self.token}
        resp = await aiohttp.request('GET', download_url, params=params)
        if resp.status == 404:
            await resp.release()
            raise exceptions.DownloadError('Download not available', code=HTTPStatus.FORBIDDEN)

        return streams.ResponseStreamReader(resp)
Exemple #6
0
 async def download(
     self,
     path: WaterButlerPath,
     range: Tuple[int, int] = None,  # type: ignore
     **kwargs
 ) -> Union[FileStreamReader, PartialFileStreamReader]:
     if not os.path.exists(path.full_path):
         raise exceptions.DownloadError(
             'Could not retrieve file \'{0}\''.format(path), code=404)
     file_pointer = open(path.full_path, 'rb')
     logger.debug('requested-range:: {}'.format(range))
     if range is not None and range[1] is not None:
         return PartialFileStreamReader(file_pointer, range)
     return FileStreamReader(file_pointer)
Exemple #7
0
    def download(self, path, revision=None, range=None, **kwargs):
        if path.identifier is None:
            raise exceptions.DownloadError('"{}" not found'.format(str(path)), code=404)

        query = {}
        if revision and revision != path.identifier:
            query['version'] = revision

        resp = yield from self.make_request(
            'GET',
            self.build_url('files', path.identifier, 'content', **query),
            range=range,
            expects=(200, 206),
            throws=exceptions.DownloadError,
        )

        return streams.ResponseStreamReader(resp)
Exemple #8
0
    def download(self, path, **kwargs):
        """Download a file. Note: Although Figshare may return a download URL,
        the `accept_url` parameter is ignored here, since Figshare does not
        support HTTPS for downloads.

        :param str path: Path to the key you want to download
        :rtype ResponseWrapper:
        """
        if path.identifier is None:
            raise exceptions.NotFoundError(str(path))

        file_metadata = yield from self.metadata(path)
        download_url = file_metadata.extra['downloadUrl']
        if download_url is None:
            raise exceptions.DownloadError(
                'Cannot download private files',
                code=http.client.FORBIDDEN,
            )
        resp = yield from aiohttp.request('GET', download_url)
        return streams.ResponseStreamReader(resp)
Exemple #9
0
    async def download(
            self,  # type: ignore
            path: OneDrivePath,
            revision: str = None,
            range: typing.Tuple[int, int] = None,
            **kwargs) -> streams.ResponseStreamReader:
        r"""Download the file identified by ``path``.  If ``revision`` is not ``None``, get
        the file at the version identified by ``revision``.

        API docs: https://dev.onedrive.com/items/download.htm

        :param str path: The path to the file on OneDrive
        :param str revision: The revision of the file to download. If ``None``, download latest.
        :param dict \*\*kwargs: Ignored
        :raises: :class:`waterbutler.core.exceptions.DownloadError`
        :rtype: waterbutler.core.streams.ResponseStreamReader
        :return: a stream of the contents of the file
        """
        logger.debug(
            'download path::{} path.identifier::{} revision::{} range::{} '
            'kwargs::{}'.format(path, path.identifier, revision, range,
                                kwargs))

        if path.identifier is None:
            raise exceptions.DownloadError('"{}" not found'.format(str(path)),
                                           code=404)

        download_url = None
        if revision:
            items = await self._revisions_json(path)
            for item in items['value']:
                if item['id'] == revision:
                    try:
                        download_url = item['@content.downloadUrl']
                    except KeyError:
                        raise exceptions.UnexportableFileTypeError(str(path))
                    break
        else:
            # TODO: we should be able to get the download url from validate_v1_path
            metadata_resp = await self.make_request(
                'GET',
                self._build_drive_url(*path.api_identifier),
                expects=(200, ),
                throws=exceptions.MetadataError)
            logger.debug('download metadata_resp::{}'.format(
                repr(metadata_resp)))
            metadata = await metadata_resp.json()
            logger.debug('download metadata::{}'.format(json.dumps(metadata)))

            try:
                package_type = metadata['package']['type']
            except KeyError:
                pass
            else:
                if package_type == 'oneNote':
                    raise exceptions.UnexportableFileTypeError(str(path))

            download_url = metadata.get('@content.downloadUrl', None)

        if download_url is None:
            raise exceptions.NotFoundError(str(path))

        logger.debug('download download_url::{}'.format(download_url))
        download_resp = await self.make_request(
            'GET',
            download_url,
            range=range,
            expects=(200, 206),
            headers={'accept-encoding': ''},
            throws=exceptions.DownloadError,
        )
        logger.debug('download download_resp::{}'.format(repr(download_resp)))

        return streams.ResponseStreamReader(download_resp)