Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/juruen/rmapi v0.0.25
github.com/poundifdef/go-remarkable2pdf v0.2.0
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
github.com/secsy/goftp v0.0.0-20200609142545-aa2de14babf4
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/studio-b12/gowebdav v0.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 h1:mZHayPoR0lNmnH
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5/go.mod h1:GEXHk5HgEKCvEIIrSpFI3ozzG5xOKA2DVlEX/gGnewM=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
github.com/secsy/goftp v0.0.0-20200609142545-aa2de14babf4 h1:PT+ElG/UUFMfqy5HrxJxNzj3QBOf7dZwupeVC+mG1Lo=
github.com/secsy/goftp v0.0.0-20200609142545-aa2de14babf4/go.mod h1:MnkX001NG75g3p8bhFycnyIjeQoOjGL6CEIsdE/nKSY=
github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
Expand Down
27 changes: 22 additions & 5 deletions internal/app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,9 +908,25 @@ func (app *App) blobStorageWrite(c *gin.Context) {
}

func (app *App) integrationsGetMetadata(c *gin.Context) {
var metadata messages.IntegrationMetadata
metadata.Thumbnail = ""
c.JSON(http.StatusOK, &metadata)
uid := c.GetString(userIDKey)
integrationID := common.ParamS(integrationKey, c)
fileID := common.ParamS(fileKey, c)

integrationProvider, err := integrations.GetIntegrationProvider(app.userStorer, uid, integrationID)
if err != nil {
log.Error(fmt.Errorf("can't get integration, %v", err))
c.AbortWithStatus(http.StatusInternalServerError)
return
}

metadata, err := integrationProvider.GetMetadata(fileID)
if err != nil {
log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}

c.JSON(http.StatusOK, metadata)
}

func (app *App) integrationsUpload(c *gin.Context) {
Expand Down Expand Up @@ -952,7 +968,7 @@ func (app *App) integrationsGetFile(c *gin.Context) {
return
}

reader, err := integrationProvider.Download(fileID)
reader, size, err := integrationProvider.Download(fileID)
if err != nil {
log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
Expand All @@ -961,8 +977,9 @@ func (app *App) integrationsGetFile(c *gin.Context) {

defer reader.Close()

c.DataFromReader(http.StatusOK, -1, "application/octet-stream", reader, nil)
c.DataFromReader(http.StatusOK, size, "application/octet-stream", reader, nil)
}

func (app *App) integrationsList(c *gin.Context) {
uid := c.GetString(userIDKey)
integrationID := common.ParamS(integrationKey, c)
Expand Down
9 changes: 7 additions & 2 deletions internal/integrations/dropbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func newDropbox(i model.IntegrationConfig) *DropBox {
}
}

func (d *DropBox) GetMetadata(fileID string) (*messages.IntegrationMetadata, error) {
// TODO
return nil, nil
}

func (d *DropBox) List(folderID string, depth int) (*messages.IntegrationFolder, error) {

args := files.ListFolderArg{
Expand Down Expand Up @@ -65,8 +70,8 @@ func (d *DropBox) List(folderID string, depth int) (*messages.IntegrationFolder,
return response, nil
}

func (d *DropBox) Download(fileID string) (io.ReadCloser, error) {
return nil, nil
func (d *DropBox) Download(fileID string) (io.ReadCloser, int64, error) {
return nil, 0, nil

}
func (d *DropBox) Upload(folderID, name, fileType string, reader io.ReadCloser) (string, error) {
Expand Down
134 changes: 134 additions & 0 deletions internal/integrations/ftp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package integrations

import (
"bytes"
"io"
"os"
"path"
"strings"

"github.com/ddvk/rmfakecloud/internal/messages"
"github.com/ddvk/rmfakecloud/internal/model"
"github.com/secsy/goftp"
"github.com/sirupsen/logrus"
)

type FTPIntegration struct {
client *goftp.Client
}

func newFTP(i model.IntegrationConfig) *FTPIntegration {
config := goftp.Config{
Logger: os.Stderr,
ActiveTransfers: i.ActiveTransfers,
}

if i.Username != "" {
config.User = i.Username
}
if i.Password != "" {
config.Password = i.Password
}

if strings.HasPrefix(i.Address, "ftps://") {
config.TLSMode = goftp.TLSImplicit
i.Address = strings.TrimPrefix(i.Address, "ftps://")
} else if strings.HasPrefix(i.Address, "ftpes://") {
config.TLSMode = goftp.TLSExplicit
i.Address = strings.TrimPrefix(i.Address, "ftpes://")
}

client, err := goftp.DialConfig(config, strings.TrimPrefix(i.Address, "ftp://"))
if err != nil {
logrus.Errorf("An error occurred creating FTP client: %v\n", err)
return nil
}

return &FTPIntegration{
client,
}
}

func (g *FTPIntegration) GetMetadata(fileID string) (*messages.IntegrationMetadata, error) {
decoded, err := decodeName(fileID)
if err != nil {
return nil, err
}

ext := path.Ext(decoded)
contentType := contentTypeFromExt(ext)

return &messages.IntegrationMetadata{
ID: fileID,
Name: path.Base(decoded),
Thumbnail: []byte{},
SourceFileType: contentType,
ProvidedFileType: contentType,
FileType: ext,
}, nil
}

func (g *FTPIntegration) List(folder string, depth int) (*messages.IntegrationFolder, error) {
response := messages.NewIntegrationFolder(folder, "")

if folder == rootFolder {
folder = "/"
response.Name = "FTP root"
} else {
decoded, err := decodeName(folder)
if err != nil {
return nil, err
}
folder = decoded
response.Name = path.Base(folder)
}
logrus.Info("[ftp] query for: ", folder, " depth: ", depth)

err := visitDir("", folder, depth, response, g.client.ReadDir)
if err != nil {
return nil, err
}

return response, nil
}

func (g *FTPIntegration) Download(fileID string) (io.ReadCloser, int64, error) {
decoded, err := decodeName(fileID)
if err != nil {
return nil, 0, err
}

st, err := g.client.Stat(decoded)
if err != nil {
return nil, 0, err
}

var buf bytes.Buffer

err = g.client.Retrieve(decoded, &buf)
if err != nil {
return nil, st.Size(), err
}

return io.NopCloser(&buf), st.Size(), err
}

func (g *FTPIntegration) Upload(folderID, name, fileType string, reader io.ReadCloser) (id string, err error) {
folder := "/"
if folderID != rootFolder {
folder, err = decodeName(folderID)
if err != nil {
return
}
}
fullpath := path.Join(folder, name+"."+fileType)
logrus.Trace(logger, "Uploading: ", fullpath)

err = g.client.Store(fullpath, reader)

if err != nil {
return
}
id = encodeName(fullpath)
return
}
13 changes: 9 additions & 4 deletions internal/integrations/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

const (
ftpProvider = "ftp"
webdavProvider = "webdav"
dropboxProvider = "dropbox"
googleProvider = "google"
Expand All @@ -21,8 +22,9 @@ const (

// IntegrationProvider abstracts 3rd party integrations
type IntegrationProvider interface {
GetMetadata(fileID string) (result *messages.IntegrationMetadata, err error)
List(folderID string, depth int) (result *messages.IntegrationFolder, err error)
Download(fileID string) (io.ReadCloser, error)
Download(fileID string) (io.ReadCloser, int64, error)
Upload(folderID, name, fileType string, reader io.ReadCloser) (string, error)
}

Expand All @@ -37,12 +39,14 @@ func GetIntegrationProvider(storer storage.UserStorer, uid, integrationid string
continue
}
switch intg.Provider {
case webdavProvider:
return newWebDav(intg), nil
case dropboxProvider:
return newDropbox(intg), nil
case ftpProvider:
return newFTP(intg), nil
case localfsProvider:
return newLocalFS(intg), nil
case webdavProvider:
return newWebDav(intg), nil
}
}
return nil, fmt.Errorf("integration not found or no implmentation (only webdav) %s", integrationid)
Expand All @@ -52,6 +56,8 @@ func GetIntegrationProvider(storer storage.UserStorer, uid, integrationid string
// fix the name
func fixProviderName(n string) string {
switch n {
case ftpProvider:
fallthrough
case dropboxProvider:
return "Dropbox"
case googleProvider:
Expand Down Expand Up @@ -125,7 +131,6 @@ func visitDir(root, currentPath string, depth int, parentFolder *messages.Integr
docName := strings.TrimSuffix(entryName, ext)
extension := strings.TrimPrefix(ext, ".")


file := &messages.IntegrationFile{
ProvidedFileType: contentType,
DateChanged: d.ModTime(),
Expand Down
32 changes: 29 additions & 3 deletions internal/integrations/localfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ func newLocalFS(i model.IntegrationConfig) *localFS {
}
}

func (d *localFS) GetMetadata(fileID string) (*messages.IntegrationMetadata, error) {
decoded, err := decodeName(fileID)
if err != nil {
return nil, err
}

ext := path.Ext(decoded)
contentType := contentTypeFromExt(ext)

return &messages.IntegrationMetadata{
ID: fileID,
Name: path.Base(decoded),
Thumbnail: []byte{},
SourceFileType: contentType,
ProvidedFileType: contentType,
FileType: ext,
}, nil
}

// List populates the response
func (d *localFS) List(folder string, depth int) (*messages.IntegrationFolder, error) {
response := messages.NewIntegrationFolder(folder, "")
Expand Down Expand Up @@ -69,16 +88,23 @@ func (d *localFS) List(folder string, depth int) (*messages.IntegrationFolder, e
return response, nil
}

func (d *localFS) Download(fileID string) (io.ReadCloser, error) {
func (d *localFS) Download(fileID string) (io.ReadCloser, int64, error) {
decoded, err := decodeName(fileID)
if err != nil {
return nil, err
return nil, 0, err
}

localPath := path.Join(d.rootPath, path.Clean(decoded))
return os.Open(localPath)

st, err := os.Stat(localPath)

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression

This path depends on a [user-provided value](1).
if err != nil {
return nil, 0, err
}

res, err := os.Open(localPath)

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression

This path depends on a [user-provided value](1).
return res, st.Size(), err
}

func (d *localFS) Upload(folderID, name, fileType string, reader io.ReadCloser) (id string, err error) {
folder := "/"
if folderID != rootFolder {
Expand Down
31 changes: 29 additions & 2 deletions internal/integrations/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,39 @@ func (w *WebDavIntegration) Upload(folderID, name, fileType string, reader io.Re
}

// Download downloads
func (w *WebDavIntegration) Download(fileID string) (io.ReadCloser, error) {
func (w *WebDavIntegration) Download(fileID string) (io.ReadCloser, int64, error) {
decoded, err := decodeName(fileID)
if err != nil {
return nil, 0, err
}

st, err := w.c.Stat(decoded)
if err != nil {
return nil, 0, err
}

res, err := w.c.ReadStream(decoded)

return res, st.Size(), err
}

func (w *WebDavIntegration) GetMetadata(fileID string) (*messages.IntegrationMetadata, error) {
decoded, err := decodeName(fileID)
if err != nil {
return nil, err
}
return w.c.ReadStream(decoded)

ext := path.Ext(decoded)
contentType := contentTypeFromExt(ext)

return &messages.IntegrationMetadata{
ID: fileID,
Name: path.Base(decoded),
Thumbnail: []byte{},
SourceFileType: contentType,
ProvidedFileType: contentType,
FileType: ext,
}, nil
}

// List populates the response
Expand Down
11 changes: 7 additions & 4 deletions internal/messages/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,11 @@ type IntegrationFolder struct {
}

type IntegrationMetadata struct {
FileType string `json:"fileType"`
ID string `json:"id"`
Name string `json:"name"`
Thumbnail string `json:"thumbnail"`
ID string `json:"id"`
Name string `json:"name"`
// Thumbnail is base64 encoded string of an image/png
Thumbnail []byte `json:"thumbnail"`
SourceFileType string `json:"sourceFileType"`
ProvidedFileType string `json:"providedFileType"`
FileType string `json:"fileType"`
}
Loading