From 5627911858f6c76f7f240e7633fcced22579e202 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 --- pkg/app/provider/wopi/wopi.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pkg/app/provider/wopi/wopi.go b/pkg/app/provider/wopi/wopi.go index f25122f40bc..44478ec535c 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_mimetypes_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(