gdriver

package module
v0.0.0-...-5ce0243 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2023 License: MIT Imports: 10 Imported by: 2

README

GDriver

A golang implementation to access google drive by using traditional file-folder-path pattern.

    f, _ := os.Open("image1.jpeg")
    gdrive.PutFile("Holidays/image1.jpeg", f)
    gdrive.Delete("Pictures/Old Apartment Images")

Example

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsExist

func IsExist(e error) bool

IsExist returns true if the error is an FileExistError

func IsNotExist

func IsNotExist(e error) bool

IsNotExist returns true if the error is an FileNotExistError

Types

type CallbackError

type CallbackError struct {
	NestedError error
}

CallbackError will be returned if the callback returned an error

func (CallbackError) Error

func (e CallbackError) Error() string

type File

type File interface {
	Info() *FileInfo
	Write([]byte) (int, error)
	Read([]byte) (int, error)
	Close() error
}

type FileExistError

type FileExistError struct {
	Path string
}

FileExistError will be thrown if an file exists

func (FileExistError) Error

func (e FileExistError) Error() string

type FileInfo

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

FileInfo represents file information for a file or directory

func (*FileInfo) CreationTime

func (i *FileInfo) CreationTime() time.Time

CreationTime returns the time when this file was created

func (*FileInfo) DriveFile

func (i *FileInfo) DriveFile() *drive.File

DriveFile returns the underlaying drive.File

func (*FileInfo) IsDir

func (i *FileInfo) IsDir() bool

IsDir returns true if this file is a directory

func (*FileInfo) ModifiedTime

func (i *FileInfo) ModifiedTime() time.Time

ModifiedTime returns the time when this file was modified

func (*FileInfo) Name

func (i *FileInfo) Name() string

Name returns the name of the file or directory

func (*FileInfo) ParentPath

func (i *FileInfo) ParentPath() string

ParentPath returns the parent path of the file or directory

func (*FileInfo) Path

func (i *FileInfo) Path() string

Path returns the full path to this file or directory

func (*FileInfo) Size

func (i *FileInfo) Size() int64

Size returns the bytes for this file

type FileIsDirectoryError

type FileIsDirectoryError struct {
	Path string
}

FileIsDirectoryError will be thrown if a file is a directory

func (FileIsDirectoryError) Error

func (e FileIsDirectoryError) Error() string

type FileIsNotDirectoryError

type FileIsNotDirectoryError struct {
	Path string
}

FileIsNotDirectoryError will be thrown if a file is not a directory

func (FileIsNotDirectoryError) Error

func (e FileIsNotDirectoryError) Error() string

type FileNotExistError

type FileNotExistError struct {
	Path string
}

FileNotExistError will be thrown if an file was not found

func (FileNotExistError) Error

func (e FileNotExistError) Error() string

type GDriver

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

GDriver can be used to access google drive in a traditional file-folder-path pattern

func New

func New(client *http.Client, opts ...Option) (*GDriver, error)

New creates a new Google Drive Driver, client must me an authenticated instance for google drive

func (*GDriver) Delete

func (d *GDriver) Delete(path string) error

Delete will delete a file or directory, if directory it will also delete its descendants

func (*GDriver) DeleteDirectory

func (d *GDriver) DeleteDirectory(path string) error

DeleteDirectory will delete a directory and its descendants

func (*GDriver) GetFile

func (d *GDriver) GetFile(path string) (*FileInfo, io.ReadCloser, error)

GetFile gets a file and returns a ReadCloser that can consume the body of the file

func (*GDriver) GetFileHash

func (d *GDriver) GetFileHash(path string, method HashMethod) (*FileInfo, []byte, error)

GetFileHash returns the hash of a file with the present method

func (*GDriver) ListDirectory

func (d *GDriver) ListDirectory(path string, fileFunc func(*FileInfo) error) error

ListDirectory will get all contents of a directory, calling fileFunc with the collected file information

func (*GDriver) ListTrash

func (d *GDriver) ListTrash(filePath string, fileFunc func(f *FileInfo) error) error

ListTrash lists the contents of the trash, if you specify directories it will only list the trash contents of the specified directories

func (*GDriver) MakeDirectory

func (d *GDriver) MakeDirectory(path string) (*FileInfo, error)

MakeDirectory creates a directory for the specified path, it will create non existent directores automatically

Examples:

MakeDirectory("Pictures/Holidays") // will create Pictures and Holidays

func (*GDriver) Move

func (d *GDriver) Move(oldPath, newPath string) (*FileInfo, error)

Move moves a file or directory to a new path, note that move also renames the target if necessary and creates non existing directories

Examples:

Move("Folder1/File1", "Folder2/File2") // File1 in Folder1 will be moved to Folder2/File2
Move("Folder1/File1", "Folder2/File1") // File1 in Folder1 will be moved to Folder2/File1

func (*GDriver) Open

func (d *GDriver) Open(path string, flag OpenFlag) (File, error)

Open opens a file in the traditional os.Open way

func (*GDriver) PutFile

func (d *GDriver) PutFile(filePath string, r io.Reader) (*FileInfo, error)

PutFile uploads a file to the specified path it creates non existing directories

func (*GDriver) Rename

func (d *GDriver) Rename(path string, newName string) (*FileInfo, error)

Rename renames a file or directory to a new name in the same folder

func (*GDriver) SetRootDirectory

func (d *GDriver) SetRootDirectory(path string) (*FileInfo, error)

SetRootDirectory changes the working root directory use this if you want to do certian operations in a special directory path should always be the absolute real path

func (*GDriver) Stat

func (d *GDriver) Stat(path string) (*FileInfo, error)

Stat gives a FileInfo for a file or directory

func (*GDriver) Trash

func (d *GDriver) Trash(path string) error

Trash trashes a file or directory

type HashMethod

type HashMethod int

HashMethod is the hashing method to use for GetFileHash

const (
	// HashMethodMD5 sets the method to MD5
	HashMethodMD5 HashMethod = 0
)

type OpenFlag

type OpenFlag int
const (
	O_RDONLY OpenFlag = 1 << iota
	O_WRONLY OpenFlag = 1 << iota
	O_CREATE OpenFlag = 1 << iota
)

type Option

type Option func(driver *GDriver) error

Option can be used to pass optional Options to GDriver

func RootDirectory

func RootDirectory(path string) Option

RootDirectory sets the root directory for all operations

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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