Skip to content

Commit

Permalink
ui: Add route to download file through integration
Browse files Browse the repository at this point in the history
  • Loading branch information
nemunaire committed Jun 1, 2023
1 parent 67e77d2 commit 418ab2a
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ func (app *App) integrationsGetFile(c *gin.Context) {
return
}

reader, err := integrationProvider.Download(fileID)
reader, _, err := integrationProvider.Download(fileID)
if err != nil {
log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
Expand Down
4 changes: 2 additions & 2 deletions internal/integrations/dropbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,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
4 changes: 2 additions & 2 deletions internal/integrations/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
// IntegrationProvider abstracts 3rd party integrations
type IntegrationProvider interface {
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 Down Expand Up @@ -132,7 +132,7 @@ func visitDir(root, currentPath string, depth int, parentFolder *messages.Integr
ID: encodedPath,
FileID: encodedPath,
Name: docName,
Size: int(d.Size()),
Size: d.Size(),
SourceFileType: contentType,
}

Expand Down
12 changes: 9 additions & 3 deletions internal/integrations/localfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,21 @@ 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)
if err != nil {
return nil, 0, err
}

res, err := os.Open(localPath)
return res, st.Size(), err
}
func (d *localFS) Upload(folderID, name, fileType string, reader io.ReadCloser) (id string, err error) {
folder := "/"
Expand Down
14 changes: 11 additions & 3 deletions internal/integrations/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,20 @@ 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, err
return nil, 0, err
}

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

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

return res, st.Size(), err
}

// List populates the response
Expand Down
2 changes: 1 addition & 1 deletion internal/messages/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ type IntegrationFile struct {
ID string `json:"id"`
Name string `json:"name"`
ProvidedFileType string `json:"providedFileType"`
Size int `json:"size"`
Size int64 `json:"size"`
SourceFileType string `json:"sourceFileType"`
}

Expand Down
26 changes: 26 additions & 0 deletions internal/ui/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,29 @@ func (app *ReactAppWrapper) exploreIntegration(c *gin.Context) {

c.JSON(http.StatusOK, response)
}

func (app *ReactAppWrapper) downloadThroughIntegration(c *gin.Context) {
uid := c.GetString(userIDContextKey)

integrationID := common.ParamS(intIDParam, c)

integrationProvider, err := integrations.GetIntegrationProvider(app.userStorer, uid, integrationID)
if err != nil {
log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}

fileid := common.ParamS("path", c)

response, size, err := integrationProvider.Download(fileid)
if err != nil {
log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}

defer response.Close()

c.DataFromReader(http.StatusOK, size, "", response, nil)
}
1 change: 1 addition & 0 deletions internal/ui/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (app *ReactAppWrapper) RegisterRoutes(router *gin.Engine) {
auth.DELETE("integrations/:intid", app.deleteIntegration)

auth.GET("integrations/:intid/explore/*path", app.exploreIntegration)
auth.GET("integrations/:intid/download/*path", app.downloadThroughIntegration)

//admin
admin := auth.Group("")
Expand Down

0 comments on commit 418ab2a

Please sign in to comment.