예제 #1
0
    def test_static_content_stream_stream_data_in_range(self):
        """
        Test StaticContentStream stream_data_in_range function,
        asserts that we get the requested number of bytes
        first_byte and last_byte are chosen to be simple but non trivial values
        and to have total_length > STREAM_DATA_CHUNK_SIZE (1024)
        """
        data = SAMPLE_STRING
        item = FakeGridFsItem(data)
        static_content_stream = StaticContentStream('loc',
                                                    'name',
                                                    'type',
                                                    item,
                                                    length=item.length)

        first_byte = 100
        last_byte = 1500

        total_length = 0
        stream = static_content_stream.stream_data_in_range(
            first_byte, last_byte)
        for chunck in stream:
            total_length += len(chunck)

        self.assertEqual(total_length, last_byte - first_byte + 1)
예제 #2
0
    def process_request(self, request):

        if request.path.startswith('/' + XASSET_LOCATION_TAG + '/'):
            if request.GET.get('width') or request.GET.get('height'):
                # generate the requested thumbnail location
                width = int(request.GET['width'])
                path = request.path.split('/')
                category = 'thumbnail'
                name = os.path.splitext(path[5])
                path[5] = "%s-w%d%s" % (name[0], width, name[1])
                path[4] = category

                path = '/'.join(path)
                thumbnail_location = StaticContent.get_location_from_path(
                    path)  # calculate thumbnail 'location' in gridfs
                try:  # is it already created ?
                    thumbnail_content = contentstore().find(thumbnail_location,
                                                            as_stream=True)
                except NotFoundError:

                    # get original asset
                    asset_location = StaticContent.get_location_from_path(
                        request.path)
                    try:
                        content = contentstore().find(asset_location,
                                                      as_stream=True)
                    except NotFoundError:
                        return  # if original asset do not exists, let the request pass by
                    # generate thumbnail
                    im = Image.open(
                        StringIO.StringIO(content.copy_to_in_mem().data))
                    im = im.convert('RGB')
                    size = (
                        width, width
                    )  # PIL is dumb, we shall use a better tool to generate smartly framed thumbnails
                    im.thumbnail(size, Image.ANTIALIAS)
                    thumbnail_file = StringIO.StringIO()
                    im.save(thumbnail_file, 'JPEG')
                    thumbnail_file.seek(0)

                    # store thumbnail in contentstore
                    thumbnail_name = StaticContent.generate_thumbnail_name(
                        thumbnail_location.name)
                    thumbnail_content = StaticContentStream(
                        thumbnail_location, thumbnail_name, 'image/jpeg',
                        thumbnail_file)

                    contentstore().save(thumbnail_content.copy_to_in_mem())

                # return found or generated tumbnail
                response = HttpResponse(
                    thumbnail_content.copy_to_in_mem().stream_data(),
                    content_type=thumbnail_content.content_type)
                return response

            return super(ThumbnailStaticContentServer,
                         self).process_request(request)
예제 #3
0
    def test_static_content_stream_stream_data(self):
        """
        Test StaticContentStream stream_data function, asserts that we get all the bytes
        """
        data = SAMPLE_STRING
        item = FakeGridFsItem(data)
        static_content_stream = StaticContentStream('loc', 'name', 'type', item, length=item.length)

        total_length = 0
        stream = static_content_stream.stream_data()
        for chunck in stream:
            total_length += len(chunck)

        self.assertEqual(total_length, static_content_stream.length)
예제 #4
0
    def process_request(self, request):

        if request.path.startswith('/' + XASSET_LOCATION_TAG + '/'):
            if request.GET.get('width') or request.GET.get('height'):
                # generate the requested thumbnail location
                width = int(request.GET['width'])
                path = request.path.split('/')
                category = 'thumbnail'
                name = os.path.splitext(path[5])
                path[5] = "%s-w%d%s" % (name[0], width, name[1])
                path[4] = category

                path = '/'.join(path)
                thumbnail_location = StaticContent.get_location_from_path(path)  # calculate thumbnail 'location' in gridfs
                try:  # is it already created ?
                    thumbnail_content = contentstore().find(thumbnail_location, as_stream=True)
                except NotFoundError:

                    # get original asset
                    asset_location = StaticContent.get_location_from_path(request.path)
                    try:
                        content = contentstore().find(asset_location, as_stream=True)
                    except NotFoundError:
                        return  # if original asset do not exists, let the request pass by
                    # generate thumbnail
                    im = Image.open(StringIO.StringIO(content.copy_to_in_mem().data))
                    im = im.convert('RGB')
                    size = (width, width)  # PIL is dumb, we shall use a better tool to generate smartly framed thumbnails
                    im.thumbnail(size, Image.ANTIALIAS)
                    thumbnail_file = StringIO.StringIO()
                    im.save(thumbnail_file, 'JPEG')
                    thumbnail_file.seek(0)

                    # store thumbnail in contentstore
                    thumbnail_name = StaticContent.generate_thumbnail_name(thumbnail_location.name)
                    thumbnail_content = StaticContentStream(thumbnail_location, thumbnail_name,
                                                      'image/jpeg', thumbnail_file)

                    contentstore().save(thumbnail_content.copy_to_in_mem())

                # return found or generated tumbnail
                response = HttpResponse(thumbnail_content.copy_to_in_mem().stream_data(), content_type=thumbnail_content.content_type)
                return response

            return super(ThumbnailStaticContentServer, self).process_request(request)
예제 #5
0
    def test_static_content_stream_stream_data_in_range(self):
        """
        Test StaticContentStream stream_data_in_range function,
        asserts that we get the requested number of bytes
        first_byte and last_byte are chosen to be simple but non trivial values
        and to have total_length > STREAM_DATA_CHUNK_SIZE (1024)
        """
        data = SAMPLE_STRING
        item = FakeGridFsItem(data)
        static_content_stream = StaticContentStream('loc', 'name', 'type', item, length=item.length)

        first_byte = 100
        last_byte = 1500

        total_length = 0
        stream = static_content_stream.stream_data_in_range(first_byte, last_byte)
        for chunck in stream:
            total_length += len(chunck)

        self.assertEqual(total_length, last_byte - first_byte + 1)