gonedrive

package module
v0.0.0-...-80eb22d Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2025 License: MIT Imports: 16 Imported by: 0

README

This package is abandoned

Use github.com/rclone/rclone instead

gonedrive

Small OneDrive library for Go

Made for practice, and for use in another personal project. May/may not be updated further down the road.

Documentation

Index

Constants

View Source
const (
	ConflictBehaviour_Rename  = ConflictBehaviour("rename")
	ConflictBehaviour_Fail    = ConflictBehaviour("fail")
	ConflictBehaviour_Replace = ConflictBehaviour("replace")
)

Variables

View Source
var ErrSyncLocalDirectory = errors.New("local file is directory")
View Source
var ErrSyncRemoteDirectory = errors.New("remote file is directory")

Functions

func EndpointPath

func EndpointPath(path string, endpoint string, query ...string) string

Helper function, creates URL from path and query.

func EndpointQuery

func EndpointQuery(query ...string) (o string)

func EndpointSelect

func EndpointSelect(names ...string) string

Builds the "$select" query string from a list of wanted results

func MakeRequest

func MakeRequest[T any](t *GraphToken, method string, url string, requestBody io.Reader) (*T, error)

Utility function for making requests. You supply the method, the endpoint (/me/drive/*) and the request body. Returns error responses from the API as a go error. Deserializes the result to whatever you want, using json.Unmarshal.

func SendRequest

func SendRequest[T any](t *GraphToken, request *http.Request) (*T, error)

Types

type ConflictBehaviour

type ConflictBehaviour string

type DriveItem

type DriveItem struct {
	Id           string `json:"id"`
	Name         string `json:"name"`
	Description  string `json:"description"`
	Size         int64  `json:"size"`
	WebURL       string `json:"webUrl"`
	Ctag         string `json:"cTag"`
	Etag         string `json:"eTag"`
	CreationDate string `json:"createdDateTime"`
	ModifiedDate string `json:"lastModifiedDateTime"`

	Root        *struct{} `json:"root"`
	DownloadURL string    `json:"@content.downloadUrl"`

	Audio *struct {
		Album             string `json:"album"`
		AlbumArtist       string `json:"albumArtist"`
		Artist            string `json:"Artist"`
		Bitrate           int    `json:"bitrate"`
		Composers         string `json:"composers"`
		Copyright         string `json:"copyright"`
		Disc              int    `json:"disc"`
		DiscCount         int    `json:"discCount"`
		Duration          int    `json:"duration"`
		Genre             string `json:"genre"`
		HasDRM            bool   `json:"hasDrm"`
		IsVariableBitrate bool   `json:"isVariableBitrate"`
		Title             string `json:"title"`
		Track             int    `json:"track"`
		TrackCount        int    `json:"trackCount"`
		Year              int    `json:"year"`
	} `json:"audio"`

	File *struct {
		MimeType string  `json:"mimeType"`
		Hashes   *Hashes `json:"hashes"`
	} `json:"file"`

	Folder *struct {
		ChildCount int `json:"childCount"`
		View       *struct {
			SortBy    string `json:"sortBy"`
			SortOrder string `json:"sortOrder"`
			ViewType  string `json:"viewType"`
		} `json:"view"`
	} `json:"folder"`
}

func (*DriveItem) IsDir

func (item *DriveItem) IsDir() bool

type ErrorResponse

type ErrorResponse struct {
	Outer struct {
		Code    string `json:"code"`
		Message string `json:"message"`
		Inner   struct {
			Date            string `json:"date"`
			RequestID       string `json:"request-id"`
			ClientRequestId string `json:"client-request-id"`
		} `json:"innerError"`
	} `json:"error"`
}

func (*ErrorResponse) Error

func (err *ErrorResponse) Error() string

type GraphToken

type GraphToken struct {
	TokenType    string `json:"token_type"`
	ExpiresIn    int    `json:"expires_in"`
	Scope        string `json:"scope"`
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	// contains filtered or unexported fields
}

func CreateAccess

func CreateAccess(clientID string, redirectURI string, refresh *GraphToken) (*GraphToken, error)

Main entrypoint for the GoneDrive package. With an access token, you can do pretty much everything you want in the API. The token passed in to be refreshed should be replaced after this call.

It is recommended to store and refresh tokens, rather than generating new tokens every time your program runs.

func (*GraphToken) BuildRequest

func (t *GraphToken) BuildRequest(method string, endpoint string, body io.Reader) (*http.Request, error)

Builds a request object. You supply the method, the endpoint (/me/drive/*) and the request body. Authorization is taken care of when the request is returned.

func (*GraphToken) BuildRequestRaw

func (t *GraphToken) BuildRequestRaw(method string, uri string, body io.Reader) (*http.Request, error)

Builds a request object. Authorization is taken care of when the request is returned.

func (*GraphToken) CreateUploadSession

func (t *GraphToken) CreateUploadSession(destPath string, params UploadSessionParams) (*UploadSessionResponse, error)

func (*GraphToken) DownloadDriveItem

func (t *GraphToken) DownloadDriveItem(item *DriveItem) (io.ReadCloser, error)

Downloads a DriveItem, and returns the file body. It is the responsibility of the caller to close the resulting reader.

func (*GraphToken) GetDriveItem

func (t *GraphToken) GetDriveItem(path string, query ...string) (*DriveItem, error)

Get information about a single drive item

func (*GraphToken) GetDriveItemChildren

func (t *GraphToken) GetDriveItemChildren(path string, query []string) (*ResponsePaginated[[]*DriveItem], error)

Runs a query to get all DriveItems within a folder. This returns the paginated response structure. Path should be WITHOUT leading/trailing slashes.

func (*GraphToken) ListFolder

func (t *GraphToken) ListFolder(path string) ([]*DriveItem, error)

Lists all files in a given folder. Path should be WITHOUT leading/trailing slashes.

func (*GraphToken) MakeRequest

func (t *GraphToken) MakeRequest(method string, url string, body io.Reader, contentType ...string) (*http.Response, error)

Utility function for making requests. You supply the method, the endpoint (/me/drive/*) and the request body. Returns error responses from the API as a go error.

Only the first value of contentType is used, the rest are ignored.

func (*GraphToken) SendRequest

func (t *GraphToken) SendRequest(request *http.Request) (*http.Response, error)

Sends a request object to the MS graph API. Returns error responses from the API as a go error.

func (*GraphToken) SyncFolder

func (t *GraphToken) SyncFolder(remotePath string, localPath string, filterFn SyncFilterFn, eventFn SyncEventFn) error

A read-only sync of a given OneDrive folder. Downloads files that don't exist in local directory. Deletes files in local directory not found on OneDrive. Does not redownload existing (up-to-date) files.

func (*GraphToken) UploadContent

func (t *GraphToken) UploadContent(r io.Reader, size int64, destPath string, params UploadSessionParams) (*DriveItem, error)

func (*GraphToken) UploadFile

func (t *GraphToken) UploadFile(file fs.File, destPath string, conflictBehaviour ConflictBehaviour) (*DriveItem, error)

type Hashes

type Hashes struct {
	Crc32    string `json:"crc32Hash"`
	Sha1     string `json:"sha1Hash"`
	Sha256   string `json:"sha256Hash"`
	QuickXor string `json:"quickXorHash"`
}

type ResponsePaginated

type ResponsePaginated[T any] struct {
	Context  string `json:"@odata.context"`
	Count    int    `json:"@odata.count"`
	NextLink string `json:"@odata.nextLink"`
	Value    T      `json:"value"`
}

type SyncEvent

type SyncEvent interface {
	fmt.Stringer
}

type SyncEventBegin

type SyncEventBegin struct {
	LocalPath  string
	RemotePath string
	IsUpload   bool
}

Begin upload/download. This event is sent whenever a download or upload is started. A corrosponding SyncEventEnd event will also be sent eventually. While the file is uploading/downloading, any number of SyncEventProgress events.

func (SyncEventBegin) String

func (event SyncEventBegin) String() string

type SyncEventDelete

type SyncEventDelete struct {
	LocalPath string
}

Deleted local file.

func (SyncEventDelete) String

func (event SyncEventDelete) String() string

type SyncEventEnd

type SyncEventEnd struct {
	LocalPath  string
	RemotePath string
	IsUpload   bool
	Success    bool
}

End upload/download. Once this event has been sent, no more events will be sent for this file.

func (SyncEventEnd) String

func (event SyncEventEnd) String() string

type SyncEventError

type SyncEventError struct {
	LocalPath  string
	RemotePath string
	Err        error
}

Error

func (SyncEventError) String

func (event SyncEventError) String() string

type SyncEventFn

type SyncEventFn func(event SyncEvent)

type SyncEventProgress

type SyncEventProgress struct {
	LocalPath  string
	RemotePath string
	IsUpload   bool
	Size       int64
	Progress   int64
}

Progress event.

func (SyncEventProgress) String

func (event SyncEventProgress) String() string

type SyncEventSkip

type SyncEventSkip struct {
	LocalPath  string
	RemotePath string
}

Skipped downloading file.

func (SyncEventSkip) String

func (event SyncEventSkip) String() string

type SyncFile

type SyncFile struct {
	// Full file path
	FileName string

	// File size, not relevant for directories
	Size int64

	// Is this a directory?
	IsDir bool
}

type SyncFilterFn

type SyncFilterFn func(file SyncFile) bool

type UploadSessionParams

type UploadSessionParams struct {
	ConflictBehaviour ConflictBehaviour
	CreatedAt         *time.Time
	AccessedAt        *time.Time
	ModifiedAt        *time.Time
}

func (*UploadSessionParams) MarshalJSON

func (p *UploadSessionParams) MarshalJSON() ([]byte, error)

type UploadSessionResponse

type UploadSessionResponse struct {
	UploadURL          string   `json:"uploadUrl"`
	Expiration         string   `json:"expirationDateTime"`
	NextExpectedRanges []string `json:"nextExpectedRanges"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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