Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to specify the mime type for files matching a pattern #37

Open
wants to merge 1 commit into
base: py3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.")
Copy link
Contributor Author

@debbiedub debbiedub Nov 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot more to say about this but I don't think this is the place.

Among other things:

  • If no files match, the mime-type will be determined my fcp3/node.py guestMimeType(filename) that in turn uses mimetypes.guess_type(filename, False).
  • The order is significant, i.e. --mime-type-match=*.gif=image/gif --mime-type-match=databasefiles/*=text/plain will, for the file databasefiles/somedir/*.gif give the mime-type image/gif while --mime-type-match=databasefiles/*=text/plain --mime-type-match=*.gif=image/gif will give the mime-type text/plain for the same file name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add that information in a file in doc/? Maybe with an example how to use this to upload a repo for dgof? Maybe as doc/freesitemgr.org ? (in org-mode format — markdown would also work)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, yes. I will see if I understand the org-mode format. I will do that in another pull request.

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