diff --git a/conn.go b/conn.go index 7c2a62f89..450565dad 100644 --- a/conn.go +++ b/conn.go @@ -15,6 +15,7 @@ import ( "fmt" "io/ioutil" "net/http" + "os" "path/filepath" log "github.com/Sirupsen/logrus" @@ -119,13 +120,27 @@ func uploadHandler(c *gin.Context) { var filePaths []string filePaths = append(filePaths, filePath) + tmpdir, err := ioutil.TempDir("", "extrafiles") + if err != nil { + c.String(http.StatusBadRequest, err.Error()) + return + } + for _, extraFile := range data.ExtraFiles { - path := filepath.Join(filepath.Dir(filePath), extraFile.Filename) + path := filepath.Join(tmpdir, extraFile.Filename) filePaths = append(filePaths, path) log.Printf("Saving %s on %s", extraFile.Filename, path) + + err = os.MkdirAll(filepath.Dir(path), 0744) + if err != nil { + c.String(http.StatusBadRequest, err.Error()) + return + } + err := ioutil.WriteFile(path, extraFile.Hex, 0644) if err != nil { - log.Printf(err.Error()) + c.String(http.StatusBadRequest, err.Error()) + return } } @@ -135,7 +150,7 @@ func uploadHandler(c *gin.Context) { go func() { // Resolve commandline - commandline, err := upload.PartiallyResolve(data.Board, filePath, data.Commandline, data.Extra, &Tools) + commandline, err := upload.PartiallyResolve(data.Board, filePath, tmpdir, data.Commandline, data.Extra, &Tools) if err != nil { send(map[string]string{uploadStatusStr: "Error", "Msg": err.Error()}) return diff --git a/tools/download.go b/tools/download.go index c3ca1b8ae..a40649139 100644 --- a/tools/download.go +++ b/tools/download.go @@ -319,6 +319,9 @@ func stringInSlice(str string, list []string) bool { } func findBaseDir(dirList []string) string { + if len(dirList) == 1 { + return filepath.Dir(dirList[0]) + "/" + } baseDir := "" // https://github.com/backdrop-ops/contrib/issues/55#issuecomment-73814500 dontdiff := []string{"pax_global_header"} @@ -419,6 +422,11 @@ func extractTarGz(body []byte, location string) (string, error) { path := filepath.Join(location, strings.Replace(header.Name, basedir, "", -1)) info := header.FileInfo() + // Create parent folder + if err = os.MkdirAll(filepath.Dir(path), info.Mode()); err != nil { + return location, err + } + if info.IsDir() { if err = os.MkdirAll(path, info.Mode()); err != nil { return location, err diff --git a/tools/download_test.go b/tools/download_test.go new file mode 100644 index 000000000..2ef79d721 --- /dev/null +++ b/tools/download_test.go @@ -0,0 +1,24 @@ +package tools + +import ( + "fmt" + "testing" +) + +func Test_findBaseDir(t *testing.T) { + cases := []struct { + dirList []string + want string + }{ + {[]string{"bin/bossac"}, "bin/"}, + {[]string{"bin/", "bin/bossac"}, "bin/"}, + {[]string{"bin/", "bin/bossac", "example"}, ""}, + } + for _, tt := range cases { + t.Run(fmt.Sprintln(tt.dirList), func(t *testing.T) { + if got := findBaseDir(tt.dirList); got != tt.want { + t.Errorf("findBaseDir() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/upload/upload.go b/upload/upload.go index 6c0563408..765ce1b2b 100644 --- a/upload/upload.go +++ b/upload/upload.go @@ -51,10 +51,11 @@ type Extra struct { // PartiallyResolve replaces some symbols in the commandline with the appropriate values // it can return an error when looking a variable in the Locater -func PartiallyResolve(board, file, commandline string, extra Extra, t Locater) (string, error) { +func PartiallyResolve(board, file, platformPath, commandline string, extra Extra, t Locater) (string, error) { commandline = strings.Replace(commandline, "{build.path}", filepath.ToSlash(filepath.Dir(file)), -1) commandline = strings.Replace(commandline, "{build.project_name}", strings.TrimSuffix(filepath.Base(file), filepath.Ext(filepath.Base(file))), -1) commandline = strings.Replace(commandline, "{network.password}", extra.Auth.Password, -1) + commandline = strings.Replace(commandline, "{runtime.platform.path}", platformPath, -1) if extra.Verbose == true { commandline = strings.Replace(commandline, "{upload.verbose}", extra.ParamsVerbose, -1)