/// <summary> /// Binds, validates and normalizes WebDAV Context configuration. /// </summary> /// <param name="configurationSection">Instance of <see cref="IConfigurationSection"/>.</param> /// <param name="config">WebDAV Context configuration.</param> /// <param name="env">Instance of <see cref="IWebHostEnvironment"/>.</param> public static async Task ReadConfigurationAsync(this IConfigurationSection configurationSection, DavContextConfig config, IWebHostEnvironment env) { if (configurationSection == null) { throw new ArgumentNullException("configurationSection"); } configurationSection.Bind(config); if (string.IsNullOrEmpty(config.RepositoryPath)) { throw new ArgumentNullException("DavContext.RepositoryPath"); } if (!Path.IsPathRooted(config.RepositoryPath)) { config.RepositoryPath = Path.GetFullPath(Path.Combine(env.ContentRootPath, config.RepositoryPath)); } if (!(string.IsNullOrEmpty(config.AttrStoragePath) || Path.IsPathRooted(config.AttrStoragePath))) { config.AttrStoragePath = Path.GetFullPath(Path.Combine(env.ContentRootPath, config.AttrStoragePath)); } if (!string.IsNullOrEmpty(config.AttrStoragePath)) { FileSystemInfoExtension.UseFileSystemAttribute(new FileSystemExtendedAttribute(config.AttrStoragePath, config.RepositoryPath)); } else if (!await new DirectoryInfo(config.RepositoryPath).IsExtendedAttributesSupportedAsync()) { var tempPath = Path.Combine(Path.GetTempPath(), System.Reflection.Assembly.GetExecutingAssembly().GetName().Name); FileSystemInfoExtension.UseFileSystemAttribute(new FileSystemExtendedAttribute(tempPath, config.RepositoryPath)); } }
/// <summary> /// Initializes a new instance of the DavContext class. /// </summary> /// <param name="httpContext"><see cref="HttpContext"/> instance.</param> public DavContext(HttpContext httpContext) : base(httpContext) { Logger = WebDAVServer.FileSystemStorage.AspNet.Logger.Instance; RepositoryPath = ConfigurationManager.AppSettings["RepositoryPath"] ?? string.Empty; bool isRoot = new DirectoryInfo(RepositoryPath).Parent == null; string configRepositoryPath = isRoot ? RepositoryPath : RepositoryPath.TrimEnd(Path.DirectorySeparatorChar); RepositoryPath = configRepositoryPath.StartsWith("~") ? HttpContext.Current.Server.MapPath(configRepositoryPath) : configRepositoryPath; string attrStoragePath = (ConfigurationManager.AppSettings["AttrStoragePath"] ?? string.Empty).TrimEnd(Path.DirectorySeparatorChar); attrStoragePath = attrStoragePath.StartsWith("~") ? HttpContext.Current.Server.MapPath(attrStoragePath) : attrStoragePath; if (!FileSystemInfoExtension.IsUsingFileSystemAttribute) { if (!string.IsNullOrEmpty(attrStoragePath)) { FileSystemInfoExtension.UseFileSystemAttribute(new FileSystemExtendedAttribute(attrStoragePath, this.RepositoryPath)); } else if (!(new DirectoryInfo(RepositoryPath).IsExtendedAttributesSupported())) { var tempPath = Path.Combine(Path.GetTempPath(), System.Reflection.Assembly.GetExecutingAssembly().GetName().Name); FileSystemInfoExtension.UseFileSystemAttribute(new FileSystemExtendedAttribute(tempPath, this.RepositoryPath)); } } if (!Directory.Exists(RepositoryPath)) { WebDAVServer.FileSystemStorage.AspNet.Logger.Instance.LogError("Repository path specified in Web.config is invalid.", null); } }
static void Init() { string contentRootPath = Directory.GetParent(Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath)).FullName; string logPath = Path.Combine(contentRootPath, @"App_Data\WebDav\Logs"); logger.LogFile = Path.Combine(logPath, "WebDAVlog.txt"); logger.IsDebugEnabled = debugLoggingEnabled; webDavEngine = new DavEngineAsync { Logger = logger // Use idented responses if debug logging is enabled. , OutputXmlFormatting = true }; /// This license lile is used to activate: /// - IT Hit WebDAV Server Engine for .NET /// - IT Hit iCalendar and vCard Library if used in a project string license = File.ReadAllText(Path.Combine(contentRootPath, "License.lic")); webDavEngine.License = license; /// This license file is used to activate G Suite Documents Editing for IT Hit WebDAV Server string gSuiteLicense = File.Exists(Path.Combine(contentRootPath, "GSuiteLicense.lic")) ? File.ReadAllText(Path.Combine(contentRootPath, "GSuiteLicense.lic")) : string.Empty; if (!string.IsNullOrEmpty(gSuiteLicense)) { gSuiteEngine = new GSuiteEngineAsync(googleServiceAccountID, googleServicePrivateKey, googleNotificationsRelativeUrl) { License = gSuiteLicense, Logger = logger }; } // Set custom handler to process GET and HEAD requests to folders and display // info about how to connect to server. We are using the same custom handler // class (but different instances) here to process both GET and HEAD because // these requests are very similar. Some WebDAV clients may fail to connect if HEAD // request is not processed. MyCustomGetHandler handlerGet = new MyCustomGetHandler(contentRootPath); MyCustomGetHandler handlerHead = new MyCustomGetHandler(contentRootPath); handlerGet.OriginalHandler = webDavEngine.RegisterMethodHandler("GET", handlerGet); handlerHead.OriginalHandler = webDavEngine.RegisterMethodHandler("HEAD", handlerHead); string attrStoragePath = (ConfigurationManager.AppSettings["AttrStoragePath"] ?? string.Empty).TrimEnd(Path.DirectorySeparatorChar); if (!string.IsNullOrEmpty(attrStoragePath)) { FileSystemInfoExtension.UseFileSystemAttribute(new FileSystemExtendedAttribute(attrStoragePath, repositoryPath)); } else if (!(new DirectoryInfo(repositoryPath).IsExtendedAttributesSupported())) { var tempPath = Path.Combine(Path.GetTempPath(), System.Reflection.Assembly.GetExecutingAssembly().GetName().Name); FileSystemInfoExtension.UseFileSystemAttribute(new FileSystemExtendedAttribute(tempPath, repositoryPath)); } }
/// <summary> /// Binds, validates and normalizes WebDAV Context configuration options. /// </summary> /// <param name="configurationSection">Instance of <see cref="IConfigurationSection"/>.</param> /// <param name="options">WebDAV Context configuration options.</param> /// <param name="env">Instance of <see cref="IHostingEnvironment"/>.</param> public static async Task ReadOptionsAsync(this IConfigurationSection configurationSection, DavContextOptions options, IHostingEnvironment env) { if (configurationSection == null) { throw new ArgumentNullException("configurationSection"); } configurationSection.Bind(options); if (string.IsNullOrEmpty(options.RepositoryPath)) { throw new ArgumentNullException("DavContextOptions.RepositoryPath"); } if (!Path.IsPathRooted(options.RepositoryPath)) { options.RepositoryPath = Path.GetFullPath(Path.Combine(env.ContentRootPath, options.RepositoryPath)); } if (!Directory.Exists(options.RepositoryPath)) { throw new DirectoryNotFoundException(string.Format("DavContextOptions.RepositoryPath specified in appsettings.webdav.json is invalid: '{0}'.", options.RepositoryPath)); } if (!(string.IsNullOrEmpty(options.AttrStoragePath) || Path.IsPathRooted(options.AttrStoragePath))) { options.AttrStoragePath = Path.GetFullPath(Path.Combine(env.ContentRootPath, options.AttrStoragePath)); } if (!string.IsNullOrEmpty(options.AttrStoragePath)) { FileSystemInfoExtension.UseFileSystemAttribute(new FileSystemExtendedAttribute(options.AttrStoragePath, options.RepositoryPath)); } else if (!await new DirectoryInfo(options.RepositoryPath).IsExtendedAttributesSupportedAsync()) { var tempPath = Path.Combine(Path.GetTempPath(), System.Reflection.Assembly.GetExecutingAssembly().GetName().Name); FileSystemInfoExtension.UseFileSystemAttribute(new FileSystemExtendedAttribute(tempPath, options.RepositoryPath)); } }