Skip to content

Instantly share code, notes, and snippets.

@penguwin
Created January 14, 2017 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save penguwin/6c573886707f1881deff8ccee711a422 to your computer and use it in GitHub Desktop.
Save penguwin/6c573886707f1881deff8ccee711a422 to your computer and use it in GitHub Desktop.
storage_mega.go
package mega
import (
"errors"
"net/url"
"path/filepath"
"strings"
knoxite "github.com/knoxite/knoxite/lib"
"github.com/t3rm1n4l/go-mega"
)
// StorageMega stores data on a remote Mega
type StorageMega struct {
url url.URL
mega *mega.Mega
knoxite.StorageFilesystem
}
// init registers the backend
func init() {
knoxite.RegisterBackendFactory(&StorageMega{})
}
// NewBackend returns a StorageMega backend
func (*StorageMega) NewBackend(u url.URL) (knoxite.Backend, error) {
// Checking for username and password
if u.User.Username() == "" {
return &StorageMega{}, knoxite.ErrInvalidUsername
}
pw, pwexist := u.User.Password()
if !pwexist {
return &StorageMega{}, knoxite.ErrInvalidPassword
}
// Creating a new client and authenticate via login
mg := mega.New()
if err := mg.Login(u.User.Username(), pw); err != nil {
return &StorageMega{}, errors.New("Failed to login")
}
backend := StorageMega{
url: u,
mega: mg,
}
// auto x = &s; // invoking on x or auto returns "auto";
// // invoking on s returns "const char *"
storageMega, err := knoxite.NewStorageFilesystem(u.Path, &backend)
if err != nil {
return &StorageMega{}, err
}
backend.StorageFilesystem = storageMega
return &backend, nil
}
// Location returns the type and location if the repository
func (backend *StorageMega) Location() string {
return backend.url.String()
}
// Close the backend
func (backend *StorageMega) Close() error {
return nil
}
// Protocols returns the Protocol schemes supported by this backend
func (backend *StorageMega) Protocols() []string {
return []string{"mega"}
}
// Description returns a user-friendly desription for this backend
func (backend *StorageMega) Description() string {
return "Mega Storage"
}
// AvailableSpace returns the free space on this backend
func (backend *StorageMega) AvailableSpace() (uint64, error) {
// Currently not implemented
return 0, nil
}
// CreatePath creates a dir including all its parents dirs, when required
func (backend *StorageMega) CreatePath(path string) error {
slicedPath := strings.Split(path, string(filepath.Separator))
for i := range slicedPath {
if i == 0 {
// Dont try to create root-dir
continue
}
// FIXME: Find the fitting node
if _, err := backend.mega.CreateDir(filepath.Join(slicedPath[:i+1]...), backend.mega.FS); err != nil {
// We only want to return an error when creating the last directory
// if this path failed. Parent dirs _may_ already exist.
if i+1 == len(slicedPath) {
return err
}
}
}
return nil
}
// Stat returns the size of a file
func (backend *StorageMega) Stat(path string) (uint64, error) {
return 0, nil
}
// ReadFile reads a file from mega
func (backend *StorageMega) ReadFile(path string) (*[]byte, error) {
return nil, nil
}
// WriteFile write files on mega
func (backend *StorageMega) WriteFile(path string, data *[]byte) (size uint64, err error) {
return 0, nil
}
// DeleteFile deletes a file from mega
func (backend *StorageMega) DeleteFile(path string) error {
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment