From 9c0bc8c20d1630c1c94d52d3038e5828894eb9f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Debora=20W=C3=B6pcke?= Date: Sun, 5 Nov 2023 11:41:13 +0100 Subject: [PATCH] Add option to specify the mime type for files matching a pattern The option is saved per added site. First time that this version is used, the option is saved for all sites. This is not different from how other options are handled but could be confusing because on subsequent updates, the option is ignored and the value is set for all sites in your config-dir, even the ones you do not update. --- fcp3/sitemgr.py | 25 +++++++++++++++++++++++-- freesitemgr | 14 ++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/fcp3/sitemgr.py b/fcp3/sitemgr.py index c130e86..0374a9d 100644 --- a/fcp3/sitemgr.py +++ b/fcp3/sitemgr.py @@ -24,7 +24,17 @@ #@+others #@+node:imports -import sys, os, os.path, io, threading, traceback, pprint, time, stat, json +import fnmatch +import io +import json +import os +import os.path +import pprint +import stat +import sys +import threading +import time +import traceback import fcp3 as fcp from fcp3 import CRITICAL, ERROR, INFO, DETAIL, DEBUG #, NOISY @@ -96,6 +106,7 @@ def __init__(self, *args, **kw): self.index = kw.get('index', 'index.html') self.sitemap = kw.get('index', 'sitemap.html') self.mtype = kw.get('mtype', 'text/html') + self.mimeTypeMatch= kw.get('mimeTypeMatch', []) self.name = "freesitemgr-" + "--".join(args) # To decide whether to upload index and activelink as part of @@ -173,6 +184,7 @@ def load(self): noInsert=self.noInsert, chkCalcNode=self.chkCalcNode, mtype=self.mtype, + mimeTypeMatch=self.mimeTypeMatch, ) self.sites.append(site) @@ -244,6 +256,7 @@ def addSite(self, **kw): index=self.index, sitemap=self.sitemap, mtype=self.mtype, + mimeTypeMatch=self.mimeTypeMatch, **kw) self.sites.append(site) @@ -489,6 +502,7 @@ def __init__(self, **kw): self.index = kw.get('index', 'index.html') self.sitemap = kw.get('sitemap', 'sitemap.html') self.mtype = kw.get('mtype', 'text/html') + self.mimeTypeMatch = kw.get('mimeTypeMatch', []) #print "Verbosity=%s" % self.Verbosity @@ -684,6 +698,7 @@ def writeVars(comment="", tail="", **kw): writeVars(index=self.index) writeVars(sitemap=self.sitemap) writeVars(mtype=self.mtype) + writeVars(mimeTypeMatch=self.mimeTypeMatch) w("\n") # we should not save generated files. @@ -1121,6 +1136,10 @@ def scan(self): rec['path'] = f['fullpath'].decode(enc) rec['name'] = f['relpath'].decode(enc) rec['mimetype'] = f['mimetype'] + for patternType in reversed(self.mimeTypeMatch): + for pattern, mimetype in patternType.items(): + if fnmatch.fnmatch(rec['name'], pattern): + rec['mimetype'] = mimetype rec['hash'] = hashFile(rec['path']) rec['sizebytes'] = getFileSize(rec['path']) rec['uri'] = '' @@ -1165,10 +1184,12 @@ def scan(self): # known file - see if changed knownrec = self.filesDict[name] if (knownrec['state'] in ('changed', 'waiting') - or knownrec['hash'] != rec['hash']): + or knownrec['hash'] != rec['hash'] + or knownrec['mimetype'] != rec['mimetype']): # flag an update log(DETAIL, "scan: file %s has changed" % name) knownrec['hash'] = rec['hash'] + knownrec['mimetype'] = rec['mimetype'] knownrec['sizebytes'] = rec['sizebytes'] knownrec['state'] = 'changed' structureChanged = True diff --git a/freesitemgr b/freesitemgr index cc3cb8c..e8d6e8b 100755 --- a/freesitemgr +++ b/freesitemgr @@ -271,6 +271,10 @@ def help(): print(" - index file (default is index.html)") print(" -m, --mime-type") print(" - mime-type of the index file (default is \"text/html\")") + print(" --mime-type-match=PATTERN=MIME/TYPE") + print(" Set mime-type for files matching PATTERN. Can be given") + print(" multiple times to match distinct patterns.") + print(" This option is only effective when doing add.") print(" -l, --logfile=filename") print(" - location of logfile (default %s)" % logFile) print(" -r, --priority") @@ -357,6 +361,7 @@ def main(): "priority", "cron", "chk-calculation-node=", "max-manifest-size=", "version", "index=", "mime-type=", + "mime-type-match=", ] ) except getopt.GetoptError: @@ -398,6 +403,15 @@ def main(): if o in ("-m", "--mime-type"): opts['mtype'] = a + if o in ("--mime-type-match"): + try: + pattern, mimeType = a.split('=') + except: + usage("--mime-type-match must have PATTERN=MIMETYPE parameter") + if 'mimeTypeMatch' not in opts: + opts['mimeTypeMatch'] = [] + opts['mimeTypeMatch'] += [ { pattern:mimeType, }, ] + if o == '--max-manifest-size': opts['maxManifestSizeBytes'] = int(float(a)*1024)