Skip to content

Commit

Permalink
Implement integration metadata retrieval
Browse files Browse the repository at this point in the history
Update messages.IntegrationMetadata struct as mention in ddvk#241 (comment)
  • Loading branch information
nemunaire committed Oct 17, 2023
1 parent 37b4729 commit cb37f62
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 7 deletions.
22 changes: 19 additions & 3 deletions internal/app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,9 +750,25 @@ func (app *App) syncGetRootV3(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
4 changes: 4 additions & 0 deletions internal/integrations/dropbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func (d *DropBox) List(folderID string, depth int) (*messages.IntegrationFolder,
return response, nil
}

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

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

Expand Down
1 change: 1 addition & 0 deletions internal/integrations/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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, int64, error)
Upload(folderID, name, fileType string, reader io.ReadCloser) (string, error)
Expand Down
19 changes: 19 additions & 0 deletions internal/integrations/localfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ func (d *localFS) List(folder string, depth int) (*messages.IntegrationFolder, e
return response, nil
}

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
}

func (d *localFS) Download(fileID string) (io.ReadCloser, int64, error) {
decoded, err := decodeName(fileID)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions internal/integrations/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ func (w *WebDavIntegration) Upload(folderID, name, fileType string, reader io.Re
return
}

func (w *WebDavIntegration) 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
}

// Download downloads
func (w *WebDavIntegration) Download(fileID string) (io.ReadCloser, int64, error) {
decoded, err := decodeName(fileID)
Expand Down
11 changes: 7 additions & 4 deletions internal/messages/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,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"`
}
24 changes: 24 additions & 0 deletions internal/ui/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,30 @@ func (app *ReactAppWrapper) exploreIntegration(c *gin.Context) {
c.JSON(http.StatusOK, response)
}

func (app *ReactAppWrapper) getMetadataIntegration(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, err := integrationProvider.GetMetadata(fileid)
if err != nil {
log.Error(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}

c.JSON(http.StatusOK, response)
}

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

Expand Down
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/metadata/*path", app.getMetadataIntegration)
auth.GET("integrations/:intid/download/*path", app.downloadThroughIntegration)

//admin
Expand Down

0 comments on commit cb37f62

Please sign in to comment.