From f029c07a61f84f8e887183a826df877fda92b86d Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 4 Dec 2019 18:52:27 +0000 Subject: [PATCH] Create etag at time of media upload --- content.py | 1 + daemon.py | 1 + media.py | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/content.py b/content.py index 1cc3912c..dd051ad5 100644 --- a/content.py +++ b/content.py @@ -471,6 +471,7 @@ def saveMediaInFormPOST(mediaBytes,debug: bool, \ fd = open(filename, 'wb') fd.write(mediaBytes[startPos:]) fd.close() + return filename,attachmentMediaType def extractTextFieldsInPOST(postBytes,boundary,debug: bool) -> {}: diff --git a/daemon.py b/daemon.py index e5b5b2be..be09f42d 100644 --- a/daemon.py +++ b/daemon.py @@ -3626,6 +3626,7 @@ class PubServer(BaseHTTPRequestHandler): if filename: if filename.endswith('.png') or \ filename.endswith('.jpg') or \ + filename.endswith('.webp') or \ filename.endswith('.gif'): if self.server.debug: print('DEBUG: POST media removing metadata') diff --git a/media.py b/media.py index efa08db4..82d3e96c 100644 --- a/media.py +++ b/media.py @@ -13,6 +13,7 @@ import os import sys import json import datetime +from hashlib import sha1 from auth import createPassword from shutil import copyfile from shutil import rmtree @@ -71,6 +72,36 @@ def getAttachmentMediaType(filename: str) -> str: return 'audio' return mediaType +def updateEtag(mediaFilename: str) -> None: + """ calculate the etag, which is a sha1 of the data + """ + # only create etags for media + if '/media/' not in mediaFilename: + return + + # check that the media exists + if not os.path.isfile(mediaFilename): + return + + # read the binary data + data=None + try: + with open(mediaFilename+, 'rb') as mediaFile: + data=mediaFile.read() + except: + pass + + if not data: + return + # calculate hash + etag=sha1(data).hexdigest() + # save the hash + try: + with open(mediaFilename+'.etag', 'w') as etagFile: + etagFile.write(etag) + except: + pass + def attachMedia(baseDir: str,httpPrefix: str,domain: str,port: int, \ postJson: {},imageFilename: str, \ mediaType: str,description: str, \ @@ -127,7 +158,8 @@ def attachMedia(baseDir: str,httpPrefix: str,domain: str,port: int, \ removeMetaData(imageFilename,mediaFilename) else: copyfile(imageFilename,mediaFilename) - + updateEtag(mediaFilename) + return postJson def archiveMedia(baseDir: str,archiveDirectory: str,maxWeeks=4) -> None: