Skip to content

Commit

Permalink
Add option to specify the mime type for files matching a pattern
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
debbiedub authored and ArneBab committed Nov 16, 2023
1 parent 145fc7a commit 58b5999
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
25 changes: 23 additions & 2 deletions fcp3/sitemgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -173,6 +184,7 @@ def load(self):
noInsert=self.noInsert,
chkCalcNode=self.chkCalcNode,
mtype=self.mtype,
mimeTypeMatch=self.mimeTypeMatch,
)
self.sites.append(site)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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'] = ''
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions freesitemgr
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -357,6 +361,7 @@ def main():
"priority", "cron",
"chk-calculation-node=", "max-manifest-size=",
"version", "index=", "mime-type=",
"mime-type-match=",
]
)
except getopt.GetoptError:
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 58b5999

Please sign in to comment.