Skip to content

Commit

Permalink
Fixing deleting temp files and incorrect zip file
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzoAiello authored and claudiodangelis committed Dec 20, 2023
1 parent 5d848c1 commit ab0abd5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 73 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807
github.com/glendc/go-external-ip v0.1.0
github.com/jhoonb/archivex v0.0.0-20180718040744-0488e4ce1681
github.com/manifoldco/promptui v0.9.0
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086
Expand Down
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jhoonb/archivex v0.0.0-20180718040744-0488e4ce1681 h1:EiEjLram6Y0WXygV4WyzKmTr3XaR4CD3tvjdTrsk3cU=
github.com/jhoonb/archivex v0.0.0-20180718040744-0488e4ce1681/go.mod h1:GN1Mg/uXQ6qwXA0HypnUO3xlcQJS9/y68EsHNeuuRa4=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down Expand Up @@ -2517,4 +2519,4 @@ rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
96 changes: 24 additions & 72 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package util

import (
"archive/zip"
"crypto/rand"
"encoding/base64"
"errors"
Expand All @@ -15,6 +14,8 @@ import (
"strconv"
"strings"
"time"

"github.com/jhoonb/archivex"
)

// Expand tilde in paths
Expand All @@ -32,105 +33,56 @@ func Expand(input string) string {
return input
}

// add folder to the zip file
func addFolderToZip(zipWriter *zip.Writer, source, target string) error {

//explore the folder and add all to the zip
return filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name = path

if info.IsDir() {
header.Name += "/"
} else {
header.Method = zip.Deflate
}

writer, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}

if info.IsDir() {
return nil
}

file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()

_, err = io.Copy(writer, file)
return err
})
}

// add files to the zip file
func addFileToZip(zipWriter *zip.Writer, fileToAdd string) error {
f1, err := os.Open(fileToAdd)
if err != nil {
return err
}
defer f1.Close()

w1, err := zipWriter.Create(filepath.Base(fileToAdd))
if err != nil {
panic(err)
}
if _, err := io.Copy(w1, f1); err != nil {
panic(err)
}

return nil
}

// ZipFiles and return the resulting zip's filename
func ZipFiles(files []string) (string, error) {
//create temporary file
zip := new(archivex.ZipFile)
tmpfile, err := os.CreateTemp("", "qrcp")
if err != nil {
return "", err
}
tempFileName := tmpfile.Name() + ".zip"
tmpfile.Close()
if err := os.Rename(tmpfile.Name(), tempFileName); err != nil {
if err := os.Rename(tmpfile.Name(), tmpfile.Name()+".zip"); err != nil {
return "", err
}

//create zip file
zipFile, err := os.Create(tempFileName)
tmpfile,err = os.OpenFile(tmpfile.Name()+".zip", os.O_RDWR, 0644)
if err != nil {
return "", err
}
defer zipFile.Close()
if err := zip.CreateWriter(tmpfile.Name(),tmpfile); err != nil {
return "", err
}

//add files and folder in the zip
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
for _, filename := range files {
fileinfo, err := os.Stat(filename)
if err != nil {
return "", err
}
if fileinfo.IsDir() {
if err := addFolderToZip(zipWriter, filename, tempFileName); err != nil {
if err := zip.AddAll(filename, true); err != nil {
return "", err
}
} else {
if err := addFileToZip(zipWriter, filename); err != nil {
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
if err := zip.Add(filename, file, fileinfo); err != nil {
return "", err
}
}
}
return tempFileName, nil

if err := zip.Writer.Close(); err != nil {
return "", err
}
if err:= tmpfile.Close(); err != nil {
return "", err
}

return zip.Name, nil
}

// GetRandomURLPath returns a random string of 4 alphanumeric characters
Expand Down

0 comments on commit ab0abd5

Please sign in to comment.