-
Notifications
You must be signed in to change notification settings - Fork 105
New integration: FTP #339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
New integration: FTP #339
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5ecd9a6
ui: Add interface to manage integrations
nemunaire 2937e8e
Reorder providers
nemunaire 1eaa6a0
Implement metadata retrieval for integrations
nemunaire adefc34
integrations: Add Size in return to download interface
nemunaire a5bd8c2
Add API routes to explore integrations content in ui
nemunaire 86ab6e1
Add FTP integration
nemunaire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression