Пример #1
0
    def fetch(self, reply):
        """Download a QNetworkReply to disk.

        Args:
            reply: The QNetworkReply to download.
        """
        _inline, suggested_filename = http.parse_content_disposition(reply)
        log.downloads.debug("fetch: {} -> {}".format(reply.url(),
                                                     suggested_filename))
        download = DownloadItem(reply, self)
        download.finished.connect(
            functools.partial(self.on_finished, download))
        download.data_changed.connect(
            functools.partial(self.on_data_changed, download))
        download.error.connect(self.on_error)
        download.basename = suggested_filename
        self.download_about_to_be_added.emit(len(self.downloads) + 1)
        self.downloads.append(download)
        self.download_added.emit()

        q = usertypes.Question(self)
        q.text = "Save file to:"
        q.mode = usertypes.PromptMode.text
        q.default = suggested_filename
        q.answered.connect(download.set_filename)
        q.cancelled.connect(download.cancel)
        q.completed.connect(q.deleteLater)
        q.destroyed.connect(functools.partial(self.questions.remove, q))
        self.questions.append(q)
        download.cancelled.connect(q.abort)
        objreg.get('message-bridge').ask(q, blocking=False)
Пример #2
0
    def on_unsupported_content(self, reply):
        """Handle an unsupportedContent signal.

        Most likely this will mean we need to download the reply, but we
        correct for some common errors the server do.

        At some point we might want to implement the MIME Sniffing standard
        here: http://mimesniff.spec.whatwg.org/
        """
        inline, _suggested_filename = http.parse_content_disposition(reply)
        if not inline:
            # Content-Disposition: attachment -> force download
            self.start_download.emit(reply)
            return
        mimetype, _rest = http.parse_content_type(reply)
        if mimetype == 'image/jpg':
            # Some servers (e.g. the LinkedIn CDN) send a non-standard
            # image/jpg (instead of image/jpeg, defined in RFC 1341 section
            # 7.5). If this is the case, we force displaying with a corrected
            # mimetype.
            if reply.isFinished():
                self.display_content(reply, 'image/jpeg')
            else:
                reply.finished.connect(
                    functools.partial(self.display_content, reply,
                                      'image/jpeg'))
        else:
            # Unknown mimetype, so download anyways.
            self.start_download.emit(reply)
Пример #3
0
    def fetch(self, reply):
        """Download a QNetworkReply to disk.

        Args:
            reply: The QNetworkReply to download.
        """
        _inline, suggested_filename = http.parse_content_disposition(reply)
        log.downloads.debug("fetch: {} -> {}".format(reply.url(),
                                                     suggested_filename))

        download = DownloadItem(reply, self)
        download.finished.connect(
            functools.partial(self.on_finished, download))
        download.data_changed.connect(
            functools.partial(self.on_data_changed, download))
        download.error.connect(self.on_error)
        download.basename = suggested_filename
        idx = len(self.downloads) + 1
        self.beginInsertRows(QModelIndex(), idx, idx)
        self.downloads.append(download)
        self.endInsertRows()

        q = usertypes.Question(self)
        q.text = "Save file to:"
        q.mode = usertypes.PromptMode.text
        q.default = suggested_filename
        q.answered.connect(download.set_filename)
        q.cancelled.connect(download.cancel)
        q.completed.connect(q.deleteLater)
        q.destroyed.connect(functools.partial(self.questions.remove, q))
        self.questions.append(q)
        download.cancelled.connect(q.abort)
        message_bridge = objreg.get('message-bridge', scope='window',
                                    window=self._win_id)
        message_bridge.ask(q, blocking=False)
Пример #4
0
    def on_unsupported_content(self, reply):
        """Handle an unsupportedContent signal.

        Most likely this will mean we need to download the reply, but we
        correct for some common errors the server do.

        At some point we might want to implement the MIME Sniffing standard
        here: http://mimesniff.spec.whatwg.org/
        """
        inline, _suggested_filename = http.parse_content_disposition(reply)
        if not inline:
            # Content-Disposition: attachment -> force download
            self.start_download.emit(reply)
            return
        mimetype, _rest = http.parse_content_type(reply)
        if mimetype == 'image/jpg':
            # Some servers (e.g. the LinkedIn CDN) send a non-standard
            # image/jpg (instead of image/jpeg, defined in RFC 1341 section
            # 7.5). If this is the case, we force displaying with a corrected
            # mimetype.
            if reply.isFinished():
                self.display_content(reply, 'image/jpeg')
            else:
                reply.finished.connect(functools.partial(
                    self.display_content, reply, 'image/jpeg'))
        else:
            # Unknown mimetype, so download anyways.
            self.start_download.emit(reply)
 def _check_filename(self, header, filename):
     """Check if the passed header has the given filename."""
     reply = stubs.FakeNetworkReply(headers={'Content-Disposition': header})
     cd_inline, cd_filename = http.parse_content_disposition(reply)
     self.assertIsNotNone(cd_filename)
     self.assertEqual(cd_filename, filename)
     self.assertFalse(cd_inline)
Пример #6
0
 def _check_filename(self, header, filename):
     """Check if the passed header has the given filename."""
     reply = stubs.FakeNetworkReply(headers={'Content-Disposition': header})
     cd_inline, cd_filename = http.parse_content_disposition(reply)
     self.assertIsNotNone(cd_filename)
     self.assertEqual(cd_filename, filename)
     self.assertFalse(cd_inline)
Пример #7
0
    def fetch(self, reply):
        """Download a QNetworkReply to disk.

        Args:
            reply: The QNetworkReply to download.
        """
        _inline, suggested_filename = http.parse_content_disposition(reply)
        log.downloads.debug("fetch: {} -> {}".format(reply.url(),
                                                     suggested_filename))

        download = DownloadItem(reply, self)
        download.finished.connect(
            functools.partial(self.on_finished, download))
        download.data_changed.connect(
            functools.partial(self.on_data_changed, download))
        download.error.connect(self.on_error)
        download.basename = suggested_filename
        idx = len(self.downloads) + 1
        self.beginInsertRows(QModelIndex(), idx, idx)
        self.downloads.append(download)
        self.endInsertRows()

        q = usertypes.Question(self)
        q.text = "Save file to:"
        q.mode = usertypes.PromptMode.text
        q.default = suggested_filename
        q.answered.connect(download.set_filename)
        q.cancelled.connect(download.cancel)
        q.completed.connect(q.deleteLater)
        q.destroyed.connect(functools.partial(self.questions.remove, q))
        self.questions.append(q)
        download.cancelled.connect(q.abort)
        message_bridge = objreg.get('message-bridge', scope='window',
                                    window='last-focused')
        message_bridge.ask(q, blocking=False)
Пример #8
0
    def fetch(self, reply, fileobj=None, filename=None):
        """Download a QNetworkReply to disk.

        Args:
            reply: The QNetworkReply to download.
            fileobj: The file object to write the answer to.
            filename: A path to write the data to.

        Return:
            The created DownloadItem.
        """
        if fileobj is not None and filename is not None:
            raise TypeError("Only one of fileobj/filename may be given!")
        if filename is not None:
            suggested_filename = os.path.basename(filename)
        elif fileobj is not None and getattr(fileobj, 'name', None):
            suggested_filename = fileobj.name
        else:
            _inline, suggested_filename = http.parse_content_disposition(reply)
        log.downloads.debug("fetch: {} -> {}".format(reply.url(),
                                                     suggested_filename))
        download = DownloadItem(reply, self)
        download.finished.connect(functools.partial(self.on_finished,
                                                    download))
        download.data_changed.connect(
            functools.partial(self.on_data_changed, download))
        download.error.connect(self.on_error)
        download.redirected.connect(
            functools.partial(self.on_redirect, download))
        download.basename = suggested_filename
        idx = len(self.downloads) + 1
        self.beginInsertRows(QModelIndex(), idx, idx)
        self.downloads.append(download)
        self.endInsertRows()

        if filename is not None:
            download.set_filename(filename)
        elif fileobj is not None:
            download.set_fileobj(fileobj)
            download.autoclose = False
        else:
            q = usertypes.Question(self)
            q.text = "Save file to:"
            q.mode = usertypes.PromptMode.text
            q.default = suggested_filename
            q.answered.connect(download.set_filename)
            q.cancelled.connect(download.cancel)
            q.completed.connect(q.deleteLater)
            q.destroyed.connect(functools.partial(self.questions.remove, q))
            self.questions.append(q)
            download.cancelled.connect(q.abort)
            message_bridge = objreg.get('message-bridge',
                                        scope='window',
                                        window=self._win_id)
            message_bridge.ask(q, blocking=False)

        return download
Пример #9
0
    def test_attonly(self):
        """'attachment' only.

        UA should offer to download the resource.
        """
        reply = stubs.FakeNetworkReply(
            headers={'Content-Disposition': 'attachment'})
        cd_inline, cd_filename = http.parse_content_disposition(reply)
        self.assertFalse(cd_inline)
        self.assertEqual(cd_filename, DEFAULT_NAME)
    def test_attonly(self):
        """'attachment' only.

        UA should offer to download the resource.
        """
        reply = stubs.FakeNetworkReply(
            headers={'Content-Disposition': 'attachment'})
        cd_inline, cd_filename = http.parse_content_disposition(reply)
        self.assertFalse(cd_inline)
        self.assertEqual(cd_filename, DEFAULT_NAME)
Пример #11
0
    def fetch(self, reply, fileobj=None, filename=None):
        """Download a QNetworkReply to disk.

        Args:
            reply: The QNetworkReply to download.
            fileobj: The file object to write the answer to.
            filename: A path to write the data to.

        Return:
            The created DownloadItem.
        """
        if fileobj is not None and filename is not None:
            raise TypeError("Only one of fileobj/filename may be given!")
        if filename is not None:
            suggested_filename = os.path.basename(filename)
        elif fileobj is not None and getattr(fileobj, 'name', None):
            suggested_filename = fileobj.name
        else:
            _inline, suggested_filename = http.parse_content_disposition(reply)
        log.downloads.debug("fetch: {} -> {}".format(reply.url(),
                                                     suggested_filename))
        download = DownloadItem(reply, self)
        download.finished.connect(
            functools.partial(self.on_finished, download))
        download.data_changed.connect(
            functools.partial(self.on_data_changed, download))
        download.error.connect(self.on_error)
        download.redirected.connect(
            functools.partial(self.on_redirect, download))
        download.basename = suggested_filename
        idx = len(self.downloads) + 1
        self.beginInsertRows(QModelIndex(), idx, idx)
        self.downloads.append(download)
        self.endInsertRows()

        if filename is not None:
            download.set_filename(filename)
        elif fileobj is not None:
            download.set_fileobj(fileobj)
            download.autoclose = False
        else:
            q = self._prepare_question()
            q.default = suggested_filename
            q.answered.connect(download.set_filename)
            q.cancelled.connect(download.cancel)
            download.cancelled.connect(q.abort)
            download.error.connect(q.abort)
            message_bridge = objreg.get('message-bridge', scope='window',
                                        window=self._win_id)
            message_bridge.ask(q, blocking=False)

        return download
Пример #12
0
 def _check_ignored(self, header):
     """Check if the passed header is ignored."""
     reply = stubs.FakeNetworkReply(headers={'Content-Disposition': header})
     cd_inline, cd_filename = http.parse_content_disposition(reply)
     self.assertEqual(cd_filename, DEFAULT_NAME)
     self.assertTrue(cd_inline)
Пример #13
0
 def _check_unnamed(self, header):
     """Check if the passed header results in an unnamed attachment."""
     reply = stubs.FakeNetworkReply(headers={'Content-Disposition': header})
     cd_inline, cd_filename = http.parse_content_disposition(reply)
     self.assertEqual(cd_filename, DEFAULT_NAME)
     self.assertFalse(cd_inline)
 def _check_ignored(self, header):
     """Check if the passed header is ignored."""
     reply = stubs.FakeNetworkReply(headers={'Content-Disposition': header})
     cd_inline, cd_filename = http.parse_content_disposition(reply)
     self.assertEqual(cd_filename, DEFAULT_NAME)
     self.assertTrue(cd_inline)
 def _check_unnamed(self, header):
     """Check if the passed header results in an unnamed attachment."""
     reply = stubs.FakeNetworkReply(headers={'Content-Disposition': header})
     cd_inline, cd_filename = http.parse_content_disposition(reply)
     self.assertEqual(cd_filename, DEFAULT_NAME)
     self.assertFalse(cd_inline)