From 8c2123cbc42e684253cd2a76e95ba161a0104b0f Mon Sep 17 00:00:00 2001 From: Giuseppe Lo Presti Date: Mon, 2 May 2022 15:57:47 +0200 Subject: [PATCH] Add custom mimetypes config to the app provider --- .../unreleased/custom-mime-appprovider.md | 6 ++++ examples/nextcloud-integration/revad.toml | 1 + .../storageprovider/storageprovider.go | 2 +- pkg/app/provider/wopi/wopi.go | 28 +++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/custom-mime-appprovider.md diff --git a/changelog/unreleased/custom-mime-appprovider.md b/changelog/unreleased/custom-mime-appprovider.md new file mode 100644 index 0000000000..60634b0a4b --- /dev/null +++ b/changelog/unreleased/custom-mime-appprovider.md @@ -0,0 +1,6 @@ +Enhancement: support custom mimetypes in the WOPI appprovider driver + +Similarly to the storage provider, also the WOPI appprovider driver +now supports custom mime types. Also fixed a small typo. + +https://github.com/cs3org/reva/pull/2813 diff --git a/examples/nextcloud-integration/revad.toml b/examples/nextcloud-integration/revad.toml index 3f8c7b33ff..34b2c6d6dc 100644 --- a/examples/nextcloud-integration/revad.toml +++ b/examples/nextcloud-integration/revad.toml @@ -73,6 +73,7 @@ iop_secret = "hello" wopi_url = "http://0.0.0.0:8880/" app_name = "Collabora" app_url = "https://your-collabora-server.org:9980" +custom_mime_types_json = "custom-mime-types-demo.json" [grpc.services.appregistry] driver = "static" diff --git a/internal/grpc/services/storageprovider/storageprovider.go b/internal/grpc/services/storageprovider/storageprovider.go index 1c6dac47b0..bba333ad7a 100644 --- a/internal/grpc/services/storageprovider/storageprovider.go +++ b/internal/grpc/services/storageprovider/storageprovider.go @@ -63,7 +63,7 @@ type config struct { DataServerURL string `mapstructure:"data_server_url" docs:"http://localhost/data;The URL for the data server."` ExposeDataServer bool `mapstructure:"expose_data_server" docs:"false;Whether to expose data server."` // if true the client will be able to upload/download directly to it AvailableXS map[string]uint32 `mapstructure:"available_checksums" docs:"nil;List of available checksums."` - CustomMimeTypesJSON string `mapstructure:"custom_mimetypes_json" docs:"nil;An optional mapping file with the list of supported custom file extensions and corresponding mime types."` + CustomMimeTypesJSON string `mapstructure:"custom_mime_types_json" docs:"nil;An optional mapping file with the list of supported custom file extensions and corresponding mime types."` } func (c *config) init() { diff --git a/pkg/app/provider/wopi/wopi.go b/pkg/app/provider/wopi/wopi.go index f25122f40b..244ceeee3b 100644 --- a/pkg/app/provider/wopi/wopi.go +++ b/pkg/app/provider/wopi/wopi.go @@ -64,6 +64,7 @@ type config struct { AppIntURL string `mapstructure:"app_int_url" docs:";The internal app URL in case of dockerized deployments. Defaults to AppURL"` AppAPIKey string `mapstructure:"app_api_key" docs:";The API key used by the app, if applicable."` JWTSecret string `mapstructure:"jwt_secret" docs:";The JWT secret to be used to retrieve the token TTL."` + CustomMimeTypesJSON string `mapstructure:"custom_mime_types_json" docs:"nil;An optional mapping file with the list of supported custom file extensions and corresponding mime types."` AppDesktopOnly bool `mapstructure:"app_desktop_only" docs:"false;Specifies if the app can be opened only on desktop."` InsecureConnections bool `mapstructure:"insecure_connections"` } @@ -111,6 +112,12 @@ func New(m map[string]interface{}) (app.Provider, error) { return http.ErrUseLastResponse } + // read and register custom mime types if configured + err = registerMimeTypes(c.CustomMimeTypesJSON) + if err != nil { + return nil, err + } + return &wopiProvider{ conf: c, wopiClient: wopiClient, @@ -279,6 +286,27 @@ func (p *wopiProvider) GetAppProviderInfo(ctx context.Context) (*appregistry.Pro }, nil } +func registerMimeTypes(mappingFile string) error { + // TODO(lopresti) this function also exists in the storage provider, to be seen if we want to factor it out, though a + // fileext <-> mimetype "service" would have to be served by the gateway for it to be accessible both by storage providers and app providers. + if mappingFile != "" { + f, err := ioutil.ReadFile(mappingFile) + if err != nil { + return fmt.Errorf("storageprovider: error reading the custom mime types file: +%v", err) + } + mimeTypes := map[string]string{} + err = json.Unmarshal(f, &mimeTypes) + if err != nil { + return fmt.Errorf("storageprovider: error unmarshalling the custom mime types file: +%v", err) + } + // register all mime types that were read + for e, m := range mimeTypes { + mime.RegisterMime(e, m) + } + } + return nil +} + func getAppURLs(c *config) (map[string]map[string]string, error) { // Initialize WOPI URLs by discovery httpcl := rhttp.GetHTTPClient(