Skip to content

Commit

Permalink
fix(zip): delete temporary file after transferring it (#304)
Browse files Browse the repository at this point in the history
* Fixing deleting temporary files

* style(util):

* chore(deps): go mod tidy

---------

Co-authored-by: Claudio d'Angelis <claudiodangelis@gmail.com>
  • Loading branch information
vincenzoAiello and claudiodangelis authored Dec 8, 2023
1 parent 8f16bfb commit 05a0d3c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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.7.0
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1045,8 +1045,6 @@ 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
94 changes: 77 additions & 17 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

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

"github.com/jhoonb/archivex"
)

// Expand tilde in paths
Expand All @@ -33,44 +32,105 @@ 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) {
zip := new(archivex.ZipFile)
tmpfile, err := ioutil.TempFile("", "qrcp")
//create temporary file
tmpfile, err := os.CreateTemp("", "qrcp")
if err != nil {
return "", err
}
tempFileName := tmpfile.Name() + ".zip"
tmpfile.Close()
if err := os.Rename(tmpfile.Name(), tmpfile.Name()+".zip"); err != nil {
if err := os.Rename(tmpfile.Name(), tempFileName); err != nil {
return "", err
}
if err := zip.Create(tmpfile.Name() + ".zip"); err != nil {

//create zip file
zipFile, err := os.Create(tempFileName)
if err != nil {
return "", err
}
defer zipFile.Close()

//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 := zip.AddAll(filename, true); err != nil {
if err := addFolderToZip(zipWriter, filename, tempFileName); err != nil {
return "", err
}
} else {
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
if err := zip.Add(filename, file, fileinfo); err != nil {
if err := addFileToZip(zipWriter, filename); err != nil {
return "", err
}
}
}
if err := zip.Close(); err != nil {
return "", nil
}
return zip.Name, nil
return tempFileName, nil
}

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

0 comments on commit 05a0d3c

Please sign in to comment.