Option to set avatar image

master
Bob Mottram 2019-07-12 15:31:56 +01:00
parent 80b2bc3a4e
commit 1cfe63022f
3 changed files with 41 additions and 9 deletions

View File

@ -204,7 +204,7 @@ Even if Eve has an account on Alice's instance this won't help her very much unl
## Install ## Install
``` bash ``` 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 sudo pip install commentjson
``` ```

View File

@ -12,6 +12,7 @@ from person import createCapabilitiesInbox
from person import setPreferredNickname from person import setPreferredNickname
from person import setBio from person import setBio
from person import validNickname from person import validNickname
from person import setProfileImage
from webfinger import webfingerHandle from webfinger import webfingerHandle
from posts import getPosts from posts import getPosts
from posts import createPublicPost from posts import createPublicPost
@ -134,6 +135,8 @@ parser.add_argument("--noannounce","--norepeat", type=str2bool, nargs='?', \
parser.add_argument("--cw", type=str2bool, nargs='?', \ parser.add_argument("--cw", type=str2bool, nargs='?', \
const=True, default=False, \ const=True, default=False, \
help="Default capabilities don't allow posts without content warnings") 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() args = parser.parse_args()
debug=False debug=False
@ -398,6 +401,20 @@ if not args.domain and not domain:
print('Specify a domain with --domain [name]') print('Specify a domain with --domain [name]')
sys.exit() 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: if federationList:
print('Federating with: '+str(federationList)) print('Federating with: '+str(federationList))

View File

@ -10,6 +10,8 @@ import json
import commentjson import commentjson
import os import os
import fileinput import fileinput
import subprocess
from pathlib import Path
from Crypto.PublicKey import RSA from Crypto.PublicKey import RSA
from shutil import copyfile from shutil import copyfile
from webfinger import createWebfingerEndpoint from webfinger import createWebfingerEndpoint
@ -24,26 +26,35 @@ def generateRSAKey() -> (str,str):
return privateKeyPem,publicKeyPem return privateKeyPem,publicKeyPem
def setProfileImage(baseDir: str,httpPrefix :str,nickname: str,domain: str, \ 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 """Saves the given image file as an avatar or background
image for the given person image for the given person
""" """
imageFilename=imageFilename.replace('\n','')
if not (imageFilename.endswith('.png') or \ if not (imageFilename.endswith('.png') or \
imageFilename.endswith('.jpg') or \ imageFilename.endswith('.jpg') or \
imageFilename.endswith('.jpeg') or \ imageFilename.endswith('.jpeg') or \
imageFilename.endswith('.gif')): 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 port!=80 and port!=443:
if ':' not in domain: fullDomain=domain+':'+str(port)
domain=domain+':'+str(port)
handle=nickname.lower()+'@'+domain.lower() handle=nickname.lower()+'@'+domain.lower()
personFilename=baseDir+'/accounts/'+handle+'.json' personFilename=baseDir+'/accounts/'+handle+'.json'
if not os.path.isfile(personFilename): if not os.path.isfile(personFilename):
return print('person definition not found: '+personFilename)
return False
if not os.path.isdir(baseDir+'/accounts/'+handle): if not os.path.isdir(baseDir+'/accounts/'+handle):
return print('Account not found: '+baseDir+'/accounts/'+handle)
return False
iconFilenameBase='icon' iconFilenameBase='icon'
if imageType=='avatar' or imageType=='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: with open(personFilename, 'r') as fp:
personJson=commentjson.load(fp) personJson=commentjson.load(fp)
personJson[iconFilenameBase]['mediaType']=mediaType 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: with open(personFilename, 'w') as fp:
commentjson.dump(personJson, fp, indent=4, sort_keys=False) 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, \ def createPersonBase(baseDir: str,nickname: str,domain: str,port: int, \
httpPrefix: str, saveToFile: bool,password=None) -> (str,str,{},{}): httpPrefix: str, saveToFile: bool,password=None) -> (str,str,{},{}):