File size: 3,970 Bytes
fcb1da1
d9c2e0e
 
 
ba33dfe
 
 
 
 
 
 
 
 
 
 
 
d9c2e0e
5eff00e
 
 
 
 
d9c2e0e
 
 
 
 
 
 
 
 
 
 
 
 
 
ba33dfe
 
 
 
 
 
 
 
 
d9c2e0e
 
ba33dfe
 
 
 
 
 
 
 
e4b6492
 
 
 
ba33dfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
824a418
d9c2e0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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()