forked from seba/servefile
Extract method DirListingHandler._appendToListing (so we can re-use it)
This commit is contained in:
parent
5374315e76
commit
4452d86498
41
servefile
41
servefile
|
@ -334,6 +334,27 @@ class DirListingHandler(FileBaseHandler):
|
||||||
self.send_response(404)
|
self.send_response(404)
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
|
|
||||||
|
def _appendToListing(self, content, item, itemPath, stat, is_dir):
|
||||||
|
# Strings to display on directory listing
|
||||||
|
lastModifiedDate = datetime.datetime.fromtimestamp(stat.st_mtime)
|
||||||
|
lastModified = lastModifiedDate.strftime("%Y-%m-%d %H:%M")
|
||||||
|
fileSize = "%.1f%s" % self.convertSize(stat.st_size)
|
||||||
|
(fileType, _) = mimetypes.guess_type(itemPath)
|
||||||
|
if not fileType:
|
||||||
|
fileType = "-"
|
||||||
|
|
||||||
|
if is_dir:
|
||||||
|
item += "/"
|
||||||
|
fileType = "Directory"
|
||||||
|
content.append("""
|
||||||
|
<tr>
|
||||||
|
<td class="name"><a href="%s">%s</a></td>
|
||||||
|
<td class="last-modified">%s</td>
|
||||||
|
<td class="size">%s</td>
|
||||||
|
<td class="type">%s</td>
|
||||||
|
</tr>
|
||||||
|
""" % (urllib.quote(item), item, lastModified, fileSize, fileType))
|
||||||
|
|
||||||
def sendDirectoryListing(self, path, head):
|
def sendDirectoryListing(self, path, head):
|
||||||
""" Generate a directorylisting for path and send it """
|
""" Generate a directorylisting for path and send it """
|
||||||
header = """<!DOCTYPE html>
|
header = """<!DOCTYPE html>
|
||||||
|
@ -385,25 +406,7 @@ class DirListingHandler(FileBaseHandler):
|
||||||
except IOError:
|
except IOError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Strings to display on directory listing
|
self._appendToListing(content, item, itemPath, stat, os.path.isdir(itemPath))
|
||||||
lastModifiedDate = datetime.datetime.fromtimestamp(stat.st_mtime)
|
|
||||||
lastModified = lastModifiedDate.strftime("%Y-%m-%d %H:%M")
|
|
||||||
fileSize = "%.1f%s" % self.convertSize(stat.st_size)
|
|
||||||
(fileType, _) = mimetypes.guess_type(itemPath)
|
|
||||||
if not fileType:
|
|
||||||
fileType = "-"
|
|
||||||
|
|
||||||
if os.path.isdir(itemPath):
|
|
||||||
item += "/"
|
|
||||||
fileType = "Directory"
|
|
||||||
content.append("""
|
|
||||||
<tr>
|
|
||||||
<td class="name"><a href="%s">%s</a></td>
|
|
||||||
<td class="last-modified">%s</td>
|
|
||||||
<td class="size">%s</td>
|
|
||||||
<td class="type">%s</td>
|
|
||||||
</tr>
|
|
||||||
""" % (urllib.quote(item), item, lastModified, fileSize, fileType))
|
|
||||||
|
|
||||||
listing = header + "\n".join(content) + footer
|
listing = header + "\n".join(content) + footer
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue