Esempio n. 1
0
    def ensure_media_is_in_local_cache(self, file_info):
        """Ensures that the given file is in the local cache. Attempts to
        download it from storage providers if it isn't.

        Args:
            file_info (FileInfo)

        Returns:
            Deferred[str]: Full path to local file
        """
        path = self._file_info_to_path(file_info)
        local_path = os.path.join(self.local_media_directory, path)
        if os.path.exists(local_path):
            defer.returnValue(local_path)

        dirname = os.path.dirname(local_path)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        for provider in self.storage_providers:
            res = yield provider.fetch(path, file_info)
            if res:
                with res:
                    consumer = BackgroundFileConsumer(open(local_path, "w"))
                    yield res.write_to_consumer(consumer)
                    yield consumer.wait()
                defer.returnValue(local_path)

        raise Exception("file could not be found")
Esempio n. 2
0
    def ensure_media_is_in_local_cache(self, file_info):
        """Ensures that the given file is in the local cache. Attempts to
        download it from storage providers if it isn't.

        Args:
            file_info (FileInfo)

        Returns:
            Deferred[str]: Full path to local file
        """
        path = self._file_info_to_path(file_info)
        local_path = os.path.join(self.local_media_directory, path)
        if os.path.exists(local_path):
            defer.returnValue(local_path)

        dirname = os.path.dirname(local_path)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        for provider in self.storage_providers:
            res = yield provider.fetch(path, file_info)
            if res:
                with res:
                    consumer = BackgroundFileConsumer(
                        open(local_path, "wb"), self.hs.get_reactor()
                    )
                    yield res.write_to_consumer(consumer)
                    yield consumer.wait()
                defer.returnValue(local_path)

        raise Exception("file could not be found")
Esempio n. 3
0
    def test_push_producer_feedback(self):
        string_file = BlockingStringWrite()
        consumer = BackgroundFileConsumer(string_file, reactor=reactor)

        try:
            producer = NonCallableMock(
                spec_set=["pauseProducing", "resumeProducing"])

            resume_deferred = defer.Deferred()
            producer.resumeProducing.side_effect = lambda: resume_deferred.callback(
                None)

            consumer.registerProducer(producer, True)

            number_writes = 0
            with string_file.write_lock:
                for _ in range(consumer._PAUSE_ON_QUEUE_SIZE):
                    consumer.write("Foo")
                    number_writes += 1

                producer.pauseProducing.assert_called_once()

            yield string_file.wait_for_n_writes(number_writes)

            yield resume_deferred
            producer.resumeProducing.assert_called_once()
        finally:
            consumer.unregisterProducer()

        yield consumer.wait()

        self.assertTrue(string_file.closed)
Esempio n. 4
0
    def test_push_producer_feedback(self):
        string_file = BlockingStringWrite()
        consumer = BackgroundFileConsumer(string_file)

        try:
            producer = NonCallableMock(spec_set=["pauseProducing", "resumeProducing"])

            resume_deferred = defer.Deferred()
            producer.resumeProducing.side_effect = lambda: resume_deferred.callback(None)

            consumer.registerProducer(producer, True)

            number_writes = 0
            with string_file.write_lock:
                for _ in range(consumer._PAUSE_ON_QUEUE_SIZE):
                    consumer.write("Foo")
                    number_writes += 1

                producer.pauseProducing.assert_called_once()

            yield string_file.wait_for_n_writes(number_writes)

            yield resume_deferred
            producer.resumeProducing.assert_called_once()
        finally:
            consumer.unregisterProducer()

        yield consumer.wait()

        self.assertTrue(string_file.closed)
Esempio n. 5
0
    async def ensure_media_is_in_local_cache(self, file_info: FileInfo) -> str:
        """Ensures that the given file is in the local cache. Attempts to
        download it from storage providers if it isn't.

        Args:
            file_info

        Returns:
            Full path to local file
        """
        path = self._file_info_to_path(file_info)
        local_path = os.path.join(self.local_media_directory, path)
        if os.path.exists(local_path):
            return local_path

        dirname = os.path.dirname(local_path)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        for provider in self.storage_providers:
            res = await provider.fetch(path, file_info)  # type: Any
            if res:
                with res:
                    consumer = BackgroundFileConsumer(
                        open(local_path, "wb"), self.hs.get_reactor()
                    )
                    await res.write_to_consumer(consumer)
                    await consumer.wait()
                return local_path

        raise Exception("file could not be found")
Esempio n. 6
0
    def test_pull_consumer(self):
        string_file = StringIO()
        consumer = BackgroundFileConsumer(string_file, reactor=reactor)

        try:
            producer = DummyPullProducer()

            yield producer.register_with_consumer(consumer)

            yield producer.write_and_wait("Foo")

            self.assertEqual(string_file.getvalue(), "Foo")

            yield producer.write_and_wait("Bar")

            self.assertEqual(string_file.getvalue(), "FooBar")
        finally:
            consumer.unregisterProducer()

        yield consumer.wait()

        self.assertTrue(string_file.closed)
Esempio n. 7
0
    def test_pull_consumer(self):
        string_file = StringIO()
        consumer = BackgroundFileConsumer(string_file)

        try:
            producer = DummyPullProducer()

            yield producer.register_with_consumer(consumer)

            yield producer.write_and_wait("Foo")

            self.assertEqual(string_file.getvalue(), "Foo")

            yield producer.write_and_wait("Bar")

            self.assertEqual(string_file.getvalue(), "FooBar")
        finally:
            consumer.unregisterProducer()

        yield consumer.wait()

        self.assertTrue(string_file.closed)
Esempio n. 8
0
    def test_push_consumer(self):
        string_file = BlockingStringWrite()
        consumer = BackgroundFileConsumer(string_file)

        try:
            producer = NonCallableMock(spec_set=[])

            consumer.registerProducer(producer, True)

            consumer.write("Foo")
            yield string_file.wait_for_n_writes(1)

            self.assertEqual(string_file.buffer, "Foo")

            consumer.write("Bar")
            yield string_file.wait_for_n_writes(2)

            self.assertEqual(string_file.buffer, "FooBar")
        finally:
            consumer.unregisterProducer()

        yield consumer.wait()

        self.assertTrue(string_file.closed)
Esempio n. 9
0
    async def ensure_media_is_in_local_cache(self, file_info: FileInfo) -> str:
        """Ensures that the given file is in the local cache. Attempts to
        download it from storage providers if it isn't.

        Args:
            file_info

        Returns:
            Full path to local file
        """
        path = self._file_info_to_path(file_info)
        local_path = os.path.join(self.local_media_directory, path)
        if os.path.exists(local_path):
            return local_path

        # Fallback for paths without method names
        # Should be removed in the future
        if file_info.thumbnail and file_info.server_name:
            legacy_path = self.filepaths.remote_media_thumbnail_rel_legacy(
                server_name=file_info.server_name,
                file_id=file_info.file_id,
                width=file_info.thumbnail_width,
                height=file_info.thumbnail_height,
                content_type=file_info.thumbnail_type,
            )
            legacy_local_path = os.path.join(self.local_media_directory,
                                             legacy_path)
            if os.path.exists(legacy_local_path):
                return legacy_local_path

        dirname = os.path.dirname(local_path)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        for provider in self.storage_providers:
            res = await provider.fetch(path, file_info)  # type: Any
            if res:
                with res:
                    consumer = BackgroundFileConsumer(open(local_path, "wb"),
                                                      self.reactor)
                    await res.write_to_consumer(consumer)
                    await consumer.wait()
                return local_path

        raise Exception("file could not be found")
Esempio n. 10
0
    def test_push_consumer(self):
        string_file = BlockingStringWrite()
        consumer = BackgroundFileConsumer(string_file, reactor=reactor)

        try:
            producer = NonCallableMock(spec_set=[])

            consumer.registerProducer(producer, True)

            consumer.write("Foo")
            yield string_file.wait_for_n_writes(1)

            self.assertEqual(string_file.buffer, "Foo")

            consumer.write("Bar")
            yield string_file.wait_for_n_writes(2)

            self.assertEqual(string_file.buffer, "FooBar")
        finally:
            consumer.unregisterProducer()

        yield consumer.wait()

        self.assertTrue(string_file.closed)