Remove attachments for deleted posts

master
Bob Mottram 2019-07-14 18:02:41 +01:00
parent e1a6528307
commit 090f8eacc3
4 changed files with 15 additions and 9 deletions

View File

@ -504,7 +504,7 @@ if args.archive:
else:
print('Archiving to '+args.archive+'...')
archiveMedia(baseDir,args.archive,archiveWeeks)
archivePosts(baseDir,args.archive,archiveMaxPosts)
archivePosts(baseDir,httpPrefix,args.archive,archiveMaxPosts)
print('Archiving complete')
sys.exit()

View File

@ -536,7 +536,7 @@ def receiveDelete(session,handle: str,baseDir: str, \
print('DEBUG: delete post not found in inbox or outbox')
print(messageJson['object'])
return True
deletePost(baseDir,handle.split('@')[0],handle.split('@')[1],postFilename,debug)
deletePost(baseDir,httpPrefix,handle.split('@')[0],handle.split('@')[1],postFilename,debug)
if debug:
print('DEBUG: post deleted - '+postFilename)
return True

View File

@ -948,7 +948,7 @@ def createBoxBase(baseDir: str,boxname: str, \
return boxHeader
return boxItems
def archivePosts(baseDir: str,archiveDir: str,maxPostsInBox=256) -> None:
def archivePosts(baseDir: str,httpPrefix: str,archiveDir: str,maxPostsInBox=256) -> None:
"""Archives posts for all accounts
"""
if archiveDir:
@ -972,16 +972,16 @@ def archivePosts(baseDir: str,archiveDir: str,maxPostsInBox=256) -> None:
if not os.path.isdir(archiveDir+'/accounts/'+handle+'/outbox'):
os.mkdir(archiveDir+'/accounts/'+handle+'/outbox')
archiveSubdir=archiveDir+'/accounts/'+handle+'/inbox'
archivePostsForPerson(nickname,domain,baseDir, \
archivePostsForPerson(httpPrefix,nickname,domain,baseDir, \
'inbox',archiveSubdir, \
maxPostsInBox)
if archiveDir:
archiveSubdir=archiveDir+'/accounts/'+handle+'/outbox'
archivePostsForPerson(nickname,domain,baseDir, \
archivePostsForPerson(httpPrefix,nickname,domain,baseDir, \
'outbox',archiveSubdir, \
maxPostsInBox)
def archivePostsForPerson(nickname: str,domain: str,baseDir: str, \
def archivePostsForPerson(httpPrefix: str,nickname: str,domain: str,baseDir: str, \
boxname: str,archiveDir: str,maxPostsInBox=256) -> None:
"""Retain a maximum number of posts within the given box
Move any others to an archive directory
@ -1007,7 +1007,7 @@ def archivePostsForPerson(nickname: str,domain: str,baseDir: str, \
if os.path.isfile(repliesPath):
os.rename(repliesPath,archivePath)
else:
deletePost(baseDir,nickname,domain,filePath,False)
deletePost(baseDir,httpPrefix,nickname,domain,filePath,False)
noOfPosts -= 1
if noOfPosts <= maxPostsInBox:
break

View File

@ -8,6 +8,7 @@ __status__ = "Production"
import os
import datetime
import commentjson
def getStatusNumber() -> (str,str):
"""Returns the status number and published date
@ -158,11 +159,15 @@ def removeAttachment(baseDir: str,httpPrefix: str,domain: str,postJson: {}):
os.remove(mediaFilename)
postJson['attachment']=[]
def deletePost(baseDir: str,nickname: str,domain: str,postFilename: str,debug: bool):
def deletePost(baseDir: str,httpPrefix: str,nickname: str,domain: str,postFilename: str,debug: bool):
"""Recursively deletes a post and its replies and attachments
"""
# remove any attachment
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
removeAttachment(baseDir,httpPrefix,domain,postJsonObject)
# remove any replies
repliesFilename=postFilename.replace('.json','.replies')
if os.path.isfile(repliesFilename):
if debug:
@ -173,6 +178,7 @@ def deletePost(baseDir: str,nickname: str,domain: str,postFilename: str,debug: b
if replyFile:
if os.path.isfile(replyFile):
deletePost(baseDir,nickname,domain,replyFile,debug)
# remove the replies file itself
# remove the replies file
os.remove(repliesFilename)
# finally, remove the post itself
os.remove(postFilename)