#webdav #client #async #file-server

webdavc

异步 webdav 客户端。基于 rustydav https://docs.rs/rustydav 开发

2 个版本

0.1.1 2022年12月31日
0.1.0 2022年12月31日

#17 in #webdav

GPL-3.0 许可证

19KB
232 行代码

rustydav

异步
添加功能:将 parse_xml 解析功能添加到文件列表中,请参阅 file.rs
警告:此库深度定制,以满足个人读取 Synology 文件的需求

用法

use webdavc::client;
use webdavc::file::parse_xml;
use webdavc::prelude::*;

async fn scan() -> Result<(), String> {
    let webdav_client = client::Client::init("username", "pwd");
    let base_url = "https://yourwebdav.com";
    let resp = webdav_client
        .list(base_url, "1")
        .await
        .map_err(|e| e.to_string())?;
    let resp = resp
        .text()
        .await
        .map_err(|e| e.to_string())
        .and_then(|x| parse_xml(&x)).and_then(|x|
        // remove first element if any
        if x.len() > 0 {
            Ok(x[1..].to_vec())
        } else {
            Ok(x)
        }
        )?;
    println!("{:?}", resp);
    Ok(())
}

在 rust 中实现 webdav 请求

这是一个用 rust 编写的库,灵感来源于 hyperdav,并使用 reqwest 库作为基础。

此库可用于调用 webdav 服务器。

支持的方法包括

  • get
  • put
  • delete
  • unzip
  • mkcol
  • mv
  • list

变更日志

示例

如何使用此库的小示例

rustydav 作为依赖项添加

[dependencies]
rustydav = "0.1.3"

然后在您的代码中添加以下内容

extern crate rustydav;

use rustydav::client;
use rustydav::prelude::*;

调用方法的简短示例

// Every method will return a Result<Response, Error>

if (result.is_ok() {
    // the method completed with success
} else {
    // somenting when wrong
}

// Create the client
let webdav_client = client::Client::init(/*username*/, /*password*/);

// Get some file from server
// The result will contain the file data
let result = webdav_client.get(/*absolute url to the server file location*/);

// Upload a file to server. It can be any type of file as long as it is transformed to a vector of bytes (Vec<u8>).
// This can be achieved with std::fs::File or zip-rs for sending zip files.
let result = webdav_client.put(/*Vec<u8>*/, /*absolute path to the server file location*/);

// Delete a remote file from the server
let result = webdav_client.delete(/*absolute path to the file on the server*/);

// Unzip a zip archive on the server
let result = webdav_client.unzip(/*absolute path to the zip archive on the server*/);

// Create a new directory on server
let result = webdav_client.mkcol(/*absolute path to the server where to create the new folder*/);

// Rename or move a file / folder / zip on the server
// If the file location changes it will move the file, if only the file name changes it will rename it.
let result = webdav_client.mv(/*absolute path on the server for old file location/name*/, /*absolute on the server for new file location/name*/);

// List files and folders at the given path on the server
// Depth of "0" applies only to the resource, "1" to the resource and it's children, "infinity" to the resource and all it's children recursively
// The result will contain an xml list with the remote folder contents.
let result = webdav_client.list(/*absolute path on the server to list the files*/, /*depth being "0", "1" or "infinity"*/);

有关它们的更多描述,请参阅 client.rs 文件。

依赖项

~4–16MB
~233K SLoC