From 31fee856474d59d86b3d1cd760a9b58ad5f481b7 Mon Sep 17 00:00:00 2001 From: thecooltechguy Date: Fri, 20 Oct 2023 00:54:49 -0700 Subject: [PATCH] working v1 --- __init__.py | 73 +++++++++++++++++++++++++++++++++++++++++-- js/comfyui-manager.js | 24 +++++++++++--- 2 files changed, 90 insertions(+), 7 deletions(-) diff --git a/__init__.py b/__init__.py index da72cf70..68ce38fd 100644 --- a/__init__.py +++ b/__init__.py @@ -1,4 +1,5 @@ import configparser +import mimetypes import shutil import folder_paths import os @@ -1200,8 +1201,76 @@ async def channel_url_list(request): async def share_art(request): # get json data json_data = await request.json() - print(json_data) - return web.Response(status=200) + + credits = json_data['credits'] + title = json_data['title'] + description = json_data['description'] + is_nsfw = json_data['is_nsfw'] + prompt = json_data['prompt'] + potential_outputs = json_data['potential_outputs'] + + # for now, pick the first output + output_to_share = potential_outputs[0] + assert output_to_share['type'] in ('image',) + + output_dir = folder_paths.get_output_directory() + asset_filename = output_to_share['image']['filename'] + asset_subfolder = output_to_share['image']['subfolder'] + asset_output_type = output_to_share['image']['type'] + + assert asset_output_type == "output" + + if asset_subfolder: + asset_filepath = os.path.join(output_dir, asset_subfolder, asset_filename) + else: + asset_filepath = os.path.join(output_dir, asset_filename) + + # get the mime type of the asset + assetFileType = mimetypes.guess_type(asset_filepath)[0] + + share_website_host = "https://comfyworkflows.com" + share_endpoint = f"{share_website_host}/api" + + # get presigned urls + async with aiohttp.ClientSession(trust_env=True, connector=aiohttp.TCPConnector(verify_ssl=False)) as session: + async with session.post( + f"{share_endpoint}/get_presigned_urls", + json={ + "assetFileName": asset_filename, + "assetFileType": assetFileType, + }, + ) as resp: + assert resp.status == 200 + presigned_urls_json = await resp.json() + assetFilePresignedUrl = presigned_urls_json["assetFilePresignedUrl"] + assetFileKey = presigned_urls_json["assetFileKey"] + + # upload asset + async with aiohttp.ClientSession(trust_env=True, connector=aiohttp.TCPConnector(verify_ssl=False)) as session: + async with session.put(assetFilePresignedUrl, data=open(asset_filepath, "rb")) as resp: + assert resp.status == 200 + + # make a POST request to /api/upload_workflow with form data key values + async with aiohttp.ClientSession(trust_env=True, connector=aiohttp.TCPConnector(verify_ssl=False)) as session: + form = aiohttp.FormData() + form.add_field("assetFileKey", assetFileKey) + form.add_field("assetFileType", assetFileType) + form.add_field("sharedWorkflowWorkflowJsonString", json.dumps(prompt['workflow'])) + form.add_field("sharedWorkflowPromptJsonString", json.dumps(prompt['output'])) + form.add_field("shareWorkflowCredits", credits) + form.add_field("shareWorkflowTitle", title) + form.add_field("shareWorkflowDescription", description) + form.add_field("shareWorkflowIsNSFW", str(is_nsfw).lower()) + + async with session.post( + f"{share_endpoint}/upload_workflow", + data=form, + ) as resp: + assert resp.status == 200 + upload_workflow_json = await resp.json() + workflowId = upload_workflow_json["workflowId"] + + return web.json_response({"url" : f"{share_website_host}/workflows/{workflowId}"}, content_type='application/json', status=200) WEB_DIRECTORY = "js" NODE_CLASS_MAPPINGS = {} diff --git a/js/comfyui-manager.js b/js/comfyui-manager.js index c91980bb..13ec6184 100644 --- a/js/comfyui-manager.js +++ b/js/comfyui-manager.js @@ -2055,7 +2055,7 @@ class ShareDialog extends ComfyDialog { // todo: add support for other output node types (animatediff combine, etc.) alert("No SaveImage node found. To share this workflow, please run a SaveImage node to your graph and re-run your prompt."); } else { - alert("To share this, please run a prompt first and then click 'Share'."); + alert("To share this, first run a prompt. Once it's done, click 'Share'."); } this.close(); return; @@ -2074,7 +2074,7 @@ class ShareDialog extends ComfyDialog { is_nsfw: this.is_nsfw_checkbox.checked, prompt, potential_outputs, - potential_output_nodes + // potential_output_nodes }) }); @@ -2084,12 +2084,15 @@ class ShareDialog extends ComfyDialog { return; } - this.final_message.innerHTML = "Your art has been shared: " + response.url + ""; + const response_json = await response.json(); + + this.final_message.innerHTML = "Your art has been shared: " + response_json.url + ""; this.final_message.style.color = "green"; // hide the share button + this.share_button.textContent = "Shared!"; this.share_button.style.display = "none"; - + // this.close(); } @@ -2143,7 +2146,18 @@ class ShareDialog extends ComfyDialog { $el("button", { type: "button", textContent: "Close", - onclick: () => this.close() + onclick: () => { + // Reset state + this.share_button.textContent = "Share"; + this.share_button.style.display = "inline-block"; + this.final_message.innerHTML = ""; + this.final_message.style.color = "white"; + this.credits_input.value = ""; + this.title_input.value = ""; + this.description_input.value = ""; + this.is_nsfw_checkbox.checked = false; + this.close() + } }), $el("br", {}, []), ];