Documentation
¶
Overview ¶
Package webdavfs provides a WebDAV filesystem implementation for the absfs ecosystem.
Index ¶
- func NewServerFileSystem(fs absfs.FileSystem) webdav.FileSystem
- type AuthProvider
- type BasicAuth
- type BearerAuth
- type Config
- type ConfigError
- type File
- func (f *File) Close() error
- func (f *File) Name() string
- func (f *File) Read(b []byte) (int, error)
- func (f *File) ReadAt(b []byte, off int64) (int, error)
- func (f *File) ReadDir(n int) ([]iofs.DirEntry, error)
- func (f *File) Readdir(n int) ([]os.FileInfo, error)
- func (f *File) Readdirnames(n int) ([]string, error)
- func (f *File) Seek(offset int64, whence int) (int64, error)
- func (f *File) Stat() (os.FileInfo, error)
- func (f *File) Sync() error
- func (f *File) Truncate(size int64) error
- func (f *File) Write(b []byte) (int, error)
- func (f *File) WriteAt(b []byte, off int64) (int, error)
- func (f *File) WriteString(s string) (int, error)
- type FileClosedError
- type FileSystem
- func (fs *FileSystem) Chdir(dir string) error
- func (fs *FileSystem) Chmod(name string, mode os.FileMode) error
- func (fs *FileSystem) Chown(name string, uid, gid int) error
- func (fs *FileSystem) Chtimes(name string, atime time.Time, mtime time.Time) error
- func (fs *FileSystem) Close() error
- func (fs *FileSystem) Create(name string) (absfs.File, error)
- func (fs *FileSystem) Getwd() (string, error)
- func (fs *FileSystem) Mkdir(name string, perm os.FileMode) error
- func (fs *FileSystem) MkdirAll(name string, perm os.FileMode) error
- func (fs *FileSystem) Open(name string) (absfs.File, error)
- func (fs *FileSystem) OpenFile(name string, flag int, perm os.FileMode) (absfs.File, error)
- func (fs *FileSystem) ReadDir(name string) ([]iofs.DirEntry, error)
- func (fs *FileSystem) ReadFile(name string) ([]byte, error)
- func (fs *FileSystem) Remove(name string) error
- func (fs *FileSystem) RemoveAll(name string) error
- func (fs *FileSystem) Rename(oldpath, newpath string) error
- func (fs *FileSystem) Stat(name string) (os.FileInfo, error)
- func (fs *FileSystem) Sub(dir string) (iofs.FS, error)
- func (fs *FileSystem) TempDir() string
- func (fs *FileSystem) Truncate(name string, size int64) error
- func (fs *FileSystem) WriteFile(name string, data []byte, perm os.FileMode) error
- type InvalidSeekError
- type MultiAuth
- type Server
- type ServerConfig
- type ServerFile
- func (f *ServerFile) Close() error
- func (f *ServerFile) Read(p []byte) (int, error)
- func (f *ServerFile) Readdir(count int) ([]os.FileInfo, error)
- func (f *ServerFile) Seek(offset int64, whence int) (int64, error)
- func (f *ServerFile) Stat() (os.FileInfo, error)
- func (f *ServerFile) Write(p []byte) (int, error)
- type ServerFileSystem
- func (s *ServerFileSystem) Mkdir(ctx context.Context, name string, perm os.FileMode) error
- func (s *ServerFileSystem) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error)
- func (s *ServerFileSystem) RemoveAll(ctx context.Context, name string) error
- func (s *ServerFileSystem) Rename(ctx context.Context, oldName, newName string) error
- func (s *ServerFileSystem) Stat(ctx context.Context, name string) (os.FileInfo, error)
- type WebDAVError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewServerFileSystem ¶
func NewServerFileSystem(fs absfs.FileSystem) webdav.FileSystem
NewServerFileSystem creates a new WebDAV filesystem adapter that wraps the given absfs.FileSystem. The returned webdav.FileSystem can be used with golang.org/x/net/webdav.Handler to serve files via WebDAV protocol.
Types ¶
type AuthProvider ¶
type AuthProvider interface {
// Authenticate checks the request and returns true if authenticated.
// It may modify the response (e.g., send 401) if authentication fails.
Authenticate(w http.ResponseWriter, r *http.Request) bool
}
AuthProvider defines the interface for authentication.
type BasicAuth ¶
type BasicAuth struct {
// Realm is the authentication realm shown to the user.
Realm string
// Validator validates username/password combinations.
// Returns true if valid, false otherwise.
Validator func(username, password string) bool
}
BasicAuth implements HTTP Basic authentication.
func (*BasicAuth) Authenticate ¶
Authenticate implements AuthProvider for HTTP Basic authentication.
type BearerAuth ¶
type BearerAuth struct {
// Realm is the authentication realm shown to the user.
Realm string
// Validator validates the bearer token.
// Returns true if valid, false otherwise.
Validator func(token string) bool
}
BearerAuth implements HTTP Bearer token authentication.
func (*BearerAuth) Authenticate ¶
func (b *BearerAuth) Authenticate(w http.ResponseWriter, r *http.Request) bool
Authenticate implements AuthProvider for HTTP Bearer authentication.
type Config ¶
type Config struct {
// URL is the base URL of the WebDAV server (e.g., "https://webdav.example.com/remote.php/dav/files/user/")
URL string
// Username for HTTP authentication (optional)
Username string
// Password for HTTP authentication (optional)
Password string
// BearerToken for Bearer token authentication (optional, mutually exclusive with Username/Password)
BearerToken string
// HTTPClient allows customization of the HTTP client (optional)
// If nil, a default client with reasonable timeouts will be used
HTTPClient *http.Client
// Timeout for HTTP requests (default: 30 seconds)
Timeout time.Duration
// TempDir specifies the temporary directory path on the WebDAV server (optional)
// If empty, defaults to "/tmp"
TempDir string
}
Config holds the configuration for connecting to a WebDAV server.
type ConfigError ¶
ConfigError represents an error in the configuration
func (*ConfigError) Error ¶
func (e *ConfigError) Error() string
type File ¶
type File struct {
// contains filtered or unexported fields
}
File represents an open file in the WebDAV filesystem
func (*File) ReadDir ¶
ReadDir reads the contents of the directory and returns a slice of up to n DirEntry values in directory order. This is compatible with io/fs.ReadDirFile.
If n > 0, ReadDir returns at most n entries. In this case, if ReadDir returns an empty slice, it will return a non-nil error explaining why. At the end of a directory, the error is io.EOF.
If n <= 0, ReadDir returns all entries from the directory in a single slice. In this case, if ReadDir succeeds (reads all the way to the end of the directory), it returns the slice and a nil error.
func (*File) Readdirnames ¶
Readdirnames reads directory entry names
type FileClosedError ¶
type FileClosedError struct {
Path string
}
FileClosedError is returned when an operation is attempted on a closed file
func (*FileClosedError) Error ¶
func (e *FileClosedError) Error() string
type FileSystem ¶
type FileSystem struct {
// contains filtered or unexported fields
}
FileSystem implements the absfs.FileSystem interface for WebDAV servers
func (*FileSystem) Chdir ¶
func (fs *FileSystem) Chdir(dir string) error
Chdir changes the current working directory
func (*FileSystem) Chmod ¶
func (fs *FileSystem) Chmod(name string, mode os.FileMode) error
Chmod changes file permissions (limited WebDAV support)
func (*FileSystem) Chown ¶
func (fs *FileSystem) Chown(name string, uid, gid int) error
Chown changes file ownership (not supported by WebDAV)
func (*FileSystem) Close ¶
func (fs *FileSystem) Close() error
Close closes the filesystem connection
func (*FileSystem) Create ¶
func (fs *FileSystem) Create(name string) (absfs.File, error)
Create creates a new file for writing
func (*FileSystem) Getwd ¶
func (fs *FileSystem) Getwd() (string, error)
Getwd returns the current working directory
func (*FileSystem) Mkdir ¶
func (fs *FileSystem) Mkdir(name string, perm os.FileMode) error
Mkdir creates a directory
func (*FileSystem) MkdirAll ¶
func (fs *FileSystem) MkdirAll(name string, perm os.FileMode) error
MkdirAll creates a directory and all parent directories
func (*FileSystem) Open ¶
func (fs *FileSystem) Open(name string) (absfs.File, error)
Open opens a file for reading
func (*FileSystem) ReadDir ¶
func (fs *FileSystem) ReadDir(name string) ([]iofs.DirEntry, error)
ReadDir reads the named directory and returns a list of directory entries sorted by filename. This is compatible with io/fs.ReadDirFS.
func (*FileSystem) ReadFile ¶
func (fs *FileSystem) ReadFile(name string) ([]byte, error)
ReadFile reads the entire file and returns its contents. This method is compatible with io/fs.ReadFileFS.
func (*FileSystem) Remove ¶
func (fs *FileSystem) Remove(name string) error
Remove removes a file or empty directory
func (*FileSystem) RemoveAll ¶
func (fs *FileSystem) RemoveAll(name string) error
RemoveAll removes a path and all children
func (*FileSystem) Rename ¶
func (fs *FileSystem) Rename(oldpath, newpath string) error
Rename renames (moves) a file or directory
func (*FileSystem) Stat ¶
func (fs *FileSystem) Stat(name string) (os.FileInfo, error)
Stat returns file information
func (*FileSystem) Sub ¶
func (fs *FileSystem) Sub(dir string) (iofs.FS, error)
Sub returns an fs.FS corresponding to the subtree rooted at dir. This is compatible with io/fs.SubFS.
func (*FileSystem) TempDir ¶
func (fs *FileSystem) TempDir() string
TempDir returns the temporary directory path
type InvalidSeekError ¶
InvalidSeekError is returned when an invalid seek operation is attempted
func (*InvalidSeekError) Error ¶
func (e *InvalidSeekError) Error() string
type MultiAuth ¶
type MultiAuth struct {
Providers []AuthProvider
}
MultiAuth combines multiple authentication providers. Authentication succeeds if any provider succeeds.
func (*MultiAuth) Authenticate ¶
Authenticate implements AuthProvider by trying each provider in order. Returns true if any provider succeeds.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server provides a WebDAV server backed by any absfs.FileSystem. It implements http.Handler and can be used directly with http.ListenAndServe or integrated into existing HTTP routers.
func NewServer ¶
func NewServer(fs absfs.FileSystem, config *ServerConfig) *Server
NewServer creates a new WebDAV server for the given filesystem.
Example usage:
fs, _ := memfs.NewFS()
server := webdavfs.NewServer(fs, &webdavfs.ServerConfig{
Prefix: "/webdav",
Auth: &webdavfs.BasicAuth{
Realm: "My Server",
Validator: func(user, pass string) bool {
return user == "admin" && pass == "secret"
},
},
})
http.ListenAndServe(":8080", server)
func (*Server) Handler ¶
Handler returns the underlying http.Handler. Useful for wrapping with middleware.
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler. It handles WebDAV protocol methods (PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK) as well as standard HTTP methods (GET, PUT, DELETE, OPTIONS).
func (*Server) SetAuth ¶
func (s *Server) SetAuth(auth AuthProvider)
SetAuth updates the authentication provider.
type ServerConfig ¶
type ServerConfig struct {
// Prefix is the URL path prefix to strip from WebDAV resource paths.
// Example: "/webdav" means requests to "/webdav/file.txt" access "/file.txt"
Prefix string
// Auth is an optional authentication handler.
// If nil, no authentication is required.
Auth AuthProvider
// Logger is an optional request logger function.
// If nil, requests are not logged.
Logger func(r *http.Request, err error)
// LockSystem configures WebDAV locking behavior.
// If nil, a MemLS (in-memory lock system) is used.
LockSystem webdav.LockSystem
}
ServerConfig holds configuration for the WebDAV server.
type ServerFile ¶
type ServerFile struct {
// contains filtered or unexported fields
}
ServerFile adapts absfs.File to webdav.File. It wraps an absfs.File to provide the interface required by golang.org/x/net/webdav for serving files via WebDAV protocol.
func (*ServerFile) Read ¶
func (f *ServerFile) Read(p []byte) (int, error)
Read reads up to len(p) bytes into p.
func (*ServerFile) Readdir ¶
func (f *ServerFile) Readdir(count int) ([]os.FileInfo, error)
Readdir reads the contents of the directory and returns a slice of os.FileInfo values, as would be returned by Stat, in directory order.
func (*ServerFile) Seek ¶
func (f *ServerFile) Seek(offset int64, whence int) (int64, error)
Seek sets the offset for the next Read or Write.
type ServerFileSystem ¶
type ServerFileSystem struct {
// contains filtered or unexported fields
}
ServerFileSystem adapts absfs.FileSystem to webdav.FileSystem, allowing any absfs filesystem to be served via WebDAV.
func (*ServerFileSystem) Mkdir ¶
Mkdir creates a directory. The context parameter is accepted for interface compliance but not used.
func (*ServerFileSystem) OpenFile ¶
func (s *ServerFileSystem) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error)
OpenFile opens a file with the specified flags and permissions. The context parameter is accepted for interface compliance but not used.
func (*ServerFileSystem) RemoveAll ¶
func (s *ServerFileSystem) RemoveAll(ctx context.Context, name string) error
RemoveAll removes a file or directory tree. The context parameter is accepted for interface compliance but not used.
type WebDAVError ¶
WebDAVError represents an error from the WebDAV server
func (*WebDAVError) Error ¶
func (e *WebDAVError) Error() string