@@ -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
|
||||||
@@ -40,15 +40,15 @@ func New() *Metrics {
|
|||||||
}, []string{}),
|
}, []string{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
prometheus.Register(
|
_ = prometheus.Register(
|
||||||
m.Counter,
|
m.Counter,
|
||||||
)
|
)
|
||||||
|
|
||||||
prometheus.Register(
|
_ = prometheus.Register(
|
||||||
m.Latency,
|
m.Latency,
|
||||||
)
|
)
|
||||||
|
|
||||||
prometheus.Register(
|
_ = prometheus.Register(
|
||||||
m.Duration,
|
m.Duration,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func NewService(opts ...Option) grpc.Service {
|
|||||||
thumbnail = svc.NewLogging(thumbnail, options.Logger)
|
thumbnail = svc.NewLogging(thumbnail, options.Logger)
|
||||||
}
|
}
|
||||||
|
|
||||||
proto.RegisterThumbnailServiceHandler(
|
_ = proto.RegisterThumbnailServiceHandler(
|
||||||
service.Server(),
|
service.Server(),
|
||||||
thumbnail,
|
thumbnail,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ type FileSystem struct {
|
|||||||
// Get retrieves an image from the filesystem.
|
// Get retrieves an image from the filesystem.
|
||||||
func (s FileSystem) Get(ctx context.Context, file string) (image.Image, error) {
|
func (s FileSystem) Get(ctx context.Context, file string) (image.Image, error) {
|
||||||
imgPath := filepath.Join(s.basePath, file)
|
imgPath := filepath.Join(s.basePath, file)
|
||||||
f, err := os.Open(imgPath)
|
f, err := os.Open(filepath.Clean(imgPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to load the file %s from %s error %s", file, imgPath, err.Error())
|
return nil, fmt.Errorf("failed to load the file %s from %s error %s", file, imgPath, err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,8 @@ type FileSystem struct {
|
|||||||
|
|
||||||
// Get loads the image from the file system.
|
// Get loads the image from the file system.
|
||||||
func (s FileSystem) Get(key string) []byte {
|
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 {
|
if err != nil {
|
||||||
s.logger.Warn().Err(err).Msgf("could not read file %s", key)
|
s.logger.Warn().Err(err).Msgf("could not read file %s", key)
|
||||||
return nil
|
return nil
|
||||||
@@ -48,7 +49,11 @@ func (s FileSystem) Set(key string, img []byte) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not create file \"%s\" error: %s", key, err.Error())
|
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)
|
_, err = f.Write(img)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not write to file \"%s\" error: %s", key, err.Error())
|
return fmt.Errorf("could not write to file \"%s\" error: %s", key, err.Error())
|
||||||
|
|||||||
Reference in New Issue
Block a user