downthedrive

package module
v0.0.0-...-eb1ac4b Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2017 License: MIT Imports: 17 Imported by: 0

README

downthedrive

I like Microsoft OneDrive as a cheap an fast solution well integrated to store my data in the cloud.

With this project I've explored some possible integrations with my personal onedrive by using OneDrive API

DownTheDrive is a library and a commandline utility to perform some nice operations on my personal onedrive.

Status of the project

it's a prerelease under development. Actually is exposes the drive only in read

TODO LIST
  • LOCALSTORE: parameterize localstore (type,..) in the cmd actions
  • LOCALSTORE: Implement SHA1 based
  • LOCALSTORE: Clean
  • HTTP functionalities
  • Authentication improvement (save token, logout...)
  • obfuscation/encryption
  • incremental updates (by using cTag or by /view.delta)
  • clean deleted items
  • ??? read/write linux fs ???

Installation

go get github.com/mysinmyc/downthedrive/cmd/downthedrive

Some concepts

DowntTheDrive high level archtecture

configuration

DownTheDrive configuration is mainly stored in a json file at the following location

  • windows: %APPDATA%\.downthedrive\config.json
  • linux: $HOME/.downthedrive/config.json

The configuration file constains parameters to connect to a database The database can contains additional configuration informations in the table beans (also authentication informations)

index db

Depending on -indexStrategy parameter, all the items exposed to the user are cached in a table named ITEMS. This table allows to speed up drive navigation.

By default items are cached for 15 days (for infrequently changes).

To modify index behaviours:

  • it could be forced an update by using command downthedrive index
  • start the program with the parameter -indexStrategy 2 to access directly to the drive without local indexes

By default indexdb is a sqlite3 file under the .downthedrive folder. SqlLite3 can generate issues in case of concurrent read/write activity

To switch to mysql modify .downthedrive/config.json file as follows

        "Db": {
                "DataSource": "{user}:{password}@tcp({host}:3306)/downthedrive?parseTime=true",
                "Driver": "mysql"
        },

local store

Before to provide to the user, files are downloaded/cached into a folder called local store. By default it is located in .downthedrive/localStore; it can be changed by using parameter -localStorePath {localStorePath}

How to

Authentication

Downthedrive requires a permanent access token to the user drive. At the time it requires only read access to the drive. Authentication has been developed to be executed on a browser located in a different machine (in case the utility runs on a server without a browser)

To Perform authentication, clientId and a clientSecrect are require. They can be obtained by following the instruction at dev.onedrive.com. Registered application must:

Authentication can be started by executing

downthedrive authenticate -clientId {clientId} -clientSecret {clientSecret}

During the authentication will be asked to copy and paste an url to a browser and then to paste back the url containing the token from the browser in order to reedim it

** WARNING: currently client secret and authentication token are stored as plain text in the database **

Index the drive

To index a path tree inside the database use the following command:

downthedrive index -drivePath {drivePath}

specify / as drivePath to index the entire drive

Download files from drive

To download a folder locally from the drive type the following command:

downthedrive download -drivePath {drivePath} -localStorePath {local path where to store items}

Navigate the drive content via a local website

To start an httpd execute the following command

downthedrive httpd

by default httpd is listening on localhost:8080; to change it specify parameter *-listen {listenAddess} *

Mount a readonly filesystem (available only in linux)

In Linux downthedrive can mount a local filesystem that exposes the drive.

This option requires fuse installed locally

To mount the drive execute:

downthedrive mount -drivePath {drivePath} -mountPoint {mountpoint}

mountpoint must exists and must be writeable by the user

By default root and others cannot access the mounted filesytem except if -allowOther true poptions is passed to downthedrive. It can be usefull in some uses cases like filesystem export via samba

To umount the filesystem use the command `downthedrive unmount -mountPoint {mountPoint}

External dependencies

This project depends directly on the following external projects, I thanks to the authors

Before using it please check license compatibility for your use cases

Documentation

Index

Constants

View Source
const (
	TABLE_ITEMS                 = "items"
	FIELD_ITEMS_ID              = "id"
	FIELD_ITEMS_PATH            = "path"
	FIELD_ITEMS_NAME            = "name"
	FIELD_ITEMS_PARENTID        = "parent_id"
	FIELD_ITEMS_PARENTPATH      = "parent_path"
	FIELD_ITEMS_SNAPSHOT_DATE   = "snapshot_date"
	FIELD_ITEMS_CREATETIMESTAMP = "create_timestamp"
	FIELD_ITEMS_UPDATETIMESTAMP = "update_timestamp"
	FIELD_ITEMS_FOLDERCHILDREN  = "folder_children"
	FIELD_ITEMS_SIZEBYTES       = "size_bytes"
	FIELD_ITEMS_HASH_SHA1       = "hash_sha1"

	DDL_ITEMS_SQLITE = "create table if not exists " + TABLE_ITEMS + " (" +
		FIELD_ITEMS_ID + " text PRIMARY KEY " +
		"," + FIELD_ITEMS_NAME + " text " +
		"," + FIELD_ITEMS_SNAPSHOT_DATE + " timestamp " +
		"," + FIELD_ITEMS_CREATETIMESTAMP + " timestamp " +
		"," + FIELD_ITEMS_UPDATETIMESTAMP + " timestamp " +
		"," + FIELD_ITEMS_FOLDERCHILDREN + " integer " +
		"," + FIELD_ITEMS_SIZEBYTES + " integer " +
		"," + FIELD_ITEMS_PATH + " text " +
		"," + FIELD_ITEMS_PARENTID + " text " +
		"," + FIELD_ITEMS_PARENTPATH + " text" +
		"," + FIELD_ITEMS_HASH_SHA1 + " text" +
		")"

	DDL_ITEMS_MYSQL = "create table if not exists " + TABLE_ITEMS + " (" +
		FIELD_ITEMS_ID + " varchar(700) PRIMARY KEY " +
		"," + FIELD_ITEMS_NAME + " varchar(1000) " +
		"," + FIELD_ITEMS_SNAPSHOT_DATE + " timestamp " +
		"," + FIELD_ITEMS_CREATETIMESTAMP + " timestamp " +
		"," + FIELD_ITEMS_UPDATETIMESTAMP + " timestamp " +
		"," + FIELD_ITEMS_FOLDERCHILDREN + " integer " +
		"," + FIELD_ITEMS_SIZEBYTES + " integer " +
		"," + FIELD_ITEMS_PATH + " varchar(10000) " +
		"," + FIELD_ITEMS_PARENTID + " varchar(700) " +
		"," + FIELD_ITEMS_PARENTPATH + " varchar(10000) " +
		"," + FIELD_ITEMS_HASH_SHA1 + " varchar(200) " +
		")"
)
View Source
const (
	IndexStrategy_IndexOrDrive IndexStrategy = 0
	IndexStrategy_IndexOnly    IndexStrategy = iota
	IndexStrategy_DriveOnly    IndexStrategy = iota
	IndexStrategy_Default      IndexStrategy = iota

	ItemsMaxAge_None        time.Duration = -1
	ItemsMaxAge_Unspecified time.Duration = 0
	ItemsMaxAge_Default     time.Duration = time.Hour * 24 * 15
)

Variables

Functions

func BasicPathResolver

func BasicPathResolver(pOneDriveItem *onedriveclient.OneDriveItem, pBasePathLocal string, pBasePathDrive string) (string, error)

BasicPathResolver file maintains the same path locally

func PathResolverByItemIdAndDate

func PathResolverByItemIdAndDate(pOneDriveItem *onedriveclient.OneDriveItem, pBasePathLocal string, pBasePathDrive string) (string, error)

PathResolverByItemIdAndDate store files locally by item id and date (for a very basic versioning)

Types

type DownTheDrive

type DownTheDrive struct {
	DenySynchronousAuthentication bool
	// contains filtered or unexported fields
}

DownTheDrive: onedrive utiliy to index the drive into a database and access the content

func NewDownTheDrive

func NewDownTheDrive(pConfig DownTheDriveConfig) (*DownTheDrive, error)

NewDownTheDrive: create a new instance of downthedrive Parameters:

DownTheDriveConfig = configuration

Returns:

*DownTheDrive = DownTheDrive instance
error = nil if succeded otherwise the error occurred

func (*DownTheDrive) Close

func (vSelf *DownTheDrive) Close() error

func (*DownTheDrive) GetApplicationInfo

func (vSelf *DownTheDrive) GetApplicationInfo() (auth.ApplicationInfo, error)

func (*DownTheDrive) GetAuthenticationToken

func (vSelf *DownTheDrive) GetAuthenticationToken() (*auth.AuthenticationToken, error)

func (*DownTheDrive) GetChachedApplicationInfo

func (vSelf *DownTheDrive) GetChachedApplicationInfo() (*auth.ApplicationInfo, error)

func (*DownTheDrive) GetDefaultContext

func (vSelf *DownTheDrive) GetDefaultContext() (*DownTheDriveContext, error)

GetDefaultContext: return a context

func (*DownTheDrive) GetItem

func (vSelf *DownTheDrive) GetItem(pItem interface{}, pExpandChilden bool, pIndexStrategy IndexStrategy, pContext *DownTheDriveContext) (vRisItem *onedriveclient.OneDriveItem, vRisError error)

func (*DownTheDrive) GetItemsMaxAge

func (vSelf *DownTheDrive) GetItemsMaxAge() time.Duration

func (*DownTheDrive) HasItemsExpiry

func (vSelf *DownTheDrive) HasItemsExpiry() bool

func (*DownTheDrive) NewContext

func (vSelf *DownTheDrive) NewContext() (*DownTheDriveContext, error)

NewContext: return a new context

func (*DownTheDrive) NewDownloader

func (vSelf *DownTheDrive) NewDownloader(pLocalStore *LocalStore, pWorkers int) (vRisDownloader *Downloader, vRisError error)

NewDownLoader: create a new instance of downloader Parameters: pDrivePath = path on the drive to download ( "/" = root of the drive) pLocalPath = local path where to downlaod item contents pWorkers = number of concurrent workers Returns: *Downloader = instance of the downloader error = nil if succeded otherwise the error

func (*DownTheDrive) NewIndexer

func (vSelf *DownTheDrive) NewIndexer(pDriveWorkers int) (*Indexer, error)

NewIndexer: create a new instance of indexer Parameters:

pDriveWorkers = number of concurrent workers to the drive

Returns:

*Indexer = indexer object
error = nil if succeded otherwise the error occurred

func (*DownTheDrive) NewItemProcessor

func (vSelf *DownTheDrive) NewItemProcessor(pOneDriveItemDispatcherFunc OneDriveItemDispatcherFunc, pIndexStategy IndexStrategy, pWorkers int) (vRisItemProcessor *ItemProcessor, vError error)

NewItemProcessor create a new instance of item processor

func (*DownTheDrive) PerformAuthentication

func (vSelf *DownTheDrive) PerformAuthentication() (vRisError error)

func (*DownTheDrive) SaveAuthenticationInfo

func (vSelf *DownTheDrive) SaveAuthenticationInfo() error

func (*DownTheDrive) SetAuthenticationHelper

func (vSelf *DownTheDrive) SetAuthenticationHelper(pAuthenticationHelper auth.AuthenticationHelper)

func (*DownTheDrive) SetDefaultIndexStrategy

func (vSelf *DownTheDrive) SetDefaultIndexStrategy(pDefaultIndexStrategy IndexStrategy)

type DownTheDriveAuthenticationConfig

type DownTheDriveAuthenticationConfig struct {
	ClientId     string
	ClientSecret string
}

type DownTheDriveAuthenticationInfo

type DownTheDriveAuthenticationInfo struct {
	auth.StaticAuthenticationInfo
}

func (*DownTheDriveAuthenticationInfo) GetIdInDb

func (vSelf *DownTheDriveAuthenticationInfo) GetIdInDb() string

type DownTheDriveConfig

type DownTheDriveConfig struct {
	Authentication DownTheDriveAuthenticationConfig
	Db             DownTheDriveDbConfig
	IndexStrategy  IndexStrategy
	ItemsMaxAge    time.Duration
}

type DownTheDriveContext

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

DownTheDriveContext: holder for all the local objects needed by downthedrive

func (*DownTheDriveContext) Close

func (vSelf *DownTheDriveContext) Close() error

func (*DownTheDriveContext) GetDb

func (vSelf *DownTheDriveContext) GetDb() (*DownTheDriveDb, error)

GetDb: return a db object instance for the current context

func (*DownTheDriveContext) GetOneDriveClient

func (vSelf *DownTheDriveContext) GetOneDriveClient() (*onedriveclient.OneDriveClient, error)

GetOneDriveClient: return a onedrive object instance for the current context

type DownTheDriveDb

type DownTheDriveDb struct {
	db.DbHelper
	// contains filtered or unexported fields
}

func NewDb

func NewDb(pDriver string, pDataSourceName string) (vRisIndexDb *DownTheDriveDb, vError error)

func (*DownTheDriveDb) BeginItemBulk

func (vSelf *DownTheDriveDb) BeginItemBulk() (vRisBeginned bool, vRisError error)

func (*DownTheDriveDb) CommitItemBulk

func (vSelf *DownTheDriveDb) CommitItemBulk() error

func (*DownTheDriveDb) EndItemBulk

func (vSelf *DownTheDriveDb) EndItemBulk() error

func (*DownTheDriveDb) GetItem

func (vSelf *DownTheDriveDb) GetItem(pItem interface{}, pExpandChildren bool) (vRisItem *onedriveclient.OneDriveItem, vRisError error)

func (*DownTheDriveDb) GetItemsByConditions

func (vSelf *DownTheDriveDb) GetItemsByConditions(pConditions string) (vRisItems []*onedriveclient.OneDriveItem, vRisError error)

func (*DownTheDriveDb) SaveItem

func (vSelf *DownTheDriveDb) SaveItem(pItem *onedriveclient.OneDriveItem) error

type DownTheDriveDbConfig

type DownTheDriveDbConfig struct {
	DataSource string
	Driver     string
}

type Downloader

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

Downloader: allows to download items content from drive

func (*Downloader) Close

func (vSelf *Downloader) Close() error

func (*Downloader) Download

func (vSelf *Downloader) Download() error

type IndexStrategy

type IndexStrategy int

type Indexer

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

Indexed: allows to index onedrive path into a database

func (*Indexer) CountIndexedItems

func (vSelf *Indexer) CountIndexedItems() int

func (*Indexer) Index

func (vSelf *Indexer) Index(pItem interface{}, pContext *DownTheDriveContext) error

Index: index a single item into the database Parameters:

pItem = item to index
pContext = instance of DownTheDriveContext

Returns:

error = nil if succeded otherwise the error

func (*Indexer) IndexDrivePath

func (vSelf *Indexer) IndexDrivePath(pPath string) error

Build: index a drive path Parameters:

pPath = drive path to index

Returns:

nil if succeded otherwise the error

func (*Indexer) IndexUpToRoot

func (vSelf *Indexer) IndexUpToRoot(pItem interface{}, pContext *DownTheDriveContext) error

IndexUpToRoot: index an item and parents up to root Parameters:

pItem = item to index
pContext = instance of DownTheDriveContext

Returns:

nil if succeded otherwise the error

func (*Indexer) IsSucceded

func (vSelf *Indexer) IsSucceded() bool

func (*Indexer) StartIndexing

func (vSelf *Indexer) StartIndexing() error

func (*Indexer) WaitForCompletition

func (vSelf *Indexer) WaitForCompletition()

type ItemProcessor

type ItemProcessor struct {

	//parent
	Parent *DownTheDrive
	// contains filtered or unexported fields
}

ItemProcessor is an object for item recurring operations

func (*ItemProcessor) Close

func (vSelf *ItemProcessor) Close() error

func (*ItemProcessor) Enqueue

func (vSelf *ItemProcessor) Enqueue(pItem interface{})

Enqueue an item Parameters: pItem = item to process

func (*ItemProcessor) ForEachWorker

func (vSelf *ItemProcessor) ForEachWorker(pConsumerFunc ItemProcessorWorkerConsumerFunc) error

func (*ItemProcessor) IsSucceded

func (vSelf *ItemProcessor) IsSucceded() bool

func (*ItemProcessor) Process

func (vSelf *ItemProcessor) Process(pItem interface{}) error

func (*ItemProcessor) WaitForCompletition

func (vSelf *ItemProcessor) WaitForCompletition()

type ItemProcessorWorkerConsumerFunc

type ItemProcessorWorkerConsumerFunc func(pContext *DownTheDriveContext, pWorkerId int) error

type LocalFileChecker

type LocalFileChecker struct {
	CheckSize    bool
	CheckModDate bool
}

func (*LocalFileChecker) CheckerFunc

func (vSelf *LocalFileChecker) CheckerFunc(pOneDriveItem *onedriveclient.OneDriveItem, pLocalFilePath string) (bool, error)

type LocalFileCheckerFunc

type LocalFileCheckerFunc func(pOneDriveItem *onedriveclient.OneDriveItem, pLocalFilePath string) (bool, error)

LocalFileCheckerFunc: function to check if a onedriveitem and a local file have the same content Parameters:

OneDriveItem = One Drive Item
string = local file path

Returns:

bool=true if local file exists and is the same of remote file, otherwise false
error=error if something goes wrong

type LocalStore

type LocalStore struct {
	LocalFileCheckerFunc LocalFileCheckerFunc
	// contains filtered or unexported fields
}

LocalStore: manager of locally stored files

func NewLocalStore

func NewLocalStore(pBasePathLocal string, pOneDriveBaseItemPath *onedriveclient.OneDriveItem, pPathResolverFunc PathResolverFunc) (*LocalStore, error)

NewLocalStore: create a new local store

func (*LocalStore) Close

func (vSelf *LocalStore) Close() error

func (*LocalStore) Exists

func (vSelf *LocalStore) Exists(pOneDriveItem *onedriveclient.OneDriveItem) (bool, error)

func (*LocalStore) Get

func (vSelf *LocalStore) Get(pOneDriveItem *onedriveclient.OneDriveItem, pContext *DownTheDriveContext) (*LocalStoreItemReference, error)

Get: store locally the content of the specified onedriveitem if not present then return a refrence Parameters:

pOneDriveItem = item to store
pDownTheDriveContext = DownTheDrive context

Return:

LocalStoreItemReference = a reference to open the file
error = nil if succeded otherwise the error

func (*LocalStore) GetOneDriveBaseItemPath

func (vSelf *LocalStore) GetOneDriveBaseItemPath() *onedriveclient.OneDriveItem

func (*LocalStore) Open

func (vSelf *LocalStore) Open(pItemReference *LocalStoreItemReference) (*os.File, error)

Open: open the file Parameters:

*LocalStoreItemReference = item reference

Returns:

*os.File = local file
error = nil if succeded otherwise the error

func (*LocalStore) WriteInto

func (vSelf *LocalStore) WriteInto(pItemReference *LocalStoreItemReference, pDestination io.Writer) error

WriteInto write the content of the item into a writer Parameters:

*LocalStoreItemReference = item reference
io.Writer = Destination

Returns: nil if succeded otherwise the error

type LocalStoreItemReference

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

LocalStoreItemReference: reference to an item stored locally

type OneDriveItemDispatcherFunc

type OneDriveItemDispatcherFunc func(pContext *DownTheDriveContext, pItem *onedriveclient.OneDriveItem, pWorkerId int) error

OneDriveItemDispatcherFunc signature of dispatcher method Parameters:

pContext = DownTheDrive context
pItem = item to process
pWorkerId = id of the current worker

Returns:

nil in case of success otherwise the error

type PathResolverFunc

type PathResolverFunc func(*onedriveclient.OneDriveItem, string, string) (string, error)

PathResolverFunc function responsible to translate item to local path Parameters:

*onedriveclient.OneDriveItem = one drive item
string = local base path
string = base drive path

Returns:

string = path of the drive item locally, empty in the item is not stored locally
error = nil if succeded, otherwise the error occurred

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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