diff --git a/changelog/unreleased/fix-code-smells.md b/changelog/unreleased/fix-code-smells.md new file mode 100644 index 0000000000..8d15094a8a --- /dev/null +++ b/changelog/unreleased/fix-code-smells.md @@ -0,0 +1,6 @@ +Change: refactor code to remove code smells + +Scanning the code using a static code analyzer showed some code smells. +This change fixes them. + +https://github.com/owncloud/ocis-thumbnails/issues/21 diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 0ecf2f55c6..8139f7ba1b 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -40,15 +40,15 @@ func New() *Metrics { }, []string{}), } - prometheus.Register( + _ = prometheus.Register( m.Counter, ) - prometheus.Register( + _ = prometheus.Register( m.Latency, ) - prometheus.Register( + _ = prometheus.Register( m.Duration, ) diff --git a/pkg/server/grpc/server.go b/pkg/server/grpc/server.go index ed67be00b0..588abbbb2c 100644 --- a/pkg/server/grpc/server.go +++ b/pkg/server/grpc/server.go @@ -40,7 +40,7 @@ func NewService(opts ...Option) grpc.Service { thumbnail = svc.NewLogging(thumbnail, options.Logger) } - proto.RegisterThumbnailServiceHandler( + _ = proto.RegisterThumbnailServiceHandler( service.Server(), thumbnail, ) diff --git a/pkg/thumbnail/imgsource/filesystem.go b/pkg/thumbnail/imgsource/filesystem.go index d84f59555a..78439db702 100644 --- a/pkg/thumbnail/imgsource/filesystem.go +++ b/pkg/thumbnail/imgsource/filesystem.go @@ -25,7 +25,7 @@ type FileSystem struct { // Get retrieves an image from the filesystem. func (s FileSystem) Get(ctx context.Context, file string) (image.Image, error) { imgPath := filepath.Join(s.basePath, file) - f, err := os.Open(imgPath) + f, err := os.Open(filepath.Clean(imgPath)) if err != nil { return nil, fmt.Errorf("failed to load the file %s from %s error %s", file, imgPath, err.Error()) } diff --git a/pkg/thumbnail/storage/filesystem.go b/pkg/thumbnail/storage/filesystem.go index dc45d9de03..83fbef88e5 100644 --- a/pkg/thumbnail/storage/filesystem.go +++ b/pkg/thumbnail/storage/filesystem.go @@ -27,7 +27,8 @@ type FileSystem struct { // Get loads the image from the file system. func (s FileSystem) Get(key string) []byte { - content, err := ioutil.ReadFile(filepath.Join(s.dir, key)) + path := filepath.Join(s.dir, key) + content, err := ioutil.ReadFile(filepath.Clean(path)) if err != nil { s.logger.Warn().Err(err).Msgf("could not read file %s", key) return nil @@ -48,7 +49,11 @@ func (s FileSystem) Set(key string, img []byte) error { if err != nil { return fmt.Errorf("could not create file \"%s\" error: %s", key, err.Error()) } - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + s.logger.Warn().Err(err).Msg("closing file resulted in an error") + } + }() _, err = f.Write(img) if err != nil { return fmt.Errorf("could not write to file \"%s\" error: %s", key, err.Error())