Ejemplo n.º 1
0
 async def start_stream(self, stream: ManagedStream):
     stream.update_status(ManagedStream.STATUS_RUNNING)
     await self.storage.change_file_status(stream.stream_hash,
                                           ManagedStream.STATUS_RUNNING)
     await stream.setup(self.node, save_file=self.config.save_files)
     self.storage.content_claim_callbacks[
         stream.stream_hash] = lambda: self._update_content_claim(stream)
Ejemplo n.º 2
0
 async def stop_stream(self, stream: ManagedStream):
     stream.stop_download()
     if not stream.finished and os.path.isfile(stream.full_path):
         try:
             os.remove(stream.full_path)
         except OSError as err:
             log.warning("Failed to delete partial download %s from downloads directory: %s", stream.full_path,
                         str(err))
     if stream.running:
         stream.update_status(ManagedStream.STATUS_STOPPED)
         await self.storage.change_file_status(stream.stream_hash, ManagedStream.STATUS_STOPPED)
Ejemplo n.º 3
0
    async def start_stream(self, stream: ManagedStream) -> bool:
        """
        Resume or rebuild a partial or completed stream
        """
        if not stream.running and not stream.output_file_exists:
            if stream.downloader:
                stream.downloader.stop()
                stream.downloader = None

            # the directory is gone, can happen when the folder that contains a published file is deleted
            # reset the download directory to the default and update the file name
            if not os.path.isdir(stream.download_directory):
                stream.download_directory = self.config.download_dir

            stream.downloader = self.make_downloader(
                stream.sd_hash, stream.download_directory,
                stream.descriptor.suggested_file_name)
            if stream.status != ManagedStream.STATUS_FINISHED:
                await self.storage.change_file_status(stream.stream_hash,
                                                      'running')
                stream.update_status('running')
            stream.start_download(self.node)
            try:
                await asyncio.wait_for(
                    self.loop.create_task(
                        stream.downloader.wrote_bytes_event.wait()),
                    self.config.download_timeout)
            except asyncio.TimeoutError:
                await self.stop_stream(stream)
                if stream in self.streams:
                    self.streams.remove(stream)
                return False
            file_name = os.path.basename(stream.downloader.output_path)
            output_dir = os.path.dirname(stream.downloader.output_path)
            await self.storage.change_file_download_dir_and_file_name(
                stream.stream_hash, output_dir, file_name)
            stream._file_name = file_name
            stream.download_directory = output_dir
            self.wait_for_stream_finished(stream)
            return True
        return True