Skip to content

Commit

Permalink
perf: use file's real name
Browse files Browse the repository at this point in the history
  • Loading branch information
hlf20010508 committed Dec 28, 2023
1 parent 94e5da2 commit d276195
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
32 changes: 20 additions & 12 deletions modules/handlers/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ async def transfer_handler(event):

try:
if "document" in event.media.to_dict():
name = "%d%s" % (event.media.document.id, event.file.ext)
name = event.file.name
status_message = await Status_Message.create(event)
callback = Callback(event, status_message)
await multi_parts_uploader(tg_client, message.media.document, name, progress_callback=callback)
logger("File uploaded to %s" % os.path.join(last_remote_root_path, name))
await status_message.finish()
response_dict = await multi_parts_uploader(tg_client, message.media.document, name, progress_callback=callback)
await status_message.finish(
path=os.path.join(last_remote_root_path, response_dict['name']),
size=event.file.size
)
elif "photo" in event.media.to_dict():
name = "%d%s" % (event.media.photo.id, event.file.ext)
status_message = await Status_Message.create(event)
callback = Callback(event, status_message)
buffer = await message.download_media(file=bytes, progress_callback=callback)
onedrive.stream_upload(buffer, name)
logger("File uploaded to %s" % os.path.join(last_remote_root_path, name))
await status_message.finish()
await status_message.finish(
path=os.path.join(last_remote_root_path, name),
size=event.file.size
)
except Exception as e:
await event.reply('Error: %s' % logger(e))

Expand Down Expand Up @@ -68,20 +72,24 @@ async def transfer_handler(event):
if message:
try:
if "document" in message.media.to_dict():
name = "%d%s" % (message.media.document.id, message.file.ext)
name = event.file.name
status_message = await Status_Message.create(event)
callback = Callback(event, status_message)
await multi_parts_uploader(tg_client, message.media.document, name, progress_callback=callback)
logger("File uploaded to %s" % os.path.join(last_remote_root_path, name))
await status_message.finish()
response_dict = await multi_parts_uploader(tg_client, message.media.document, name, progress_callback=callback)
await status_message.finish(
path=os.path.join(last_remote_root_path, response_dict['name']),
size=event.file.size
)
elif "photo" in message.media.to_dict():
name = "%d%s" % (message.media.photo.id, message.file.ext)
status_message = await Status_Message.create(event)
callback = Callback(event, status_message)
buffer = await message.download_media(file=bytes, progress_callback=callback)
onedrive.stream_upload(buffer, name)
logger("File uploaded to %s" % os.path.join(last_remote_root_path, name))
await status_message.finish()
await status_message.finish(
path=os.path.join(last_remote_root_path, name),
size=event.file.size
)
except Exception as e:
await event.reply('Error: %s' % logger(e))
else:
Expand Down
12 changes: 8 additions & 4 deletions modules/handlers/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ async def url_handler(event):
await status_message.update()

if progress['status'] == 'completed':
logger("File uploaded to %s"%os.path.join(last_remote_root_path, name))
await status_message.finish()
await status_message.finish(
path=os.path.join(last_remote_root_path, name),
size=total_length
)
break

await asyncio.sleep(5)
Expand All @@ -93,8 +95,10 @@ async def url_handler(event):
logger('use local uploader to upload from url')
callback = Callback(event, status_message)
await multi_parts_uploader_from_url(name, local_response, callback)
logger("File uploaded to %s"%os.path.join(last_remote_root_path, name))
await status_message.finish()
await status_message.finish(
path=os.path.join(last_remote_root_path, name),
size=total_length
)
else:
logger(local_response.headers)
# this happends when downloading github release assets
Expand Down
9 changes: 6 additions & 3 deletions modules/onedrive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import asyncio
import time
from onedrivesdk import HttpProvider, AuthProvider, OneDriveClient
from onedrivesdk.options import HeaderOption
from onedrivesdk.options import HeaderOption, QueryOption
from onedrivesdk.error import OneDriveError, ErrorCode
from onedrivesdk.model.upload_session import UploadSession
from onedrivesdk.model.item import Item
Expand Down Expand Up @@ -86,7 +86,9 @@ def stream_upload(self, buffer, name):
request.send(data=buffer)

def multipart_upload_session_builder(self, name):
item = Item({})
item = Item({
'@microsoft.graph.conflictBehavior': 'rename'
})
session = self.client.item(path=self.remote_root_path).children[name].create_session(item).post()
return session

Expand All @@ -99,7 +101,7 @@ async def multipart_upload(self, uploader, buffer, offset):
while True:
try:
tries += 1
uploader.post(offset, part_size, buffer)
response = uploader.post(offset, part_size, buffer)
except OneDriveError as exc:
if exc.status_code in (408, 500, 502, 503, 504) and tries < 5:
await asyncio.sleep(5)
Expand All @@ -116,6 +118,7 @@ async def multipart_upload(self, uploader, buffer, offset):
# Swallow value errors (usually JSON error) and try again.
continue
break # while True
return response._prop_dict

def upload_from_url(self, url, name):
opts = [
Expand Down
8 changes: 5 additions & 3 deletions modules/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ async def multi_parts_uploader(
current_size += len(part)
task_list.clear()
buffer.seek(0)
await onedrive.multipart_upload(uploader, buffer, pre_offset)
response_dict = await onedrive.multipart_upload(uploader, buffer, pre_offset)
pre_offset = offset
buffer = BytesIO()
if progress_callback:
cor = progress_callback(current_size, document.size)
if inspect.isawaitable(cor):
await cor
buffer.close()
return response_dict


async def multi_parts_uploader_from_url(name, response, progress_callback=None):
Expand All @@ -89,10 +90,11 @@ async def multi_parts_uploader_from_url(name, response, progress_callback=None):
buffer = BytesIO()
buffer.write(chunk)
buffer.seek(0)
await onedrive.multipart_upload(uploader, buffer, offset)
response_dict = await onedrive.multipart_upload(uploader, buffer, offset)
offset += buffer.getbuffer().nbytes
part_num += 1
if progress_callback and (part_num % 5 ==0 or offset == total_length):
cor = progress_callback(offset, total_length)
if inspect.isawaitable(cor):
await cor
await cor
return response_dict
7 changes: 5 additions & 2 deletions modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ async def report_error(self, error, progress_url=None, response=None):
else:
await self.event.reply(self.error_template % logger(error))

async def finish(self):
self.status = 'Done.'
async def finish(self, path, size):
if size > 0:
self.status = logger('Done.\n\nFile uploaded to %s\n\nSize: %.2fMB' % (path, size / (1024 * 1024)))
else:
self.status = logger('Done.\n\nFile uploaded to %s' % path)
await edit_message(tg_bot, self.message, self.response)
await delete_message(self.event)
await delete_message(self.message)
Expand Down

0 comments on commit d276195

Please sign in to comment.