【问题标题】:Create "File-Drop (Upload Only)" share in NextCloud using WebDav使用 WebDav 在 NextCloud 中创建“File-Drop (Upload Only)”共享
【发布时间】:2020-05-19 06:24:34
【问题描述】:

使用 NextCloud,我想知道是否可以使用 WebDav 创建“FileDrop(仅上传)”共享。我在 NextClouds 网站和 WebDav RFC 规范中都找不到有关此的任何文档。这是 Nextcloud 网络界面上的一项功能。

我正在使用 .NET 和 WebDav 客户端库

【问题讨论】:

    标签: asp.net nextcloud


    【解决方案1】:

    我找到了自己的答案。把它贴在这里以防有人偶然发现它。

    要使用 API 在 Nextcloud 上共享文件夹,您需要使用 OCS Share API 而不是 WebDav OCS Sharing API

    需要创建权限设置为“Create == 4, shareType == 3”的共享

    编辑: 我写这段代码已经有一段时间了,但我希望它仍然有效。没有测试它,因为最后我没有在我的项目中使用代码。

     public class NextCloud
    {
        private INextCloudConfig _config;
        private HttpClient _client;
        public NextCloud(INextCloudConfig config, string userName, string password)
        {
            _config = config;
            var credentials = Encoding.ASCII.GetBytes(userName + ":" + password);
            _client = new HttpClient();
            _client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentials));
    
        }
    
        public async Task<HttpResponseMessage> CreateFolderAsync(string path)
        {
            HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("MKCOL"), _config.WebDavAddress + "/" + path);
            HttpResponseMessage response = await _client.SendAsync(request);
            return response;
        }
    
        public async Task<HttpResponseMessage> CreateShareAsync(string path, ShareType shareType, Permissions permissions)
        {
            HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("POST"), new Uri(_config.OCSAddress + "/shares?format=json"));
            request.Headers.Add("OCS-APIRequest", "true");
    
            var parameters = new List<KeyValuePair<string, string>>();
            parameters.Add(new KeyValuePair<string, string>("shareType", ((int)shareType).ToString()));
            parameters.Add(new KeyValuePair<string, string>("shareWith", null));
            parameters.Add(new KeyValuePair<string, string>("path", path));
            parameters.Add(new KeyValuePair<string, string>("permissions", ((int)permissions).ToString()));
            //parameters.Add(new KeyValuePair<string, string>("publicUpload", "true"));
            
            request.Content = new FormUrlEncodedContent(parameters);
            HttpResponseMessage response = await _client.SendAsync(request);
    
            var responseContent = response.Content.ReadAsStringAsync().Result;
            var json = JsonDocument.Parse(responseContent);
    
            return response;
        }
    }
    
    public interface INextCloudConfig
    {
        string WebDavAddress { get; }
        string OCSAddress { get; }
    }
    
    public class NextCloudConfig : INextCloudConfig
    {
        private const string DEFAULTWEBDAVSUFFIX = "remote.php/webdav/";
        private const string DEFAULTOCSSUFFIX = "ocs/v2.php/apps/files_sharing/api/v1/";
    
        private static string _baseAddress = "";
        private static string _webdavSuffix = "";
        private static string _ocsSuffix = "";
    
        public string WebDavAddress
        {
            get => _baseAddress + "/" + _webdavSuffix;
        }
    
        public string OCSAddress
        {
            get => _baseAddress + "/" + _ocsSuffix;
        }
    
        public NextCloudConfig(string baseAddress) : this(baseAddress, DEFAULTWEBDAVSUFFIX, DEFAULTOCSSUFFIX)
        {
            
        }
    
        public NextCloudConfig(string baseAddress, string webdavSuffix, string ocsSuffix)
        {
            _baseAddress = baseAddress.TrimEnd('/');
            _webdavSuffix = webdavSuffix.TrimEnd('/');
            _ocsSuffix = ocsSuffix.TrimEnd('/');
        }
    
    }
    
    public enum Permissions
    {
        Read = 1,
        Update = 2,
        Create = 4, 
        Delete = 8,
        Share = 16,
        All = 31
    }
    
    public enum ShareType
    {
        User = 0,
        Group = 1,
        PublicLink = 3
    }
    
    static void Main(string[] args)
    {
        Run().Wait();
    }
    
    public static async Task Run()
    {
        NextCloudConfig config = new NextCloudConfig("https://url.to.nexcloudinstance");
        NextCloud nc = new NextCloud(config, "john.doe@gmail.com", "passw0rd");
        await nc.CreateFolderAsync(path);
        await nc.CreateShareAsync(path, ShareType.PublicLink, Permissions.Create);
    
        Console.ReadLine();
     
    }
    

    这是我的测试项目中的代码。这是一个简单的命令提示符应用程序。我希望这对你有帮助。我发布的链接仍然有效,并提供了有关不同枚举的更多信息。

    【讨论】:

    • 你能详细说明一下你是怎么做到的吗
    • @Fuseteam :查看我的编辑
    • 谢谢!我会检查我是否可以使用这个