Create etag at time of media upload

merge-requests/30/head
Bob Mottram 2019-12-04 18:52:27 +00:00
parent 085ea620b8
commit f029c07a61
3 changed files with 35 additions and 1 deletions

View File

@ -471,6 +471,7 @@ def saveMediaInFormPOST(mediaBytes,debug: bool, \
fd = open(filename, 'wb') fd = open(filename, 'wb')
fd.write(mediaBytes[startPos:]) fd.write(mediaBytes[startPos:])
fd.close() fd.close()
return filename,attachmentMediaType return filename,attachmentMediaType
def extractTextFieldsInPOST(postBytes,boundary,debug: bool) -> {}: def extractTextFieldsInPOST(postBytes,boundary,debug: bool) -> {}:

View File

@ -3626,6 +3626,7 @@ class PubServer(BaseHTTPRequestHandler):
if filename: if filename:
if filename.endswith('.png') or \ if filename.endswith('.png') or \
filename.endswith('.jpg') or \ filename.endswith('.jpg') or \
filename.endswith('.webp') or \
filename.endswith('.gif'): filename.endswith('.gif'):
if self.server.debug: if self.server.debug:
print('DEBUG: POST media removing metadata') print('DEBUG: POST media removing metadata')

View File

@ -13,6 +13,7 @@ import os
import sys import sys
import json import json
import datetime import datetime
from hashlib import sha1
from auth import createPassword from auth import createPassword
from shutil import copyfile from shutil import copyfile
from shutil import rmtree from shutil import rmtree
@ -71,6 +72,36 @@ def getAttachmentMediaType(filename: str) -> str:
return 'audio' return 'audio'
return mediaType 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, \ def attachMedia(baseDir: str,httpPrefix: str,domain: str,port: int, \
postJson: {},imageFilename: str, \ postJson: {},imageFilename: str, \
mediaType: str,description: str, \ mediaType: str,description: str, \
@ -127,7 +158,8 @@ def attachMedia(baseDir: str,httpPrefix: str,domain: str,port: int, \
removeMetaData(imageFilename,mediaFilename) removeMetaData(imageFilename,mediaFilename)
else: else:
copyfile(imageFilename,mediaFilename) copyfile(imageFilename,mediaFilename)
updateEtag(mediaFilename)
return postJson return postJson
def archiveMedia(baseDir: str,archiveDirectory: str,maxWeeks=4) -> None: def archiveMedia(baseDir: str,archiveDirectory: str,maxWeeks=4) -> None: