def upload_file(self, file, file_type, success_callback=None, failure_callback=None, **kwargs): future = BaleFuture(request_id=None, response_body_module=None, response_body_class=None, success_callback=success_callback, failure_callback=failure_callback, **kwargs) buffer = get_file_buffer(file=file) if buffer is None: future.reject(response=None) return file_size = get_file_size(buffer) file_crc32 = get_file_crc32(buffer) def file_upload_url_success(result, user_data): file_id = result.body.file_id user_id = result.body.user_id url = result.body.url dup = result.body.dup data = buffer headers = {'filesize': str(file_size)} async def upload_data(): try: async with aiohttp.ClientSession() as session: async with session.put( url, data=data, headers=headers) as upload_response: status = upload_response.status if status == 200: future.set_user_data(file_id=file_id, user_id=user_id, url=url, dup=dup) future.resolve(response=None) else: future.reject(response=None) except Exception as e: future.reject(response=None) asyncio.ensure_future(upload_data()) def file_upload_url_failure(result, user_data): future.reject(response=result) self.get_file_upload_url(size=file_size, crc=file_crc32, file_type=file_type, success_callback=file_upload_url_success, failure_callback=file_upload_url_failure)
def send_photo(self, user_peer, image, caption_text="", name="", file_storage_version=1, mime_type="image/jpeg", success_callback=None, failure_callback=None, **kwargs): image_buffer = get_file_buffer(file=image) file_size = sys.getsizeof(image_buffer) im = Image.open(io.BytesIO(image_buffer)) width, height = im.size thumb = get_image_thumbnails(im) def success_upload_image(user_data, server_response): file_id = str(server_response.get("file_id", None)) access_hash = str(server_response.get("user_id", None)) photo_message = PhotoMessage(file_id=file_id, access_hash=access_hash, name=name, file_size=file_size, mime_type=mime_type, file_storage_version=file_storage_version, width=width, height=height, caption_text=TextMessage(text=caption_text), thumb=thumb) self.send_message(message=photo_message, peer=user_peer, success_callback=success_callback, failure_callback=failure_callback, kwargs=kwargs) self.upload_file(file=image, file_type="file", success_callback=success_upload_image, failure_callback=failure_callback)
def upload_file(self, file, file_type, success_callback=None, failure_callback=None, **kwargs): future = BaleFuture(request_id=None, response_body_module=None, response_body_class=None, success_callback=success_callback, failure_callback=failure_callback, **kwargs) buffer = get_file_buffer(file=file) if buffer is None: future.reject(response=None) return file_size = get_file_size(buffer) file_crc32 = get_file_crc32(buffer) def file_upload_url_success(result, user_data): file_id = result.body.file_id user_id = result.body.user_id url = result.body.url dup = result.body.dup data = buffer headers = {'filesize': str(file_size)} async def upload_data(): try: async with aiohttp.ClientSession() as session: async with session.put(url, data=data, headers=headers) as upload_response: status = upload_response.status if status == 200: future.set_user_data(file_id=file_id, user_id=user_id, url=url, dup=dup) future.resolve(response=None) else: future.reject(response=None) except Exception as e: future.reject(response=None) asyncio.ensure_future(upload_data()) def file_upload_url_failure(result, user_data): future.reject(response=result) self.get_file_upload_url(size=file_size, crc=file_crc32, file_type=file_type, success_callback=file_upload_url_success, failure_callback=file_upload_url_failure)