fn build()

in core/src/services/webdav/backend.rs [141:227]


    fn build(self) -> Result<impl Access> {
        debug!("backend build started: {:?}", &self);

        let endpoint = match &self.config.endpoint {
            Some(v) => v,
            None => {
                return Err(Error::new(ErrorKind::ConfigInvalid, "endpoint is empty")
                    .with_context("service", Scheme::Webdav));
            }
        };
        // Some services might return the path with suffix `/remote.php/webdav/`, we need to trim them.
        let server_path = http::Uri::from_str(endpoint)
            .map_err(|err| {
                Error::new(ErrorKind::ConfigInvalid, "endpoint is invalid")
                    .with_context("service", Scheme::Webdav)
                    .set_source(err)
            })?
            .path()
            .trim_end_matches('/')
            .to_string();

        let root = normalize_root(&self.config.root.clone().unwrap_or_default());
        debug!("backend use root {}", root);

        let mut authorization = None;
        if let Some(username) = &self.config.username {
            authorization = Some(format_authorization_by_basic(
                username,
                self.config.password.as_deref().unwrap_or_default(),
            )?);
        }
        if let Some(token) = &self.config.token {
            authorization = Some(format_authorization_by_bearer(token)?)
        }

        let core = Arc::new(WebdavCore {
            info: {
                let am = AccessorInfo::default();
                am.set_scheme(Scheme::Webdav)
                    .set_root(&root)
                    .set_native_capability(Capability {
                        stat: true,
                        stat_has_content_length: true,
                        stat_has_content_type: true,
                        stat_has_etag: true,
                        stat_has_last_modified: true,

                        read: true,

                        write: true,
                        write_can_empty: true,

                        create_dir: true,
                        delete: true,

                        copy: !self.config.disable_copy,

                        rename: true,

                        list: true,
                        list_has_content_length: true,
                        list_has_content_type: true,
                        list_has_etag: true,
                        list_has_last_modified: true,

                        // We already support recursive list but some details still need to polish.
                        // list_with_recursive: true,
                        shared: true,

                        ..Default::default()
                    });

                // allow deprecated api here for compatibility
                #[allow(deprecated)]
                if let Some(client) = self.http_client {
                    am.update_http_client(|_| client);
                }

                am.into()
            },
            endpoint: endpoint.to_string(),
            server_path,
            authorization,
            root,
        });
        Ok(WebdavBackend { core })
    }