async def create_file(session, event, subscriber, message, user, full_scan): """Create a file object from a message.""" to_id, to_type = get_peer_information(message.to_id) file_type, file_id = await get_file_information(event, message, subscriber, user, full_scan) if not file_type: return None # Check if this exact file from the same message is already downloaded. # This is a hard constraint which shouldn't be violated. if File.exists(session, subscriber, file_id): return None # The file path is depending on the media type. # In case such a file already exists and duplicate files are disabled, the function returns None. # In that case we will return early. file_path, file_name = get_file_path(subscriber, get_username(user), message) # Don't check zipped files from ourselves. # Otherwise we would double in size on each /scan_chat /zip command combination me = await event.client.get_me() splitted = file_name.rsplit('.', maxsplit=2) if user.id == me.id and len(splitted) == 3 and splitted[1] == '7z': return None if file_path is None: # Inform the user about duplicate files if subscriber.verbose: text = f"File with name {file_name} already exists." await event.respond(text) sentry.captureMessage("File already exists", extra={ 'file_path': file_path, 'file_name': file_name, 'chat': subscriber.chat_name, 'user': get_username(user) }, tags={'level': 'info'}) return None # The file path is depending on the media type. new_file = File(file_id, to_id, user.id, subscriber, to_type, message.id, file_type, file_name, file_path) session.add(new_file) session.commit() return new_file
async def set_name(event, session): """Set the name of the current chat (also affects the saving directory.""" to_id, to_type = get_peer_information(event.message.to_id) new_chat_name = event.message.message.split(" ", maxsplit=1)[1].strip() # We already save to zips, prevent that if new_chat_name == "zips": return "Invalid chat name. Pick another." subscriber = Subscriber.get_or_create( session, to_id, to_type, event.message, chat_name=new_chat_name ) old_chat_path = get_chat_path(subscriber.chat_name) new_chat_path = get_chat_path(new_chat_name) # Handle any command that tries to escape the download directory new_real_path = os.path.realpath(new_chat_path) target_real_path = os.path.realpath(config["download"]["target_dir"]) if ( not new_real_path.startswith(target_real_path) or new_real_path == target_real_path ): user = await archive.get_entity(types.PeerUser(event.message.from_id)) sentry.captureMessage( "User tried to escape directory.", extra={ "new_chat_name": new_chat_name, "chat": subscriber.chat_name, "user": get_username(user), }, tags={"level": "info"}, ) return "Please stop fooling around and don't try to escape the directory. I have been notified about this." # Check whether we already have a chat with this name if ( session.query(Subscriber) .filter(Subscriber.chat_name == new_chat_name) .one_or_none() ): return "Chat name already exists. Please choose another one." # Move the old directory to the new location elif old_chat_path != new_chat_path: subscriber.chat_name = new_chat_name if os.path.exists(old_chat_path): os.rename(old_chat_path, new_chat_path) return "Chat name changed."
async def set_name(event, session): """Set query attributes.""" to_id, to_type = get_peer_information(event.message.to_id) new_channel_name = event.message.message.split(' ', maxsplit=1)[1].strip() if new_channel_name == 'zips': return "Invalid channel name. Pick another." subscriber = Subscriber.get_or_create(session, to_id, to_type, event.message, channel_name=new_channel_name) old_channel_path = get_channel_path(subscriber.channel_name) new_channel_path = get_channel_path(new_channel_name) new_real_path = os.path.realpath(new_channel_path) target_real_path = os.path.realpath(config.TARGET_DIR) if not new_real_path.startswith(target_real_path) or \ new_real_path == target_real_path: user = await archive.get_entity(event.message.from_id) sentry.captureMessage("User tried to escape directory.", extra={ 'new_channel_name': new_channel_name, 'channel': subscriber.channel_name, 'user': get_username(user) }, tags={'level': 'info'}) return "Please stop fooling around and try to escape the directory. I have been notified as well." if session.query(Subscriber) \ .filter(Subscriber.channel_name == new_channel_name) \ .one_or_none(): return "Channel name already exists. Please choose another one." elif old_channel_path != new_channel_path: subscriber.channel_name = new_channel_name if os.path.exists(old_channel_path): os.rename(old_channel_path, new_channel_path) return "Channel name changed."