Jellyfin.Plugin.Webdav/WebDavClientService.cs
2025-04-21 15:04:10 +02:00

62 lines
2 KiB
C#

/*
* Jellyfin.Plugin.Webdav
* Copyright (C) 2025 Jellyfin contributors
* Licensed under GPLv3
*/
namespace Jellyfin.Plugin.Webdav
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Jellyfin.Plugin.Webdav.Configuration;
using WebDav;
/// <summary>
/// Service for interacting with WebDAV endpoints.
/// </summary>
public sealed class WebDavClientService
{
private readonly PluginConfiguration _config;
private readonly WebDavClient _client;
/// <summary>
/// Initializes a new instance of the <see cref="WebDavClientService"/> class.
/// </summary>
/// <param name="config">Plugin configuration.</param>
public WebDavClientService(PluginConfiguration config)
{
_config = config;
var parameters = new WebDavClientParams
{
BaseAddress = new Uri(_config.BaseUrl.TrimEnd('/')),
Credentials = new NetworkCredential(_config.Username, _config.Password),
PreAuthenticate = true
};
_client = new WebDavClient(parameters);
}
/// <summary>
/// Lists resources at the specified WebDAV path.
/// </summary>
/// <param name="path">Remote path.</param>
/// <returns>A collection of WebDAV resources.</returns>
public async Task<IEnumerable<dynamic>> ListAsync(string path)
{
var response = await _client.Propfind(path).ConfigureAwait(false);
return response.Resources;
}
/// <summary>
/// Gets a raw file stream from the WebDAV endpoint.
/// </summary>
/// <param name="path">Remote file path.</param>
/// <returns>A stream for the remote file.</returns>
public async Task<Stream> GetStreamAsync(string path)
{
var response = await _client.GetRawFile(path).ConfigureAwait(false);
return response.Stream;
}
}
}