Skip to content

Commit 213356e

Browse files
authored
Merge pull request #238 from arduino/fix_download_tool
Create parent folder for files in archives
2 parents 570005a + 2f79099 commit 213356e

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

conn.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"fmt"
1616
"io/ioutil"
1717
"net/http"
18+
"os"
1819
"path/filepath"
1920

2021
log "github.com/Sirupsen/logrus"
@@ -119,13 +120,27 @@ func uploadHandler(c *gin.Context) {
119120
var filePaths []string
120121
filePaths = append(filePaths, filePath)
121122

123+
tmpdir, err := ioutil.TempDir("", "extrafiles")
124+
if err != nil {
125+
c.String(http.StatusBadRequest, err.Error())
126+
return
127+
}
128+
122129
for _, extraFile := range data.ExtraFiles {
123-
path := filepath.Join(filepath.Dir(filePath), extraFile.Filename)
130+
path := filepath.Join(tmpdir, extraFile.Filename)
124131
filePaths = append(filePaths, path)
125132
log.Printf("Saving %s on %s", extraFile.Filename, path)
133+
134+
err = os.MkdirAll(filepath.Dir(path), 0744)
135+
if err != nil {
136+
c.String(http.StatusBadRequest, err.Error())
137+
return
138+
}
139+
126140
err := ioutil.WriteFile(path, extraFile.Hex, 0644)
127141
if err != nil {
128-
log.Printf(err.Error())
142+
c.String(http.StatusBadRequest, err.Error())
143+
return
129144
}
130145
}
131146

@@ -135,7 +150,7 @@ func uploadHandler(c *gin.Context) {
135150

136151
go func() {
137152
// Resolve commandline
138-
commandline, err := upload.PartiallyResolve(data.Board, filePath, data.Commandline, data.Extra, &Tools)
153+
commandline, err := upload.PartiallyResolve(data.Board, filePath, tmpdir, data.Commandline, data.Extra, &Tools)
139154
if err != nil {
140155
send(map[string]string{uploadStatusStr: "Error", "Msg": err.Error()})
141156
return

tools/download.go

+8
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ func stringInSlice(str string, list []string) bool {
319319
}
320320

321321
func findBaseDir(dirList []string) string {
322+
if len(dirList) == 1 {
323+
return filepath.Dir(dirList[0]) + "/"
324+
}
322325
baseDir := ""
323326
// https://github.com/backdrop-ops/contrib/issues/55#issuecomment-73814500
324327
dontdiff := []string{"pax_global_header"}
@@ -419,6 +422,11 @@ func extractTarGz(body []byte, location string) (string, error) {
419422
path := filepath.Join(location, strings.Replace(header.Name, basedir, "", -1))
420423
info := header.FileInfo()
421424

425+
// Create parent folder
426+
if err = os.MkdirAll(filepath.Dir(path), info.Mode()); err != nil {
427+
return location, err
428+
}
429+
422430
if info.IsDir() {
423431
if err = os.MkdirAll(path, info.Mode()); err != nil {
424432
return location, err

tools/download_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package tools
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func Test_findBaseDir(t *testing.T) {
9+
cases := []struct {
10+
dirList []string
11+
want string
12+
}{
13+
{[]string{"bin/bossac"}, "bin/"},
14+
{[]string{"bin/", "bin/bossac"}, "bin/"},
15+
{[]string{"bin/", "bin/bossac", "example"}, ""},
16+
}
17+
for _, tt := range cases {
18+
t.Run(fmt.Sprintln(tt.dirList), func(t *testing.T) {
19+
if got := findBaseDir(tt.dirList); got != tt.want {
20+
t.Errorf("findBaseDir() = %v, want %v", got, tt.want)
21+
}
22+
})
23+
}
24+
}

upload/upload.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ type Extra struct {
5151

5252
// PartiallyResolve replaces some symbols in the commandline with the appropriate values
5353
// it can return an error when looking a variable in the Locater
54-
func PartiallyResolve(board, file, commandline string, extra Extra, t Locater) (string, error) {
54+
func PartiallyResolve(board, file, platformPath, commandline string, extra Extra, t Locater) (string, error) {
5555
commandline = strings.Replace(commandline, "{build.path}", filepath.ToSlash(filepath.Dir(file)), -1)
5656
commandline = strings.Replace(commandline, "{build.project_name}", strings.TrimSuffix(filepath.Base(file), filepath.Ext(filepath.Base(file))), -1)
5757
commandline = strings.Replace(commandline, "{network.password}", extra.Auth.Password, -1)
58+
commandline = strings.Replace(commandline, "{runtime.platform.path}", platformPath, -1)
5859

5960
if extra.Verbose == true {
6061
commandline = strings.Replace(commandline, "{upload.verbose}", extra.ParamsVerbose, -1)

0 commit comments

Comments
 (0)