dropbox

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2021 License: MIT Imports: 10 Imported by: 5 modules, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Dropbox Backend for Afero

Go version Build codecov Go Report Card GoDoc

About

It provides an afero filesystem implementation of a Dropbox backend.

This was created to provide a backend to the ftpserver but can definitely be used in any other code.

I'm very opened to any improvement through issues or pull-request that might lead to a better implementation or even better testing.

It is implemented on top of the Dropbox Go SDK.

Key points

  • Download & upload file streaming
  • Some coverage (all APIs are tested, but not all errors are reproduced)
  • Very carefully linted

Known limitations

  • File appending / seeking for write is not supported because dropbox doesn't support it
  • Chmod / Chtimes are not supported because dropbox doesn't support it

How to use

Note: Errors handling is skipped for brevity, but you definitely have to handle it.

package main

import(
	"os"
	
	dropbox "github.com/fclairamb/afero-dropbox"
)

func main() {
  // You access an FS with it's token
  fs := dropbox.NewFs(os.Getenv("DROPBOX_TOKEN"))
  
  // And do your thing
  file, _ := fs.OpenFile("file.txt", os.O_WRONLY, 0777)
  file.WriteString("Hello world !")
  file.Close()
}

Documentation

Overview

Package dropbox provides an afero implementation to the dropbox API

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 ErrNotSupported = errors.New("dropbox doesn't support this operation")

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

Functions

This section is empty.

Types

type File

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

File represents a file structure.

func (*File) Close

func (f *File) Close() error

Close closes the File, rendering it unusable for I/O. It returns an error, if any.

func (*File) Name

func (f *File) Name() string

Name returns the file name.

func (*File) Read

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

Read reads up to len(b) bytes from the File. It returns the number of bytes read and an error, if any. EOF is signaled by a zero count with err set to io.EOF.

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. It returns the number of bytes read and the error, if any. ReadAt always returns a non-nil error when n < len(b). At end of file, that error is io.EOF.

func (*File) Readdir

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

Readdir lists all the files of a directory. Unfortunately the dropbox API doesn't allow to limit the number of returned files per call. so what we're doing here is to using a channel a temporary buffer.

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 to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any. The behavior of Seek on a file opened with O_APPEND is not specified.

func (*File) Stat

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

Stat fetches the file stat with a cache.

func (*File) Sync

func (f *File) Sync() error

Sync doesn't do anything.

func (*File) Truncate

func (f *File) Truncate(size int64) error

Truncate should truncate a file to a specific size but isn't supported by dropbox.

func (*File) Write

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

Write writes len(b) bytes to the File. It returns the number of bytes written and an error, if any. Write returns a non-nil error when n != len(b).

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. It returns the number of bytes written and an error, if any. WriteAt returns a non-nil error when n != len(p).

func (*File) WriteString

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

WriteString writes a string.

type FileInfo

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

FileInfo is dropbox file description.

func (FileInfo) IsDir

func (f FileInfo) IsDir() bool

IsDir returns if it's a directory.

func (FileInfo) ModTime

func (f FileInfo) ModTime() time.Time

ModTime returns the modification time.

func (FileInfo) Mode

func (f FileInfo) Mode() os.FileMode

Mode return the file mode.

func (FileInfo) Name

func (f FileInfo) Name() string

Name returns the file name.

func (FileInfo) Size

func (f FileInfo) Size() int64

Size returns the file size.

func (FileInfo) Sys

func (f FileInfo) Sys() interface{}

Sys returns the underlying structure.

type Fs

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

Fs is the dropbox filesystem.

func NewFs

func NewFs(token string) *Fs

NewFs creates new dropbox FS instance.

func (*Fs) Chmod

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

Chmod is not supported.

func (*Fs) Chown

func (fs *Fs) Chown(name string, uid int, gid int) error

Chown is not supported.

func (*Fs) Chtimes

func (fs *Fs) Chtimes(name string, _ time.Time, mtime time.Time) error

Chtimes is not supported because dropbox doesn't support simply changing a time.

func (*Fs) Create

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

Create creates a file. This implementation respects the afero specs but is super slow because it will always open the file two times.

func (*Fs) Mkdir

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

Mkdir creates 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 *Fs) Name() string

Name of the fs: dropbox.

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, _ os.FileMode) (afero.File, error)

OpenFile opens a file.

func (*Fs) Remove

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

Remove removes a file.

func (*Fs) RemoveAll

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

RemoveAll removes all files inside a directory.

func (*Fs) Rename

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

Rename renames a file.

func (*Fs) SetRootDirectory

func (fs *Fs) SetRootDirectory(fullPath string)

SetRootDirectory defines a base directory This is mostly useful to isolate tests and can most probably forgotten for most use-cases.

func (*Fs) Stat

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

Stat fetches the file info.

Jump to

Keyboard shortcuts

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