def delete_file(): path = get_path_to_host() request_path = get_server_path() + path request_headers = {'X-AUTH-TOKEN': get_auth_token_header()} request = urllib.request.Request(request_path, headers=request_headers) request.get_method = lambda: 'DELETE' send_request(request)
def upload_and_serve_file(viaPublish = False): file_to_upload_path = get_file_to_upload() path = get_path_to_host() validate_file(file_to_upload_path) with open(file_to_upload_path, 'r') as content_file: content = content_file.read() image_details = [] for imagePath in re.findall('(?:!\[.*?\]\(([^:]*?)\))', content): head, tail = os.path.split(imagePath) image_details.append({ 'originalPath': imagePath, 'updatedAt': 0 #os.path.getmtime(imagePath) }) payload = {"content": content, "imageDetails": image_details} request_params = json.dumps(payload).encode('utf8') request_headers = {'content-type': 'application/json', 'X-AUTH-TOKEN': get_auth_token_header()} request_path = get_server_path() + path public_url = request_path is_public = "-p" in sys.argv or viaPublish if is_public: request_path = request_path + '?forcePublic=true' request = urllib.request.Request(request_path, data=request_params, headers=request_headers) response = send_request(request) data = json.load(response) images_to_upload = data.get('imagesToUpload') if not images_to_upload or len(images_to_upload) == 0: print(f'This page is now served') if is_public: print(f'It should be available on when public :\n{public_url}') else: print(f'This page is not public yet! Please run the command:\n\nqtip public {get_path_to_host()} \n To make this page public to the world.') if not viaPublish: sys.exit() # upload images for imagePath in images_to_upload: upload_image(imagePath)
def restrict_ip(): path = get_path_to_host() restricted_ip_list = get_ip_list() request_path = get_server_path() + path + '?method=restrict-ip' request_headers = {'X-AUTH-TOKEN': get_auth_token_header(), 'content-type': 'application/json'} payload = {"ipList": restricted_ip_list} request_params = json.dumps(payload).encode('utf8') request = urllib.request.Request(request_path, headers=request_headers, data=request_params) request.get_method = lambda: 'PUT' send_request(request)
def get_file_data(): path = get_path_to_host() request_path = get_server_path() + path + '?method=cat' request_headers = {'X-AUTH-TOKEN': get_auth_token_header()} request = urllib.request.Request(request_path, headers=request_headers) request.get_method = lambda: 'GET' response = send_request(request) data = json.load(response) return data.get('results')
def restrict_access_code(): path = get_path_to_host() access_code = sys.argv[3] request_path = get_server_path() + path + '?method=restrict-access-code' request_headers = {'X-AUTH-TOKEN': get_auth_token_header(), 'content-type': 'application/json'} payload = {"code": access_code} request_params = json.dumps(payload).encode('utf8') request = urllib.request.Request(request_path, headers=request_headers, data=request_params) request.get_method = lambda: 'PUT' send_request(request)
def upload_image(file_path): file_to_upload_path = os.path.join(os.path.dirname(get_file_to_upload()), file_path.replace('./', '')) path = get_path_to_host() request_path = get_server_path() + '/___image' request_headers = { 'X-AUTH-TOKEN': get_auth_token_header(), } url = request_path data = { 'path': path } files = {'file': open(file_to_upload_path, 'rb')} r = requests.post(url, data=data, files=files, headers=request_headers)
def make_private(): path = get_path_to_host() print(f'This will make the file `{path}` hidden to everyone on the internet!\n\n') ans = input('Confirm making file private (y/n): ') if ans != 'y': print('Operation aborted') sys.exit() request_path = get_server_path() + path + '?method=make-private' request_headers = {'X-AUTH-TOKEN': get_auth_token_header()} request = urllib.request.Request(request_path, headers=request_headers) request.get_method = lambda: 'PUT' send_request(request)
def make_public(): path = get_path_to_host() warning_text = f'{bcolors.FAIL}{bcolors.BOLD}You cannot make a project private again, you would need to delete the project to hide it.\n{bcolors.ENDC}' print(f'This will make the file `{path}` visible to everyone on the internet!\n\n{warning_text}') ans = input('Confirm making file public (y/n): ') if ans != 'y': print('Operation aborted') sys.exit() request_path = get_server_path() + path + '?method=make-public' request_headers = {'X-AUTH-TOKEN': get_auth_token_header()} request = urllib.request.Request(request_path, headers=request_headers) request.get_method = lambda: 'PUT' send_request(request)
def list_directory(): path = get_path_to_host() request_path = get_server_path() + path + '?method=ls' request_headers = {'X-AUTH-TOKEN': get_auth_token_header()} request = urllib.request.Request(request_path, headers=request_headers) request.get_method = lambda: 'GET' response = send_request(request) data = json.load(response) for result in data.get('results'): if 'public' in result: print(f'{bcolors.FAIL}{result}{bcolors.ENDC}') elif 'restricted' in result: print(f'{bcolors.WARNING}{result}{bcolors.ENDC}') else: print(result)