webdav

package module
v0.0.0-...-3f60e73 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2026 License: MIT Imports: 11 Imported by: 0

README

WebDAV Backend for Afero

Go Report Card GoDoc

About

It provides an afero filesystem implementation of a WebDAV backend.

This implementation uses gowebdav as the underlying WebDAV client library.

We are open to any improvement through issues or pull-request that might lead to a better implementation or even better testing.

Known limitations

  • File appending / seeking for write is not supported
  • Chtimes is not supported
  • Chmod support is very limited (WebDAV doesn't have standard permission model)

How to use

Note: Errors handling is skipped for brevity but a complete example is provided in the example folder

package main

import (
	"io"
	"log"
	"os"

	webdavfs "github.com/looplj/afero-webdav"
)

var (
	url      = "http://localhost:6065/dav"
	user     = "admin"
	password = "admin"
	key      = "/path/to/file.txt"
)

func main() {
	// Initialize the file system
	fs := webdavfs.NewFs(url, user, password)

	// And do your thing
	src, _ := fs.Open(key)
	defer src.Close()

	var out = os.Stdout
	n, _ := io.Copy(out, src)

	log.Printf("copied %d bytes", n)
}

Development

The project uses Taskfile to orchestrate the local development flow.

go install github.com/go-task/task/v3/cmd/task@latest

Install task, and then use

# see the available dev tasks
task --list

To run the test suite (requires a WebDAV server running):

# Start WebDAV server
docker-compose -f contrib/docker/docker-compose.yml up -d

# Run tests
task test

To run the example code:

task run-example -- --help

Thanks

This project is based on contiamo/afero-s3 and adapted for WebDAV.

Documentation

Overview

Package webdav brings WebDAV files handling to afero

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyOpened = errors.New("already opened")

ErrAlreadyOpened is returned when the file is already opened.

View Source
var ErrInvalidSeek = errors.New("invalid seek offset")

ErrInvalidSeek is returned when the seek operation is not doable.

View Source
var ErrNotImplemented = errors.New("not implemented")

ErrNotImplemented is returned when this operation is not (yet) implemented.

View Source
var ErrNotSupported = errors.New("webdav doesn't support this operation")

ErrNotSupported is returned when this operations is not supported by WebDAV.

Functions

This section is empty.

Types

type File

type File struct {
	// contains filtered or unexported fields
}

File represents a file in WebDAV.

func NewFile

func NewFile(fs *Fs, name string) *File

NewFile initializes a File object.

func (*File) Close

func (f *File) Close() error

Close closes the File.

func (*File) Name

func (f *File) Name() string

Name returns the filename.

func (*File) Read

func (f *File) Read(p []byte) (int, error)

Read reads up to len(b) bytes from the File.

func (*File) ReadAt

func (f *File) ReadAt(p []byte, off int64) (n int, err error)

ReadAt reads len(p) bytes from the file starting at byte offset off.

func (*File) Readdir

func (f *File) Readdir(n int) ([]os.FileInfo, error)

Readdir reads the contents of the directory associated with file.

func (*File) Readdirnames

func (f *File) Readdirnames(n int) ([]string, error)

Readdirnames reads and returns a slice of names from the directory f.

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (int64, error)

Seek sets the offset for the next Read or Write on file.

func (*File) Stat

func (f *File) Stat() (os.FileInfo, error)

Stat returns the FileInfo structure describing file.

func (*File) Sync

func (f *File) Sync() error

Sync flushes the file to the WebDAV server.

func (*File) Truncate

func (f *File) Truncate(int64) error

Truncate changes the size of the file.

func (*File) Write

func (f *File) Write(p []byte) (int, error)

Write writes len(b) bytes to the File.

func (*File) WriteAt

func (f *File) WriteAt(p []byte, off int64) (n int, err error)

WriteAt writes len(p) bytes to the file starting at byte offset off.

func (*File) WriteString

func (f *File) WriteString(s string) (int, error)

WriteString is like Write, but writes the contents of string s.

type FileInfo

type FileInfo struct {
	// contains filtered or unexported fields
}

FileInfo implements os.FileInfo for a file in WebDAV.

func NewFileInfo

func NewFileInfo(name string, directory bool, sizeInBytes int64, modTime time.Time) FileInfo

NewFileInfo creates file info.

func (FileInfo) IsDir

func (fi FileInfo) IsDir() bool

IsDir provides the abbreviation for Mode().IsDir().

func (FileInfo) ModTime

func (fi FileInfo) ModTime() time.Time

ModTime provides the last modification time.

func (FileInfo) Mode

func (fi FileInfo) Mode() os.FileMode

Mode provides the file mode bits. For a file in WebDAV this defaults to 664 for files, 755 for directories.

func (FileInfo) Name

func (fi FileInfo) Name() string

Name provides the base name of the file.

func (FileInfo) Size

func (fi FileInfo) Size() int64

Size provides the length in bytes for a file.

func (FileInfo) Sys

func (fi FileInfo) Sys() interface{}

Sys provides the underlying data source (can return nil).

type Fs

type Fs struct {
	// contains filtered or unexported fields
}

Fs is an FS object backed by WebDAV.

func NewFs

func NewFs(url, user, password string) *Fs

NewFs creates a new Fs instance from WebDAV server URL and credentials.

func NewFsFromClient

func NewFsFromClient(client *gowebdav.Client) *Fs

NewFsFromClient creates a new Fs instance from an existing WebDAV client.

func (*Fs) Chmod

func (fs *Fs) Chmod(name string, mode os.FileMode) error

Chmod is not fully supported by WebDAV, but we accept the call without error.

func (Fs) Chown

func (Fs) Chown(string, int, int) error

Chown is not supported by WebDAV.

func (Fs) Chtimes

func (Fs) Chtimes(string, time.Time, time.Time) error

Chtimes is not supported by WebDAV.

func (*Fs) Create

func (fs *Fs) Create(name string) (afero.File, error)

Create a file.

func (*Fs) Mkdir

func (fs *Fs) Mkdir(name string, perm os.FileMode) error

Mkdir makes a directory.

func (*Fs) MkdirAll

func (fs *Fs) MkdirAll(name string, perm os.FileMode) error

MkdirAll creates a directory and all parent directories if necessary.

func (*Fs) Name

func (*Fs) Name() string

Name returns the type of FS object this is: Fs.

func (*Fs) Open

func (fs *Fs) Open(name string) (afero.File, error)

Open a file for reading.

func (*Fs) OpenFile

func (fs *Fs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error)

OpenFile opens a file.

func (*Fs) Remove

func (fs *Fs) Remove(name string) error

Remove a file.

func (*Fs) RemoveAll

func (fs *Fs) RemoveAll(name string) error

RemoveAll removes a path and all its children.

func (*Fs) Rename

func (fs *Fs) Rename(oldname, newname string) error

Rename a file.

func (*Fs) Stat

func (fs *Fs) Stat(name string) (os.FileInfo, error)

Stat returns a FileInfo describing the named file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL