Skip to content

Commit 711ed3c

Browse files
authored
Merge pull request #9 from kaasops/fix-dockerimage
check mime type before unarchive
2 parents 822f68e + e79b1bd commit 711ed3c

File tree

1 file changed

+76
-16
lines changed

1 file changed

+76
-16
lines changed

pkg/configreloader/configreloader.go

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@ import (
55
"flag"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"log"
109
"net/http"
1110
"os"
1211
"path/filepath"
1312
"strconv"
13+
"strings"
1414
"time"
1515

1616
fsnotify "github.com/fsnotify/fsnotify"
1717
"github.com/kaasops/config-reloader/pkg/metrics"
1818
)
1919

20+
const (
21+
gziptype = "application/gzip"
22+
xgziptype = "application/x-gzip"
23+
gzExtention = ".gz"
24+
)
25+
2026
func New() (*ConfigReloader, error) {
2127
cfg := &ConfigReloader{}
2228
cfg.InitMode = flag.Bool("init-mode", false, "Init mode for unarchive files. Works only if volume-dir-archive exist. Default - false")
@@ -236,20 +242,29 @@ func (cfg *ConfigReloader) sendWebHook() {
236242
}
237243

238244
func (cfg *ConfigReloader) unarchiveDir(path string) error {
239-
// fmt.Println(path)
240-
// kuberPath := path + "/..data"
241-
// fmt.Println(kuberPath)
242-
files, err := ioutil.ReadDir(path)
245+
246+
files, err := os.ReadDir(path)
243247
if err != nil {
244248
return err
245249
}
246250

247251
for _, file := range files {
248-
if file.Name()[len(file.Name())-3:] != ".gz" {
249-
continue
250-
}
251252
fullFilePath := path + "/" + file.Name()
252-
err := cfg.unarchiveFile(fullFilePath)
253+
isGzipArchive, err := isGzipArchive(fullFilePath)
254+
255+
if err != nil {
256+
return err
257+
}
258+
259+
if !isGzipArchive {
260+
log.Printf("File %s is not a gzip archive", fullFilePath)
261+
if err = copy(fullFilePath, *cfg.DirForUnarchive+"/"+filepath.Base(fullFilePath)); err != nil {
262+
return err
263+
}
264+
return nil
265+
}
266+
267+
err = cfg.unarchiveFile(fullFilePath)
253268
if err != nil {
254269
return err
255270
}
@@ -258,19 +273,18 @@ func (cfg *ConfigReloader) unarchiveDir(path string) error {
258273
}
259274

260275
func (cfg *ConfigReloader) unarchiveFile(path string) error {
261-
outFileName := *cfg.DirForUnarchive + "/" + filepath.Base(path)[0:len(filepath.Base(path))-len(filepath.Ext(filepath.Base(path)))]
262-
log.Printf("Unarhive file from %s to %s", path, outFileName)
276+
outFileName := *cfg.DirForUnarchive + "/" + strings.TrimSuffix(filepath.Base(path), gzExtention)
263277

264-
// if path[len(path)-3:] != ".gz" {
265-
// return fmt.Errorf("File %s is not a .gz archive. Do nothing", path)
266-
// }
278+
f, err := os.Open(path)
267279

268-
gzipFile, err := os.Open(path)
269280
if err != nil {
270281
return err
271282
}
272283

273-
gzipReader, err := gzip.NewReader(gzipFile)
284+
defer f.Close()
285+
286+
log.Printf("Unarhive file from %s to %s", path, outFileName)
287+
gzipReader, err := gzip.NewReader(f)
274288
if err != nil {
275289
return err
276290
}
@@ -288,5 +302,51 @@ func (cfg *ConfigReloader) unarchiveFile(path string) error {
288302
if err != nil {
289303
return err
290304
}
305+
306+
return nil
307+
}
308+
309+
func isGzipArchive(path string) (bool, error) {
310+
f, err := os.Open(path)
311+
312+
if err != nil {
313+
return false, err
314+
}
315+
316+
defer f.Close()
317+
318+
// Read first 512 bytes to determine the Content-Type of the given data
319+
buff := make([]byte, 512)
320+
321+
_, err = f.Read(buff)
322+
323+
if err != nil {
324+
return false, err
325+
}
326+
327+
filetype := http.DetectContentType(buff)
328+
if filetype == gziptype || filetype == xgziptype {
329+
return true, nil
330+
}
331+
return false, nil
332+
}
333+
334+
func copy(fromFile string, toFile string) error {
335+
from, err := os.Open(fromFile)
336+
if err != nil {
337+
log.Fatal(err)
338+
}
339+
defer from.Close()
340+
341+
to, err := os.Create(toFile)
342+
if err != nil {
343+
log.Fatal(err)
344+
}
345+
defer to.Close()
346+
347+
_, err = io.Copy(to, from)
348+
if err != nil {
349+
log.Fatal(err)
350+
}
291351
return nil
292352
}

0 commit comments

Comments
 (0)