@@ -337,8 +337,7 @@ async def share_art(request):
337337 content_type = assetFileType
338338
339339 try :
340- from matrix_client .api import MatrixHttpApi
341- from matrix_client .client import MatrixClient
340+ from nio import AsyncClient , LoginResponse , UploadResponse
342341
343342 homeserver = 'matrix.org'
344343 if matrix_auth :
@@ -347,31 +346,81 @@ async def share_art(request):
347346 if not homeserver .startswith ("https://" ):
348347 homeserver = "https://" + homeserver
349348
350- client = MatrixClient (homeserver )
351- try :
352- token = client . login ( username = matrix_auth [ 'username' ], password = matrix_auth [ 'password' ])
353- if not token :
354- return web . json_response ({ "error" : "Invalid Matrix credentials." }, content_type = 'application/json' , status = 400 )
355- except Exception :
349+ client = AsyncClient (homeserver , matrix_auth [ 'username' ] )
350+
351+ # Login
352+ login_resp = await client . login ( matrix_auth [ 'password' ])
353+ if not isinstance ( login_resp , LoginResponse ) or not login_resp . access_token :
354+ await client . close ()
356355 return web .json_response ({"error" : "Invalid Matrix credentials." }, content_type = 'application/json' , status = 400 )
357356
358- matrix = MatrixHttpApi ( homeserver , token = token )
357+ # Upload asset
359358 with open (asset_filepath , 'rb' ) as f :
360- mxc_url = matrix .media_upload (f .read (), content_type , filename = filename )['content_uri' ]
361-
362- workflow_json_mxc_url = matrix .media_upload (prompt ['workflow' ], 'application/json' , filename = 'workflow.json' )['content_uri' ]
363-
359+ upload_resp , _maybe_keys = await client .upload (f , content_type = content_type , filename = filename )
360+ asset_data = f .seek (0 ) or f .read () # get size for info below
361+ if not isinstance (upload_resp , UploadResponse ) or not upload_resp .content_uri :
362+ await client .close ()
363+ return web .json_response ({"error" : "Failed to upload asset to Matrix." }, content_type = 'application/json' , status = 500 )
364+ mxc_url = upload_resp .content_uri
365+
366+ # Upload workflow JSON
367+ import io
368+ workflow_json_bytes = json .dumps (prompt ['workflow' ]).encode ('utf-8' )
369+ workflow_io = io .BytesIO (workflow_json_bytes )
370+ upload_workflow_resp , _maybe_keys = await client .upload (workflow_io , content_type = 'application/json' , filename = 'workflow.json' )
371+ workflow_io .seek (0 )
372+ if not isinstance (upload_workflow_resp , UploadResponse ) or not upload_workflow_resp .content_uri :
373+ await client .close ()
374+ return web .json_response ({"error" : "Failed to upload workflow to Matrix." }, content_type = 'application/json' , status = 500 )
375+ workflow_json_mxc_url = upload_workflow_resp .content_uri
376+
377+ # Send text message
364378 text_content = ""
365379 if title :
366380 text_content += f"{ title } \n "
367381 if description :
368382 text_content += f"{ description } \n "
369383 if credits :
370384 text_content += f"\n credits: { credits } \n "
371- matrix .send_message (comfyui_share_room_id , text_content )
372- matrix .send_content (comfyui_share_room_id , mxc_url , filename , 'm.image' )
373- matrix .send_content (comfyui_share_room_id , workflow_json_mxc_url , 'workflow.json' , 'm.file' )
374- except Exception :
385+ await client .room_send (
386+ room_id = comfyui_share_room_id ,
387+ message_type = "m.room.message" ,
388+ content = {"msgtype" : "m.text" , "body" : text_content }
389+ )
390+
391+ # Send image
392+ await client .room_send (
393+ room_id = comfyui_share_room_id ,
394+ message_type = "m.room.message" ,
395+ content = {
396+ "msgtype" : "m.image" ,
397+ "body" : filename ,
398+ "url" : mxc_url ,
399+ "info" : {
400+ "mimetype" : content_type ,
401+ "size" : len (asset_data )
402+ }
403+ }
404+ )
405+
406+ # Send workflow JSON file
407+ await client .room_send (
408+ room_id = comfyui_share_room_id ,
409+ message_type = "m.room.message" ,
410+ content = {
411+ "msgtype" : "m.file" ,
412+ "body" : "workflow.json" ,
413+ "url" : workflow_json_mxc_url ,
414+ "info" : {
415+ "mimetype" : "application/json" ,
416+ "size" : len (workflow_json_bytes )
417+ }
418+ }
419+ )
420+
421+ await client .close ()
422+
423+ except :
375424 import traceback
376425 traceback .print_exc ()
377426 return web .json_response ({"error" : "An error occurred when sharing your art to Matrix." }, content_type = 'application/json' , status = 500 )
0 commit comments