From 8c32ff95d4d6bd925c4c764c1efd25008e9216b6 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Tue, 26 Jul 2022 11:13:25 +0800 Subject: [PATCH] optimize localossObject implement logic --- internal/dao/storage/localoss.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/dao/storage/localoss.go b/internal/dao/storage/localoss.go index 31d52a79..3278d85e 100644 --- a/internal/dao/storage/localoss.go +++ b/internal/dao/storage/localoss.go @@ -118,17 +118,24 @@ func (s *localossCreateTempDirServant) PersistObject(objectKey string) error { if err != nil { return err } + needCloseReader := true + defer func() { + if needCloseReader { + reader.Close() + } + }() + writer, err := os.Create(s.savePath + objectKey) if err != nil { return err } defer writer.Close() if _, err = io.Copy(writer, reader); err != nil { - reader.Close() return err } - reader.Close() + reader.Close() + needCloseReader = false if err = os.Remove(tmpObjPath); err != nil { return err }