webdav / app.py
LPX55's picture
Update app.py
824a418 verified
import os
from cheroot import wsgi
from wsgidav.wsgidav_app import WsgiDAVApp
from wsgidav.fs_dav_provider import FilesystemProvider
# from wsgidav.mw.debug_filter import WsgiDavDebugFilter
from wsgidav.dir_browser import WsgiDavDirBrowser
from wsgidav.error_printer import ErrorPrinter
from wsgidav.http_authenticator import HTTPAuthenticator
# from wsgidav.mw.impersonator import Impersonator
from wsgidav.mw.cors import Cors
from wsgidav.request_resolver import RequestResolver
DEFAULT_VERBOSE = 3
DEFAULT_LOGGER_DATE_FORMAT = "%H:%M:%S"
DEFAULT_LOGGER_FORMAT = "%(asctime)s.%(msecs)03d - %(levelname)-8s: %(message)s"
curr_path = os.path.dirname(os.path.realpath(__file__))
print(f"CP: {curr_path}")
print(f"CWD: {os.getcwd()}")
files_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "files")
if not os.path.exists(files_dir):
os.makedirs(files_dir)
# Create a sample.txt file
with open(os.path.join(files_dir, "sample.txt"), "w") as f:
f.write("This is a sample file for the WebDAV server.")
print(f"Created 'files' directory with sample.txt at {files_dir}")
# Configuration
config = {
"provider_mapping": {
"/": FilesystemProvider(files_dir)
},
"middleware_stack": [
# WsgiDavDebugFilter,
Cors,
ErrorPrinter,
HTTPAuthenticator,
# Impersonator,
WsgiDavDirBrowser, # configured under dir_browser option (see below)
RequestResolver, # this must be the last middleware item
],
"http_authenticator": {
"domain_controller": None,
"accept_basic": True, # Allow basic authentication, True or False
"accept_digest": True, # Allow digest authentication, True or False
"default_to_digest": True, # True (default digest) or False (default basic)
# Name of a header field that will be accepted as authorized user
"trusted_auth_header": None,
},
"simple_dc": {"user_mapping": {
"*": {
"user": {
"password": "hunter123",
"roles": ["editor", "admin"]
}
}
}},
"verbose": DEFAULT_VERBOSE,
"logging": {
"enable": True, # True: activate 'wsgidav' logger (in library mode)
"logger_date_format": DEFAULT_LOGGER_DATE_FORMAT,
"logger_format": DEFAULT_LOGGER_FORMAT,
"enable_loggers": [],
"debug_methods": [],
},
"dir_browser": {
"enable": True, # Render HTML listing for GET requests on collections
# Add a trailing slash to directory URLs (by generating a 301 redirect):
"directory_slash": True,
# List of fnmatch patterns:
"ignore": [
".DS_Store", # macOS folder meta data
"._*", # macOS hidden data files
"Thumbs.db", # Windows image previews
],
"icon": True,
"response_trailer": True, # Raw HTML code, appended as footer (True: use a default)
"show_user": True, # Show authenticated user an realm
# Send <dm:mount> response if request URL contains '?davmount' (rfc4709)
"davmount": True,
# Add 'Mount' link at the top
"davmount_links": False,
"ms_sharepoint_support": False, # Invoke MS Office documents for editing using WebDAV
"libre_office_support": False, # Invoke Libre Office documents for editing using WebDAV
# The path to the directory that contains template.html and associated assets.
# The default is the htdocs directory within the dir_browser directory.
"htdocs_path": "/home/user/app/dir_browser/htdocs",
},
}
# Create the WSGI application
app = WsgiDAVApp(config)
# Create and start the server
server_addr = "0.0.0.0"
server_port = 7860
server = wsgi.Server((server_addr, server_port), app)
print(f"Starting WebDAV server at http://{server_addr}:{server_port}")
try:
server.start()
except KeyboardInterrupt:
print("Stopping server...")
server.stop()