def encrypt(user_id, file_id): try: multi_id = int(round(time.time() * 1000)) user_path = f'../storage/users/{user_id}' key_file_path = f'/keys/{HashHandler.choose_hash_function("sha1", str(multi_id + 1))}.key' key = FileEncryptor.create_key(user_path, key_file_path) file, file_name = FileEncryptor.prepare_for_crypt( file_id, user_id, user_path) fernet = Fernet(key) encrypted_file = fernet.encrypt(file) encrypted_filename = f'{file_name}.encrypted' encrypted_file_path = f'/files/encrypted/{HashHandler.choose_hash_function("sha1", str(multi_id))}' with open(user_path + encrypted_file_path, 'wb') as f: f.write(encrypted_file) file_description = 'My encrypted file' DBfiles.insert(multi_id, user_id, encrypted_filename, file_description, encrypted_file_path, is_encrypted=1) DBfiles.insert_file_key(user_id, multi_id, key_file_path, multi_id + 1) except (RuntimeError, TypeError, NameError): return 'Something went wrong while encrypting the file' else: return 'Successfully encrypted the file'
def change_file_name(user_id, file_id, new_file_name, file_description): try: DBfiles.update_file(user_id, file_id, new_file_name, file_description) except OSError: return 'Something went wrong while changing the file' else: return 'Successfully changed the file'
def delete_file(user_id, file_id, path, is_encrypted): try: user_path = f'../storage/users/{user_id}' os.remove(f'{user_path}{path}') if int(is_encrypted): FileEncryptor.delete_key(user_id, file_id, user_path) DBfiles.delete_file(file_id, user_id) except OSError: return 'Something went wrong while deleting the file' else: return 'Successfully deleted the file'
def prepare_for_crypt(file_id, user_id, user_path): db_file = DBfiles.get_file(file_id, user_id) file_path = db_file[0]['path'] file_name = db_file[0]['file_name'] with open(f'{user_path}{file_path}', 'rb') as f: file_data = f.read() return file_data, file_name
def delete_key(user_id, file_id, user_path): key = DBfiles.get_file_key(file_id, user_id) try: os.remove(f'{user_path}{key[0]["key_path"]}') except OSError: return 'Something went wrong while deleting the key' else: return 'Successfully deleted the key'
def write_file(user_id, file, file_description): file_id = int(round(time.time() * 1000)) user_path = f'../storage/users/{user_id}' size = 0 path = f'/files/unencrypted/{HashHandler.choose_hash_function("sha1", str(file_id))}' with open(f'{user_path}{path}', 'wb') as f: while True: data = file.file.read(8192) if not data: break f.write(data) size += len(data) DBfiles.insert(file_id, user_id, file.filename, file_description, path, is_encrypted=0)
def get_user_files(self, auth_token): user_id = InputValidator.check_session_value('user_id') if AuthHandler.check_for_auth( user_id) and AuthHandler.check_auth_token(auth_token): user_id = str(user_id) files = DBfiles.get_files(user_id) return files else: return ResponseHandler.unauthorized_response( 'You are unauthorized')
def file_delete(self, file_id, is_encrypted, auth_token): user_id = InputValidator.check_session_value('user_id') if AuthHandler.check_for_auth( user_id) and AuthHandler.check_auth_token(auth_token): user_id = str(user_id) message = FileHandler.delete_file( user_id, file_id, DBfiles.get_file_path(user_id, file_id), is_encrypted) return ResponseHandler.success_response(message) else: return ResponseHandler.unauthorized_response( 'You are unauthorized')
def decrypt(user_id, file_id): try: file_id_decrypt = int(round(time.time() * 1000)) user_path = f'../storage/users/{user_id}' key = FileEncryptor.get_key(user_id, file_id, user_path) file, file_name = FileEncryptor.prepare_for_crypt( file_id, user_id, user_path) fernet = Fernet(key) decrypted = fernet.decrypt(file) decrypted_file_path = f'/files/unencrypted/{HashHandler.choose_hash_function("sha1", str(file_id_decrypt))}' with open(f'{user_path}{decrypted_file_path}', 'wb') as f: f.write(decrypted) file_description = 'My decrypted file' DBfiles.insert(file_id_decrypt, user_id, file_name.replace('.encrypted', ''), file_description, decrypted_file_path, is_encrypted=0) except (RuntimeError, TypeError, NameError): return 'Something went wrong while decrypting the file' else: return 'Successfully decrypted the file'
def file_download(self, file_id, auth_token): user_id = InputValidator.check_session_value('user_id') if AuthHandler.check_for_auth( user_id) and AuthHandler.check_auth_token(auth_token): user_id = str(user_id) user_path = f'../storage/users/{user_id}' absolute_file_path = os.path.abspath( f'{user_path}{DBfiles.get_file_path(user_id, file_id)}') file_name = DBfiles.get_file_name(file_id, user_id) return serve_file(absolute_file_path, disposition="attachment", name=file_name) else: ResponseHandler.unauthorized_response('You are unauthorized') raise cherrypy.HTTPRedirect('/sign')
def get_key(user_id, file_id, user_path): db_file_key = DBfiles.get_file_key(file_id, user_id) key_file_path = db_file_key[0]['key_path'] with open(f'{user_path}{key_file_path}', 'rb') as f: key = f.read() return key