Skip to content

Commit aa3bb8b

Browse files
committed
done save to memory
1 parent 0ea1580 commit aa3bb8b

File tree

1 file changed

+8
-24
lines changed

1 file changed

+8
-24
lines changed

src/handlers.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
# 2023
33

44
import logging
5-
import os
65
import time
76
import traceback
8-
from io import BufferedRWPair, BufferedIOBase, BytesIO, FileIO
7+
from io import BytesIO
98

109
import requests
1110
from sqlalchemy import create_engine
@@ -96,29 +95,27 @@ async def handler_unknown_command(update: Update, context: ContextTypes.DEFAULT_
9695

9796
@handler
9897
async def handler_print(update: Update, context: ContextTypes.DEFAULT_TYPE):
99-
t = time.time()
10098
requisites = __auth(update)
10199
if requisites is None:
102100
await context.bot.send_message(chat_id=update.message.chat.id,
103101
text=ans['doc_not_accepted'])
104102
return
105103
try:
106-
fff, filename = await __get_attachments(update, context)
104+
filebytes, filename = await __get_attachments(update, context)
107105
except FileSizeError:
108106
await update.message.reply_text(text=ans['file_size_error'].format(update.message.document.file_name),
109107
reply_to_message_id=update.message.id,
110108
parse_mode=ParseMode('HTML'))
111109
return
112-
except FileNotFoundError:
110+
except TelegramError:
113111
await update.message.reply_text(text=ans['download_error'], reply_to_message_id=update.message.id)
114112
return
115113

116114
r = requests.post(settings.PRINT_URL + '/file',
117115
json={'surname': requisites[1], 'number': requisites[2], 'filename': filename})
118116
if r.status_code == 200:
119117
pin = r.json()['pin']
120-
files = {'file': (filename, fff.getvalue(), 'application/pdf', {'Expires': '0'})}
121-
# rfile = requests.post(settings.PRINT_URL + '/file/' + pin, files=files)
118+
files = {'file': (filename, filebytes.getvalue(), 'application/pdf', {'Expires': '0'})}
122119
rfile = requests.post(settings.PRINT_URL + '/file/' + pin, files=files)
123120
if rfile.status_code == 200:
124121
reply_markup = InlineKeyboardMarkup(
@@ -130,7 +127,6 @@ async def handler_print(update: Update, context: ContextTypes.DEFAULT_TYPE):
130127
disable_web_page_preview=True,
131128
parse_mode=ParseMode('HTML'))
132129
marketing.print_success(tg_id=update.message.chat.id, surname=requisites[1], number=requisites[2])
133-
print('Print time', time.time() - t)
134130
return
135131

136132
await context.bot.send_message(chat_id=update.effective_user.id,
@@ -234,21 +230,9 @@ class FileSizeError(Exception):
234230

235231

236232
async def __get_attachments(update, context):
237-
if not os.path.exists(settings.PDF_PATH):
238-
os.makedirs(settings.PDF_PATH)
239-
if not os.path.exists(os.path.join(settings.PDF_PATH, str(update.message.chat.id))):
240-
os.makedirs(os.path.join(settings.PDF_PATH, str(update.message.chat.id)))
241-
path_to_save = os.path.join(settings.PDF_PATH, str(update.message.chat.id), update.message.document.file_name)
242233
if update.message.document.file_size / 1024 ** 2 > settings.MAX_PDF_SIZE_MB:
243234
raise FileSizeError
244-
file = await context.bot.get_file(update.message.document.file_id)
245-
fff = BytesIO()
246-
await file.download_to_memory(fff)
247-
with open("output.pdf", "wb") as f:
248-
f.write(fff.getbuffer())
249-
# return fff
250-
# await file.download_to_drive(custom_path=path_to_save)
251-
# if not os.path.exists(path_to_save):
252-
# raise FileNotFoundError
253-
return fff, update.message.document.file_name
254-
# return path_to_save, update.message.document.file_name
235+
file_info = await context.bot.get_file(update.message.document.file_id)
236+
filebytes = BytesIO()
237+
await file_info.download_to_memory(filebytes)
238+
return filebytes, update.message.document.file_name

0 commit comments

Comments
 (0)