diff --git a/media.py b/media.py index 9ec15e45..5e4d94e3 100644 --- a/media.py +++ b/media.py @@ -17,7 +17,7 @@ from auth import createPassword from shutil import copyfile def getImageHash(imageFilename: str): - return blurencode(numpy.array(Image.open("img/logo.png").convert("RGB"))) + return blurencode(numpy.array(Image.open(imageFilename).convert("RGB"))) def isImage(imageFilename: str) -> bool: if imageFilename.endswith('.png') or \ @@ -26,12 +26,17 @@ def isImage(imageFilename: str) -> bool: return True return False -def createMediaPath(baseDir: str,weeksSinceEpoch: int) -> None: +def createMediaDirs(baseDir: str,mediaPath: str) -> None: if not os.path.isdir(baseDir+'/media'): os.mkdir(baseDir+'/media') - if not os.path.isdir(baseDir+'/media/'+str(weeksSinceEpoch)): - os.mkdir(baseDir+'/media/'+str(weeksSinceEpoch)) + if not os.path.isdir(baseDir+'/'+mediaPath): + os.mkdir(baseDir+'/'+mediaPath) +def getMediaPath() -> str: + currTime=datetime.datetime.utcnow() + weeksSinceEpoch=(currTime - datetime.datetime(1970,1,1)).days/7 + return 'media/'+str(weeksSinceEpoch) + def attachImage(baseDir: str,httpPrefix: str,domain: str,port: int, \ postJson: {},imageFilename: str,description: str, \ useBlurhash: bool) -> {}: @@ -55,10 +60,9 @@ def attachImage(baseDir: str,httpPrefix: str,domain: str,port: int, \ if ':' not in domain: domain=domain+':'+str(port) - currTime=datetime.datetime.utcnow() - weeksSinceEpoch=(currTime - datetime.datetime(1970,1,1)).days/7 - createMediaPath(baseDir,weeksSinceEpoch) - mediaPath='media/'+str(weeksSinceEpoch)+'/'+createPassword(32)+'.'+fileExtension + mPath=getMediaPath() + createMediaDirs(baseDir,mPath) + mediaPath=mPath+'/'+createPassword(32)+'.'+fileExtension mediaFilename=baseDir+'/'+mediaPath attachmentJson={ diff --git a/posts.py b/posts.py index b62aa5a4..617556ed 100644 --- a/posts.py +++ b/posts.py @@ -401,7 +401,7 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \ if attachImageFilename: newPost['object']= \ attachImage(baseDir,httpPrefix,domain,port, \ - postJson['object'],attachImageFilename, \ + newPost['object'],attachImageFilename, \ imageDescription,useBlurhash) else: newPost = { @@ -437,7 +437,7 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \ if attachImageFilename: newPost= \ attachImage(baseDir,httpPrefix,domain,port, \ - postJson,attachImageFilename, \ + newPost,attachImageFilename, \ imageDescription,useBlurhash) if ccUrl: if len(ccUrl)>0: diff --git a/tests.py b/tests.py index a8d18120..e4f6bddc 100644 --- a/tests.py +++ b/tests.py @@ -44,6 +44,7 @@ from auth import authorizeBasic from auth import storeBasicCredentials from like import likePost from announce import announcePublic +from media import getMediaPath testServerAliceRunning = False testServerBobRunning = False @@ -259,23 +260,30 @@ def testPostMessageBetweenServers(): ccUrl=None alicePersonCache={} aliceCachedWebfingers={} - useBlurhash=False + attachedImageFilename=baseDir+'/img/logo.png' + attachedImageDescription='Logo' + useBlurhash=True # nothing in Alice's outbox outboxPath=aliceDir+'/accounts/alice@'+aliceDomain+'/outbox' assert len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==0 - sendResult = sendPost(sessionAlice,aliceDir,'alice', aliceDomain, alicePort, 'bob', bobDomain, bobPort, ccUrl, httpPrefix, 'Why is a mouse when it spins?', followersOnly, saveToFile, clientToServer,None,None,useBlurhash, federationList, aliceSendThreads, alicePostLog, aliceCachedWebfingers,alicePersonCache,inReplyTo, inReplyToAtomUri, subject) + sendResult = sendPost(sessionAlice,aliceDir,'alice', aliceDomain, alicePort, 'bob', bobDomain, bobPort, ccUrl, httpPrefix, 'Why is a mouse when it spins?', followersOnly, saveToFile, clientToServer,attachedImageFilename,attachedImageDescription,useBlurhash, federationList, aliceSendThreads, alicePostLog, aliceCachedWebfingers,alicePersonCache,inReplyTo, inReplyToAtomUri, subject) print('sendResult: '+str(sendResult)) queuePath=bobDir+'/accounts/bob@'+bobDomain+'/queue' inboxPath=bobDir+'/accounts/bob@'+bobDomain+'/inbox' + mPath=getMediaPath() + mediaPath=aliceDir+'/'+mPath for i in range(30): if os.path.isdir(inboxPath): if len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])>0: if len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==1: - break + if len([name for name in os.listdir(mediaPath) if os.path.isfile(os.path.join(mediaPath, name))])>0: + break time.sleep(1) + # Image attachment created + assert len([name for name in os.listdir(mediaPath) if os.path.isfile(os.path.join(mediaPath, name))])>0 # inbox item created assert len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==1 # queue item removed