Skip to content

Create parent folder for files in archives #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"

log "github.com/Sirupsen/logrus"
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tools/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions tools/download_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
3 changes: 2 additions & 1 deletion upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down