Beispiel #1
0
 def test_as_app_iter(self, filedepot, image_asset):
     from pyramid.response import FileIter
     data = image_asset.read()
     f = self._create_file(data)
     resp = UploadedFileResponse(f.data, DummyRequest())
     assert isinstance(resp.app_iter, FileIter)
     assert ''.join(resp.app_iter) == data
Beispiel #2
0
    def test_as_app_iter(self, filedepot):
        from pyramid.response import FileIter

        f = self._create_file()
        resp = UploadedFileResponse(f.data, DummyRequest())
        assert isinstance(resp.app_iter, FileIter)
        assert ''.join(resp.app_iter) == 'file contents'
Beispiel #3
0
    def image(self, subpath=None):
        """Return the image in a specific scale, either inline
        (default) or as attachment.

        :param subpath: [<image_scale>]/download] (optional).
                        When 'download' is the last element in subpath,
                        the image is served with a 'Content-Disposition:
                        attachment' header.  <image_scale> has to be one of the
                        predefined image_scales - either from the defaults in
                        this module or one set with a
                        kotti.image_scales.<scale_name> in your app config ini
                        file.
        :type subpath: str

        :result: complete response object
        :rtype: pyramid.response.Response
        """

        if subpath is None:
            subpath = self.request.subpath

        width, height = (None, None)
        subpath = list(subpath)

        if (len(subpath) > 0) and (subpath[-1] == "download"):
            disposition = "attachment"
            subpath.pop()
        else:
            disposition = "inline"

        if len(subpath) == 1:
            scale = subpath[0]
            if scale in image_scales:
                # /path/to/image/scale/thumb
                width, height = image_scales[scale]

        if not (width and height):
            return UploadedFileResponse(self.context.data, self.request,
                                        disposition)

        image, format, size = scaleImage(self.context.data.file.read(),
                                         width=width,
                                         height=height,
                                         direction="thumb")
        res = Response(
            headerlist=[
                ('Content-Disposition', '{0};filename="{1}"'.format(
                    disposition,
                    self.context.filename.encode('ascii', 'ignore'))),
                ('Content-Length', str(len(image))),
                ('Content-Type', str(self.context.mimetype)),
            ],
            body=image,
        )

        return res
Beispiel #4
0
    def test_redirect(self, filedepot):
        f = self._create_file()
        f.data._thaw()
        f.data['_public_url'] = 'http://example.com'
        f.data._freeze()

        with pytest.raises(HTTPMovedPermanently) as e:
            UploadedFileResponse(f.data, DummyRequest())

        response = e.value
        assert response.headers['Location'] == 'http://example.com'
Beispiel #5
0
    def test_redirect(self, filedepot):
        class PublicFile(object):
            public_url = 'http://example.com'

        class PublicData(object):
            file = PublicFile()

        with pytest.raises(HTTPMovedPermanently) as e:
            UploadedFileResponse(PublicData(), DummyRequest())

        response = e.value
        assert response.headers['Location'] == 'http://example.com'
Beispiel #6
0
    def test_caching(self, filedepot, monkeypatch):
        import datetime
        import webob.response

        f = self._create_file()
        d = datetime.datetime(2012, 12, 31, 13)

        class mockdatetime:
            @staticmethod
            def utcnow():
                return d

        monkeypatch.setattr(webob.response, 'datetime', mockdatetime)

        resp = UploadedFileResponse(f.data, DummyRequest(), cache_max_age=10)

        # this is set by filedepot fixture
        assert resp.headers['Last-Modified'] == 'Sun, 30 Dec 2012 00:00:00 GMT'
        assert resp.headers['Expires'] == 'Mon, 31 Dec 2012 13:00:10 GMT'
Beispiel #7
0
 def test_guess_content_type(self, filedepot, image_asset):
     f = self._create_file(image_asset.read(), "file.png", None)
     resp = UploadedFileResponse(f.data, DummyRequest())
     assert resp.headers['Content-Type'] == 'image/png'
Beispiel #8
0
 def test_unknown_filename(self, filedepot, image_asset2):
     f = self._create_file(b'foo', "file.bar", None)
     resp = UploadedFileResponse(f.data, DummyRequest())
     assert resp.headers['Content-Type'] == 'application/octet-stream'
Beispiel #9
0
 def test_as_body(self, filedepot, image_asset):
     data = image_asset.read()
     f = self._create_file(data)
     resp = UploadedFileResponse(f.data, DummyRequest())
     assert resp.body == data
Beispiel #10
0
 def test_guess_content_type(self, filedepot):
     f = self._create_file("file contents", u"file.jpg", None)
     resp = UploadedFileResponse(f.data, DummyRequest())
     assert resp.headers['Content-Type'] == 'image/jpeg'
Beispiel #11
0
 def test_unknown_filename(self, filedepot):
     f = self._create_file("file contents", u"file", None)
     resp = UploadedFileResponse(f.data, DummyRequest())
     assert resp.headers['Content-Type'] == 'application/octet-stream'
Beispiel #12
0
 def test_as_body(self, filedepot):
     f = self._create_file()
     resp = UploadedFileResponse(f.data, DummyRequest())
     assert resp.body == 'file contents'