def get_or_create_account(self): try: user, is_authed = AccountService.get_or_create_account( self.key.data, self.password.data ) if not user or not is_authed: self.key.errors.append("Key or password is incorrect") except Error as e: self.key.errors.append(e.error) return None, False return user, is_authed
def api_upload_file(upload_type="file"): if upload_type not in ["file", "paste"]: # The type the user provided doesn't exist. raise json_error("This upload type does not exist") # support login thru the API key = request.form.get("key") password = request.form.get("password") if key and password and not current_user.is_authenticated: user, is_authed = AccountService.get_or_create_account(key, password) if user and is_authed: login_user(user) else: raise json_error("Incorrect key or password") user_id = current_user.get_id() is_file = upload_type == "file" file: Optional[Union[FileInterface, PasteInterface]] = None if is_file: file = submit_file(user_id) elif upload_type == "paste": file = submit_paste(user_id) if not file: raise json_error("Failed to upload file") shorturl = file["shorturl"] ext = file["ext"] if is_file else "paste" host = get_host() path = "/" + ("" if is_file else upload_type + "/") return json_response( type=ext, key="anon" if not user_id else current_user["key"], base=host, url=path + shorturl, full_url=host + path + shorturl, )