Skip to content

Commit

Permalink
Fixed namespace folder creation, and added fileutils
Browse files Browse the repository at this point in the history
  • Loading branch information
VikingPingvin committed Dec 3, 2020
1 parent 371aa41 commit 6398d81
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
1 change: 1 addition & 0 deletions locker/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func parseAndSendMetaData(connection net.Conn, inputData *InputData) (fileInfo o

log.Info().
Str("file name", fileInfo.Name()).
Str("Namespace", fmt.Sprintf("%s/%s/%s", inputData.NameSpace, inputData.Project, inputData.JobID)).
Str("size", fmt.Sprintf("%d", fileInfo.Size())).
Str("hash", fmt.Sprintf("%v", inputData.FileHash)).
Str("id", inputData.ID.String()).
Expand Down
30 changes: 30 additions & 0 deletions locker/fileutils/fileutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fileutils

import (
"os"
"path/filepath"

"github.com/rs/zerolog/log"
)

func EnsurePathExists(elements ...string) error {
fullPath := filepath.Join(elements...)

err := os.MkdirAll(fullPath, os.ModeDir)
if err != nil {
log.Err(err).Msg("Cannot create folder structure")
}
return err
}

func CreateFileAt() {

}

func MoveFile(oldPath string, newPath string) error {
err := os.Rename(oldPath, newPath)
if err != nil {
log.Err(err).Msg("File move failed!")
}
return err
}
3 changes: 3 additions & 0 deletions locker/fileutils/fileutils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package fileutils

// Test EnsurePathexists with 0, 1 and more []string
35 changes: 27 additions & 8 deletions locker/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"time"
"vikingPingvin/locker/locker/fileutils"
"vikingPingvin/locker/locker/messaging"
"vikingPingvin/locker/locker/messaging/protobuf"

Expand All @@ -27,9 +28,12 @@ type connectionType struct {

// Struct containing file data from received MetaData message
type metaInfo struct {
fileHash []byte
fileName string
ID []byte
fileHash []byte
fileName string
namespace string
project string
jobID string
ID []byte
}

type Server interface {
Expand Down Expand Up @@ -74,7 +78,7 @@ func handleConnection(connection net.Conn) {
artifactReceived := false
var artifactPath *os.File

metaData := metaInfo{}
metaData := &metaInfo{}

timeoutDuration := 5 * time.Second
invalidCounterMax := 10
Expand Down Expand Up @@ -153,18 +157,33 @@ func handleConnection(connection net.Conn) {
if receptionSuccesful {
//Rename file
baseDir := filepath.Dir(artifactPath.Name())
newPath := filepath.Join(baseDir, metaData.fileName)
os.Rename(artifactPath.Name(), newPath)
newPath := filepath.Join(baseDir,
metaData.namespace,
metaData.project,
metaData.jobID,
metaData.fileName)
err := fileutils.EnsurePathExists(filepath.Dir(newPath))
if err == nil {
err = fileutils.MoveFile(artifactPath.Name(), newPath)
if err != nil {
log.Err(err).Msg("SAD")
}
}
log.Info().Msgf("Artifact ready: %s", newPath)
}
sendAckMessage(connection, &metaData, receptionSuccesful)
sendAckMessage(connection, metaData, receptionSuccesful)
}

func handleProtoMeta(metaMessage *protobuf.FileMeta) (file *os.File, metaData metaInfo) {
func handleProtoMeta(metaMessage *protobuf.FileMeta) (file *os.File, _ *metaInfo) {

metaData := &metaInfo{}

xidValue, _ := xid.FromBytes(metaMessage.GetId())
metaData.fileHash = metaMessage.GetHash()
metaData.fileName = metaMessage.GetFilename()
metaData.namespace = metaMessage.GetNamespace()
metaData.project = metaMessage.GetProject()
metaData.jobID = metaMessage.GetJobID()
metaData.ID = metaMessage.GetId()

log.Info().
Expand Down

0 comments on commit 6398d81

Please sign in to comment.