Skip to content

Commit

Permalink
added changes to devide the chunks in bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
vinay033 committed Jan 24, 2022
1 parent fe14061 commit 9486ee5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
55 changes: 33 additions & 22 deletions components/automate-gateway/gateway/services.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gateway

import (
"bufio"
"bytes"
"context"
"encoding/json"
Expand Down Expand Up @@ -964,11 +965,10 @@ func (s *Server) DeploymentStatusHandler(w http.ResponseWriter, r *http.Request)

// UploadZipFile used to upload the migration zip file of infraProxy
func (s *Server) UploadZipFile(w http.ResponseWriter, r *http.Request) {
var fileData []byte
var cType, fileName string

var content bytes.Buffer
file, _, err := r.FormFile("file")
file, metaData, err := r.FormFile("file")

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand All @@ -981,20 +981,14 @@ func (s *Server) UploadZipFile(w http.ResponseWriter, r *http.Request) {
return
}

fileData = content.Bytes()
cType = r.URL.Query().Get("contentType")

owner := r.URL.Query().Get("owner")
cType = r.Header.Get("Content-type")
fileName = metaData.Filename

const (
action = "infra:infraServers:sync"
resource = "infra:infraServers"
)

fmt.Println("-----------------------", r.URL)
fmt.Println("-----------------------", cType)
fmt.Println("-----------------------", owner)

ctx, err := s.authRequest(r, resource, action)
if err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
Expand All @@ -1014,18 +1008,35 @@ func (s *Server) UploadZipFile(w http.ResponseWriter, r *http.Request) {
return
}

request := infra_proxy.UploadZipFileRequest{
Chunk: &infra_proxy.Chunk{Data: fileData},
Meta: &infra_proxy.Metadata{
ContentType: cType,
Name: fileName,
},
}
err = stream.Send(&request)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
// break the data in chunks
reader := bufio.NewReader(&content)
buffer := make([]byte, 1024)

for {
n, err := reader.Read(buffer)
if err == io.EOF {
break
}

if err != nil {
log.Fatal("cannot read chunk to buffer: ", err)
}

request := infra_proxy.UploadZipFileRequest{
Chunk: &infra_proxy.Chunk{Data: buffer[:n]},
Meta: &infra_proxy.Metadata{
ContentType: cType,
Name: fileName,
},
}

err = stream.Send(&request)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}

reply, err := stream.CloseAndRecv()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand Down
15 changes: 9 additions & 6 deletions components/infra-proxy-service/server/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,35 @@ func saveFile(migrationId string, filename string, fileData bytes.Buffer) error

// UploadFile Takes the stream of data to upload a file
func (*Server) UploadFile(stream service.MigrationDataService_UploadFileServer) error {
in, err := stream.Recv()
if err != io.EOF && err != nil {
return err
}
var fileName string
migrationId, err := createMigrationId()
if err != nil {
log.WithError(err).Error("Unable to create migration id")
}
folderName := in.GetMeta().Name

fileData := bytes.Buffer{}

for {
req, err := stream.Recv()

if err == io.EOF {
break
}
if err != nil {
return err
}

fileName = req.GetMeta().GetName()
chunk := req.GetChunk().Data

_, err = fileData.Write(chunk)
if err != nil {
return err
}
}
err = saveFile(migrationId, folderName, fileData)

err = saveFile(migrationId, fileName, fileData)

if err != nil {
return err
}
Expand Down

0 comments on commit 9486ee5

Please sign in to comment.