From 1cfe63022f25e81eecbc1c365855c57d20cc1073 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 12 Jul 2019 15:31:56 +0100 Subject: [PATCH] Option to set avatar image --- README.md | 2 +- epicyon.py | 17 +++++++++++++++++ person.py | 31 +++++++++++++++++++++++-------- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 350803cb..7107afcb 100644 --- a/README.md +++ b/README.md @@ -204,7 +204,7 @@ Even if Eve has an account on Alice's instance this won't help her very much unl ## Install ``` bash -sudo pacman -S tor python-pip python-pysocks python-pycryptodome python-beautifulsoup4 +sudo pacman -S tor python-pip python-pysocks python-pycryptodome python-beautifulsoup4 imagemagick sudo pip install commentjson ``` diff --git a/epicyon.py b/epicyon.py index 5009fd2f..5459dd8d 100644 --- a/epicyon.py +++ b/epicyon.py @@ -12,6 +12,7 @@ from person import createCapabilitiesInbox from person import setPreferredNickname from person import setBio from person import validNickname +from person import setProfileImage from webfinger import webfingerHandle from posts import getPosts from posts import createPublicPost @@ -134,6 +135,8 @@ parser.add_argument("--noannounce","--norepeat", type=str2bool, nargs='?', \ parser.add_argument("--cw", type=str2bool, nargs='?', \ const=True, default=False, \ help="Default capabilities don't allow posts without content warnings") +parser.add_argument('--icon','--avatar', dest='avatar', type=str,default=None, \ + help='Set the avatar filename for an account') args = parser.parse_args() debug=False @@ -398,6 +401,20 @@ if not args.domain and not domain: print('Specify a domain with --domain [name]') sys.exit() +if args.avatar: + if not os.path.isfile(args.avatar): + print(args.avatar+' is not an image filename') + sys.exit() + if not args.nickname: + print('Specify a nickname with --nickname [name]') + sys.exit() + if setProfileImage(baseDir,httpPrefix,args.nickname,domain, \ + port,args.avatar,'avatar'): + print('Avatar added for '+args.nickname) + else: + print('Avatar was not added for '+args.nickname) + sys.exit() + if federationList: print('Federating with: '+str(federationList)) diff --git a/person.py b/person.py index c084aef5..56a5d847 100644 --- a/person.py +++ b/person.py @@ -10,6 +10,8 @@ import json import commentjson import os import fileinput +import subprocess +from pathlib import Path from Crypto.PublicKey import RSA from shutil import copyfile from webfinger import createWebfingerEndpoint @@ -24,26 +26,35 @@ def generateRSAKey() -> (str,str): return privateKeyPem,publicKeyPem def setProfileImage(baseDir: str,httpPrefix :str,nickname: str,domain: str, \ - imageFilename: str,imageType :str): + port :int,imageFilename: str,imageType :str) -> bool: """Saves the given image file as an avatar or background image for the given person """ + imageFilename=imageFilename.replace('\n','') if not (imageFilename.endswith('.png') or \ imageFilename.endswith('.jpg') or \ imageFilename.endswith('.jpeg') or \ imageFilename.endswith('.gif')): - return + print('Profile image must be png, jpg or gif format') + return False + if imageFilename.startswith('~/'): + imageFilename=imageFilename.replace('~/',str(Path.home())+'/') + + if ':' in domain: + domain=domain.split(':')[0] + fullDomain=domain if port!=80 and port!=443: - if ':' not in domain: - domain=domain+':'+str(port) + fullDomain=domain+':'+str(port) handle=nickname.lower()+'@'+domain.lower() personFilename=baseDir+'/accounts/'+handle+'.json' if not os.path.isfile(personFilename): - return + print('person definition not found: '+personFilename) + return False if not os.path.isdir(baseDir+'/accounts/'+handle): - return + print('Account not found: '+baseDir+'/accounts/'+handle) + return False iconFilenameBase='icon' if imageType=='avatar' or imageType=='icon': @@ -65,10 +76,14 @@ def setProfileImage(baseDir: str,httpPrefix :str,nickname: str,domain: str, \ with open(personFilename, 'r') as fp: personJson=commentjson.load(fp) personJson[iconFilenameBase]['mediaType']=mediaType - personJson[iconFilenameBase]['url']=httpPrefix+'://'+domain+'/users/'+nickname+'/'+iconFilename + personJson[iconFilenameBase]['url']=httpPrefix+'://'+fullDomain+'/users/'+nickname+'/'+iconFilename with open(personFilename, 'w') as fp: commentjson.dump(personJson, fp, indent=4, sort_keys=False) - copyfile(imageFilename,profileFilename) + + cmd = '/usr/bin/convert '+imageFilename+' -size 128x128 -quality 50 '+profileFilename + subprocess.call(cmd, shell=True) + return True + return False def createPersonBase(baseDir: str,nickname: str,domain: str,port: int, \ httpPrefix: str, saveToFile: bool,password=None) -> (str,str,{},{}):