Beispiel #1
0
async def upload_files(log, event, uploader, files, progress_text, files_size_sum):
    uploaded_sum = 0
    for f in files:
        log.info('upload file ', f.info.fullpath)
        await uploader.add_file(f.info.fullpath, f.info.size,
                                tc.AsyncTorrentContentFileWrapper(f, event, progress_text, uploaded_sum,
                                                                  files_size_sum, log))
        uploaded_sum += f.info.size
Beispiel #2
0
async def on_button(event):
    global session
    try:
        l.debug(event)
        button_id = None
        zfile = None
        idlog = _log.new_logger(user_id=event.sender_id)
        #user = await client.get_entity(event.sender_id)

        if event.sender_id in in_progress_users:
            #idlog.info(user.username + ' ' + 'Button pressed while' +
            #           ' ' + ('being in progress' if event.sender_id in in_progress_users else 'in pending state'))
            idlog.debug('in_progress_users ' + str(in_progress_users))
            idlog.debug('pending_torrents ' + str(pending_torrents))
            await event.reply('Wait until current torrent will be downloaded')
            return

        in_progress_users.add(event.sender_id)
        nidlog = None

        if event.sender_id not in pending_torrents or int(
                event.data.split(b':')[1]) != pending_torrents[
                    event.sender_id][0][1]:
            # handle torrent request from old message
            idlog.info('request torrent handle from old message')
            event.message = await (await
                                   event.get_message()).get_reply_message()
            idlog.debug('reply message ' + str(event.message))
            magnet_link_or_bytes = await get_torrent_from_event(idlog, event)
            if magnet_link_or_bytes is None:
                idlog.warning('Not magnet or torrent in old message')
                try:
                    in_progress_users.remove(event.sender_id)
                    await event.reply('Failed resolve torrent')
                except:
                    pass
                return

            th = None
            try:
                th = await get_torrent_handle(idlog, magnet_link_or_bytes)
            except NoMetadataError:
                await event.edit(
                    'Couldn\'t resolve magnet link for metadata, torrent was removed from downloads. Sorry'
                )
                del pending_torrents[event.sender_id]
                in_progress_users.remove(event.sender_id)
                return
            except Exception as e:
                idlog.exception(e)
                await event.edit('Error occured')
                del pending_torrents[event.sender_id]
                in_progress_users.remove(event.sender_id)
                return
            th.calc_prioritized_piece_count()
            th.prepare_pieces_priority()

            nidlog = _log.new_logger(torrent_name=th.name(),
                                     user_id=event.sender_id)

            s = th.status()
            files_info = th.files()
            files = [File(f) for f in files_info]
            nidlog.debug(files_info)

            #status_msg_text, status_msg_current_file_text, zips = prepare_status_message(files, s.name, th.total_size())
            #            zfile = prepare_zip_file(th, s.name, files, event, status_msg_text, status_msg_current_file_text, event, nidlog)
            zfile = prepare_zip_file(th, s.name, files, event, nidlog)
            #zfile.progress_text = zips
            button_id, _ = event.data.split(b':')

        elif event.sender_id in pending_torrents:
            zfile, origin_msg_id = pending_torrents[event.sender_id][0]
            nidlog = _log.new_logger(torrent_name=zfile.torrent_handler.name(),
                                     user_id=event.sender_id)
            del pending_torrents[event.sender_id]

            button_id, msg_id = event.data.split(b':')
            #if int(msg_id) != origin_msg_id:
            # button was pressed on old torrent when new torrent was added recently
            #    nidlog.info('button was pressed on old torrent when new torrent was added recently')
            #    session.remove_torrent(zfile.torrent_handler)

        if button_id == b'1':
            # via Telegram
            nidlog.info('via Telegram')
            try:
                # await event.edit((await event.get_message()).message, buttons=[Button.inline('Cancel', str(event.sender_id))])
                await zfile.progress_callback(0)
                # await event.edit((await event.get_message()).message)
                zfile.event = event
                upload_task = client.loop.create_task(
                    upload_all_torrent_content(zfile, event, nidlog))
                tasks[event.sender_id] = upload_task
                try:
                    await upload_task
                except asyncio.CancelledError as e:
                    nidlog.info('Canceled')

                if not upload_task.cancelled():
                    # TODO receive progress_text somehow another way
                    nidlog.info('Completed!')
                    _files = [File(f) for f in zfile.torrent_handler.files()]
                    progress_text = prepare_status_message(
                        _files, zfile.torrent_handler.name(),
                        zfile.torrent_handler.total_size())
                    await event.edit(progress_text.format(100))
                # size = zfile.size
                # f = open('test.zip', 'wb')
                # d = await zfile.read(500*1024)
                # while len(d) > 0:
                #    f.write(d)
                #    await asyncio.sleep(0.1)
                #    d = await zfile.read(500*1024)
                # f.close()

            except tc.NoActivityTimeoutError:
                await event.edit(
                    'Couldn\'t find any peers, torrent was removed from downloads. Sorry'
                )
            except Exception as e:
                nidlog.exception(e)
            finally:
                zfile.close()
                session.remove_torrent(zfile.torrent_handler)

        elif button_id == b'2':
            # via Web
            nidlog.info('via Web')
            try:
                # url = 'https://api.anonfiles.com/upload'
                # url = 'http://127.0.0.1'

                # r = await httpx.post(url=url, files={'file': SyncZipTorrentContentFile(zfile)})
                # print(r.text)
                # curl = await asyncio.create_subprocess_exec('/usr/bin/curl',
                #                            '-T',
                #                            '-',
                #                            '-X',
                #                            'POST',
                #                            'https://api.anonfiles.com/upload',
                #                            stdout=asyncio.subprocess.PIPE,
                #                           stdin=asyncio.subprocess.PIPE, loop=client.loop)
                # await asyncio.sleep(5)
                # d = await zfile.read()
                # while d:
                #    print('curl write ', len(d))
                #    curl.stdin.write(d)
                #    await curl.stdin.drain()
                #    d = await zfile.read()
                # print('curl close stdin')
                # curl.stdin.close()
                # ret = await curl.stdout.read()
                # print(ret)
                # curl.kill()

                #                headers={
                #    'Content-Length': str(zfile.size + 195 + 2*len(zfile.name.encode('utf')))
                # }
                #                data = aiohttp.FormData(quote_fields=False)
                #                data.add_field('file', zfile, filename=zfile.name)

                #                async with aiohttp.ClientSession() as ses:
                #                    print('begin file uploading')
                #                    async with ses.post(url, data=data, headers=headers)  as resp:
                #                        print(await resp.text())
                await event.edit(
                    (await event.get_message()).message,
                    buttons=[Button.inline('Cancel', str(event.sender_id))])
                zfile.set_should_split(False)
                fu = await fex.FexUploader.new()
                upload_task = client.loop.create_task(
                    fu.add_file(zfile.name, zfile.size, zfile))
                tasks[event.sender_id] = upload_task
                try:
                    await upload_task
                except asyncio.CancelledError as e:
                    nidlog.info('Canceled')
                # await fu.add_file(zfile.name, zfile.size, zfile)

                if not upload_task.cancelled():
                    nidlog.info('Download url ' + fu.download_link)

                    # TODO receive progress_text somehow another way
                    _files = [File(f) for f in zfile.torrent_handler.files()]
                    progress_text = prepare_status_message(
                        _files, zfile.torrent_handler.name(),
                        zfile.torrent_handler.total_size())
                    await event.edit(progress_text.format(100),
                                     buttons=[
                                         Button.url('Download content',
                                                    fu.download_link)
                                     ])

                # await upload_to_ipfs(zfile)
            except tc.NoActivityTimeoutError:
                await event.edit(
                    'Couldn\'t find any peers, torrent was removed from downloads. Sorry'
                )
            except Exception as e:
                nidlog.exception(e)
            finally:
                await fu.delete()
                zfile.close()
                session.remove_torrent(zfile.torrent_handler)
        # await event.edit((await event.get_message()).message,buttons=[Button.url('Download content', 'http://206.189.63.205:8080/'+str(file_key))])
        elif button_id == b'3':
            # upload via Web raw
            nidlog.info('via Web raw')
            try:
                fu = await fex.FexUploader.new(nidlog)
                await event.edit(
                    (await event.get_message()).message,
                    buttons=[Button.inline('Cancel', str(event.sender_id))])

                files = []
                relative_size = 0
                _files = [File(f) for f in zfile.torrent_handler.files()]
                progress_text = prepare_status_message(
                    _files, zfile.torrent_handler.name(),
                    zfile.torrent_handler.total_size())
                callback = lambda percent: \
                        event.edit(progress_text.format(percent), buttons=[Button.inline('Cancel', str(event.sender_id))])
                for f in zfile.files:
                    files.append(
                        fex.FexFile(
                            tc.AsyncTorrentContentFileWrapper(
                                f, callback, relative_size,
                                zfile.files_size_sum, nidlog), f.info.fullpath,
                            f.info.size))
                    relative_size += f.info.size

                upload_task = client.loop.create_task(fu.upload_files(files))
                tasks[event.sender_id] = upload_task

                try:
                    await upload_task
                except asyncio.CancelledError as e:
                    nidlog.info('Canceled')

                if not upload_task.cancelled():
                    nidlog.info('Completed!')
                    nidlog.info('Download url ' + fu.download_link)
                    await event.edit(progress_text.format(100),
                                     buttons=[
                                         Button.url('Download content',
                                                    fu.download_link)
                                     ])

            except tc.NoActivityTimeoutError:
                await event.edit(
                    'Couldn\'t find any peers, torrent was removed from downloads. Sorry'
                )
            except Exception as e:
                nidlog.exception(e)
            finally:
                await fu.delete()
                zfile.close()
                session.remove_torrent(zfile.torrent_handler)
        else:
            nidlog.error('Error: wrong button id ', button_id)

    except Exception as e:
        l.exception(e)
    finally:
        if event.sender_id in tasks:
            if not tasks[event.sender_id].cancelled():
                tasks[event.sender_id].cancel()
            del tasks[event.sender_id]
        if event.sender_id in in_progress_users:
            in_progress_users.remove(event.sender_id)
        if event.sender_id in pending_torrents:
            del pending_torrents[event.sender_id]