import os
import shutil
import ssl
+import stat
import systemd.journal
import tempfile
import urllib.parse
return os.path.exists(path)
+ @run_in_thread
+ def stat(self, path, fmt=None):
+ """
+ stat()s a file
+ """
+ # Make the path absolute
+ if not path.startswith("/"):
+ path = self.path(path)
+
+ # Stat the file
+ s = os.stat(path)
+
+ # Check if the format matches
+ if fmt:
+ # Return nothing if the format does not match
+ if not stat.S_IFMT(s.st_mode) == fmt:
+ return
+
+ return s
+
@run_in_thread
def makedirs(self, path, **kwargs):
"""
#!/usr/bin/python
+import datetime
import logging
+import stat
import tornado.web
from . import base
@base.ratelimit(limit=100, minutes=60, key="downloads")
async def get(self, path):
# Check if the file exists
- if not await self.backend.exists(path):
+ if not await self.backend.stat(path, stat.S_IFREG):
raise tornado.web.HTTPError(404)
# Fetch all mirrors for this client
self.redirect(url)
+ @base.ratelimit(limit=100, minutes=60, key="downloads")
+ async def head(self, path):
+ # Stat the file
+ s = await self.backend.stat(path, stat.S_IFREG)
+
+ # Send 404 if the file does not exist
+ if not s:
+ raise tornado.web.HTTPError(404)
+
+ # Send a couple of headers
+ self.set_header("Content-Type", "application/octet-stream")
+ self.set_header("Content-Length", s.st_size)
+ self.set_header("Last-Modified", datetime.datetime.fromtimestamp(s.st_mtime))
+
async def write_error(self, *args, **kwargs):
"""
Don't send any body in error responses