Move images to the media directory

master
Bob Mottram 2019-07-16 17:08:21 +01:00
parent c99828c264
commit d7598125cf
2 changed files with 28 additions and 1 deletions

View File

@ -30,7 +30,10 @@ from inbox import runInboxQueue
from inbox import savePostToInboxQueue
from follow import getFollowingFeed
from auth import authorize
from auth import createPassword
from threads import threadWithTrace
from media import getMediaPath
from media import createMediaDirs
import os
import sys
@ -136,6 +139,30 @@ class PubServer(BaseHTTPRequestHandler):
return False
# https://www.w3.org/TR/activitypub/#create-activity-outbox
messageJson['object']['attributedTo']=messageJson['actor']
if messageJson['object'].get('attachment'):
attachmentIndex=0
if messageJson['object']['attachment'][attachmentIndex].get('mediaType'):
fileExtension='png'
if messageJson['object']['attachment'][attachmentIndex]['mediaType'].endswith('jpeg'):
fileExtension='jpg'
if messageJson['object']['attachment'][attachmentIndex]['mediaType'].endswith('gif'):
fileExtension='gif'
mediaDir=self.server.baseDir+'/accounts/'+self.postToNickname+'@'+self.server.domain
uploadMediaFilename=mediaDir+'/upload.'+fileExtension
if not os.path.isfile(uploadMediaFilename):
del messageJson['object']['attachment']
else:
# generate a path for the uploaded image
mPath=getMediaPath()
mediaPath=mPath+'/'+createPassword(32)+'.'+fileExtension
createMediaDirs(self.server.baseDir,mPath)
mediaFilename=self.server.baseDir+'/'+mediaPath
# move the uploaded image to its new path
os.rename(uploadMediaFilename,mediaFilename)
# change the url of the attachment
messageJson['object']['attachment'][attachmentIndex]['url']= \
self.server.httpPrefix+'://'+self.server.domain+'/'+mediaPath
permittedOutboxTypes=[
'Create','Announce','Like','Follow','Undo', \
'Update','Add','Remove','Block','Delete'

View File

@ -1064,7 +1064,7 @@ def testClientToServer():
assert thrBob.isAlive()==False
os.chdir(baseDir)
#shutil.rmtree(aliceDir)
shutil.rmtree(aliceDir)
shutil.rmtree(bobDir)
def runAllTests():