async def download(self, path, revision=None, range=None, **kwargs): if revision and not revision.endswith(settings.DRIVE_IGNORE_VERSION): metadata = await self.metadata(path, revision=revision) else: metadata = await self.metadata(path) download_resp = await self.make_request( 'GET', metadata.raw.get('downloadUrl') or drive_utils.get_export_link(metadata.raw), range=range, expects=(200, 206), throws=exceptions.DownloadError, ) if metadata.size is not None: return streams.ResponseStreamReader(download_resp, size=metadata.size) # google docs, not drive files, have no way to get the file size # must buffer the entire file into memory stream = streams.StringStream(await download_resp.read()) if download_resp.headers.get('Content-Type'): stream.content_type = download_resp.headers['Content-Type'] stream.name = metadata.export_name return stream
async def download(self, path, revision=None, range=None, **kwargs): if revision and not revision.endswith(settings.DRIVE_IGNORE_VERSION): # Must make additional request to look up download URL for revision async with self.request( 'GET', self.build_url('files', path.identifier, 'revisions', revision, alt='json'), expects=(200, ), throws=exceptions.MetadataError, ) as response: data = await response.json() else: data = await self.metadata(path, raw=True) download_resp = await self.make_request( 'GET', data.get('downloadUrl') or drive_utils.get_export_link(data), range=range, expects=(200, 206), throws=exceptions.DownloadError, ) if 'fileSize' in data: return streams.ResponseStreamReader(download_resp, size=data['fileSize']) # google docs, not drive files, have no way to get the file size # must buffer the entire file into memory stream = streams.StringStream(await download_resp.read()) if download_resp.headers.get('Content-Type'): stream.content_type = download_resp.headers['Content-Type'] if drive_utils.is_docs_file(data): stream.name = path.name + drive_utils.get_download_extension(data) return stream
def download(self, path, revision=None, **kwargs): if revision and not revision.endswith(settings.DRIVE_IGNORE_VERSION): # Must make additional request to look up download URL for revision response = yield from self.make_request( 'GET', self.build_url('files', path.identifier, 'revisions', revision, alt='json'), expects=(200, ), throws=exceptions.MetadataError, ) data = yield from response.json() else: data = yield from self.metadata(path, raw=True) download_resp = yield from self.make_request( 'GET', data.get('downloadUrl') or drive_utils.get_export_link(data['exportLinks']), expects=(200, ), throws=exceptions.DownloadError, ) if 'fileSize' in data: return streams.ResponseStreamReader(download_resp, size=data['fileSize']) # google docs, not drive files, have no way to get the file size # must buffer the entire file into memory stream = streams.StringStream((yield from download_resp.read())) if download_resp.headers.get('Content-Type'): stream.content_type = download_resp.headers['Content-Type'] return stream
def download(self, path, revision=None, range=None, **kwargs): if revision and not revision.endswith(settings.DRIVE_IGNORE_VERSION): # Must make additional request to look up download URL for revision response = yield from self.make_request( "GET", self.build_url("files", path.identifier, "revisions", revision, alt="json"), expects=(200,), throws=exceptions.MetadataError, ) data = yield from response.json() else: data = yield from self.metadata(path, raw=True) download_resp = yield from self.make_request( "GET", data.get("downloadUrl") or drive_utils.get_export_link(data), range=range, expects=(200, 206), throws=exceptions.DownloadError, ) if "fileSize" in data: return streams.ResponseStreamReader(download_resp, size=data["fileSize"]) # google docs, not drive files, have no way to get the file size # must buffer the entire file into memory stream = streams.StringStream((yield from download_resp.read())) if download_resp.headers.get("Content-Type"): stream.content_type = download_resp.headers["Content-Type"] if drive_utils.is_docs_file(data): stream.name = path.name + drive_utils.get_download_extension(data) return stream
def download(self, path, revision=None, **kwargs): data = yield from self.metadata(path, raw=True) if revision and not revision.endswith(settings.DRIVE_IGNORE_VERSION): # Must make additional request to look up download URL for revision response = yield from self.make_request( 'GET', self.build_url('files', data['id'], 'revisions', revision, alt='json'), expects=(200, ), throws=exceptions.MetadataError, ) data = yield from response.json() try: download_url = data['downloadUrl'] except KeyError: download_url = drive_utils.get_export_link(data['exportLinks']) download_resp = yield from self.make_request( 'GET', download_url, expects=(200, ), throws=exceptions.DownloadError, ) return streams.ResponseStreamReader(download_resp)
async def download( self, # type: ignore path: GoogleDrivePath, revision: str = None, range: Tuple[int, int] = None, **kwargs) -> streams.BaseStream: """Download the file at `path`. If `revision` is present, attempt to download that revision of the file. See **Revisions** in the class doctring for an explanation of this provider's revision handling. The actual revision handling is done in `_file_metadata()`. Quirks: Google docs don't have a size until they're exported, so WB must download them, then re-stream them as a StringStream. :param GoogleDrivePath path: the file to download :param str revision: the id of a particular version to download :param tuple(int, int) range: range of bytes to download in this request :rtype: streams.ResponseStreamReader :rtype: streams.StringStream :returns: For GDocs, a StringStream. All others, a ResponseStreamReader. """ metadata = await self.metadata(path, revision=revision) download_resp = await self.make_request( 'GET', metadata.raw.get('downloadUrl') or utils.get_export_link(metadata.raw), # type: ignore range=range, expects=(200, 206), throws=exceptions.DownloadError, ) if metadata.size is not None: # type: ignore return streams.ResponseStreamReader( download_resp, size=metadata.size_as_int) # type: ignore # google docs, not drive files, have no way to get the file size # must buffer the entire file into memory stream = streams.StringStream(await download_resp.read()) if download_resp.headers.get('Content-Type'): # TODO: Add these properties to base class officially, instead of as one-off stream.content_type = download_resp.headers[ 'Content-Type'] # type: ignore stream.name = metadata.export_name # type: ignore return stream
async def download(self, # type: ignore path: GoogleDrivePath, revision: str=None, range: Tuple[int, int]=None, **kwargs) -> streams.BaseStream: """Download the file at `path`. If `revision` is present, attempt to download that revision of the file. See **Revisions** in the class doctring for an explanation of this provider's revision handling. The actual revision handling is done in `_file_metadata()`. Quirks: Google docs don't have a size until they're exported, so WB must download them, then re-stream them as a StringStream. :param GoogleDrivePath path: the file to download :param str revision: the id of a particular version to download :param tuple(int, int) range: range of bytes to download in this request :rtype: streams.ResponseStreamReader :rtype: streams.StringStream :returns: For GDocs, a StringStream. All others, a ResponseStreamReader. """ metadata = await self.metadata(path, revision=revision) download_resp = await self.make_request( 'GET', metadata.raw.get('downloadUrl') or utils.get_export_link(metadata.raw), # type: ignore range=range, expects=(200, 206), throws=exceptions.DownloadError, ) if metadata.size is not None: # type: ignore return streams.ResponseStreamReader(download_resp, size=metadata.size_as_int) # type: ignore # google docs, not drive files, have no way to get the file size # must buffer the entire file into memory stream = streams.StringStream(await download_resp.read()) if download_resp.headers.get('Content-Type'): # TODO: Add these properties to base class officially, instead of as one-off stream.content_type = download_resp.headers['Content-Type'] # type: ignore stream.name = metadata.export_name # type: ignore return stream
def download(self, path, revision=None, **kwargs): if revision and not revision.endswith(settings.DRIVE_IGNORE_VERSION): # Must make additional request to look up download URL for revision response = yield from self.make_request( 'GET', self.build_url('files', path.identifier, 'revisions', revision, alt='json'), expects=(200, ), throws=exceptions.MetadataError, ) data = yield from response.json() else: data = yield from self.metadata(path, raw=True) download_resp = yield from self.make_request( 'GET', data.get('downloadUrl') or drive_utils.get_export_link(data['exportLinks']), expects=(200, ), throws=exceptions.DownloadError, ) size = data.get('fileSize') # google docs, not drive files, have no way to get the file size :( # TODO See what else this breaks return streams.ResponseStreamReader(download_resp, size=size, unsizeable=size is None) # Hope the docs have a content length