diff --git a/codegen.go b/codegen.go index 4f2dc77..a73d9fc 100644 --- a/codegen.go +++ b/codegen.go @@ -86,12 +86,28 @@ func FormatAppfile(filedata string) (string, string) { } // Streams the data into a zip to be used for a cloud function -func StreamZipdata(ctx context.Context, identifier, pythoncode, requirements string) (string, error) { +func StreamZipdata(ctx context.Context, identifier, pythoncode, requirements, bucketName string) (string, error) { filename := fmt.Sprintf("generated_cloudfunctions/%s.zip", identifier) buf := new(bytes.Buffer) zipWriter := zip.NewWriter(buf) + if project.Environment == "cloud" { + client, err := storage.NewClient(ctx) + if err != nil { + log.Printf("Failed to create datastore client: %v", err) + return filename, err + } + + bucket := client.Bucket(bucketName) + + obj := bucket.Object(filename) + storageWriter := obj.NewWriter(ctx) + defer storageWriter.Close() + + zipWriter = zip.NewWriter(storageWriter) + } + zipFile, err := zipWriter.Create("main.py") if err != nil { log.Printf("Packing failed to create zip file from bucket: %v", err) @@ -118,6 +134,12 @@ func StreamZipdata(ctx context.Context, identifier, pythoncode, requirements str return filename, err } + //src := client.Bucket(bucketName).Object(fmt.Sprintf("%s/baseline/%s", basePath, file)) + //dst := client.Bucket(bucketName).Object(fmt.Sprintf("%s/%s", appPath, file)) + //if _, err := dst.CopierFrom(src).Run(ctx); err != nil { + // return "", err + //} + return filename, nil } diff --git a/shared.go b/shared.go index 31480d8..4ef8c15 100644 --- a/shared.go +++ b/shared.go @@ -1045,121 +1045,3 @@ func RunInit(dbclient datastore.Client, gceProject, environment string) ShuffleS return project } - -func HandleEditOrg(resp http.ResponseWriter, request *http.Request) { - cors := handleCors(resp, request) - if cors { - return - } - - user, err := handleApiAuthentication(resp, request) - if err != nil { - log.Printf("Api authentication failed in cloud setup: %s", err) - resp.WriteHeader(401) - resp.Write([]byte(`{"success": false}`)) - return - } - - if user.Role != "admin" { - log.Printf("Not admin.") - resp.WriteHeader(401) - resp.Write([]byte(`{"success": false, "reason": "Not admin"}`)) - return - } - - body, err := ioutil.ReadAll(request.Body) - if err != nil { - resp.WriteHeader(401) - resp.Write([]byte(`{"success": false, "reason": "Failed reading body"}`)) - return - } - - type ReturnData struct { - Image string `json:"image" datastore:"image"` - Name string `json:"name" datastore:"name"` - Description string `json:"description" datastore:"description"` - OrgId string `json:"org_id" datastore:"org_id"` - } - - var tmpData ReturnData - err = json.Unmarshal(body, &tmpData) - if err != nil { - log.Printf("Failed unmarshalling test: %s", err) - resp.WriteHeader(401) - resp.Write([]byte(`{"success": false}`)) - return - } - - var fileId string - location := strings.Split(request.URL.String(), "/") - if location[1] == "api" { - if len(location) <= 4 { - log.Printf("Path too short: %d", len(location)) - resp.WriteHeader(401) - resp.Write([]byte(`{"success": false}`)) - return - } - - fileId = location[4] - } - - if tmpData.OrgId != user.ActiveOrg.Id || fileId != user.ActiveOrg.Id { - log.Printf("User can't edit the org") - resp.WriteHeader(401) - resp.Write([]byte(`{"success": false, "No permission to edit this org"}`)) - return - } - - ctx := context.Background() - org, err := getOrg(ctx, tmpData.OrgId) - if err != nil { - log.Printf("Organization doesn't exist: %s", err) - resp.WriteHeader(401) - resp.Write([]byte(`{"success": false}`)) - return - } - - admin := false - userFound := false - for _, inneruser := range org.Users { - if inneruser.Id == user.Id { - userFound = true - if inneruser.Role == "admin" { - admin = true - } - - break - } - } - - if !userFound { - log.Printf("User %s doesn't exist in organization for edit %s", user.Id, org.Id) - resp.WriteHeader(401) - resp.Write([]byte(`{"success": false}`)) - return - } - - if !admin { - log.Printf("User %s doesn't have edit rights to %s", user.Id, org.Id) - resp.WriteHeader(401) - resp.Write([]byte(`{"success": false}`)) - return - } - - org.Image = tmpData.Image - org.Name = tmpData.Name - org.Description = tmpData.Description - //log.Printf("Org: %#v", org) - err = setOrg(ctx, *org, org.Id) - if err != nil { - log.Printf("User %s doesn't have edit rights to %s", user.Id, org.Id) - resp.WriteHeader(401) - resp.Write([]byte(`{"success": false}`)) - return - } - - log.Printf("SUCCESSFULLY UPDATED ORG") - resp.WriteHeader(200) - resp.Write([]byte(fmt.Sprintf(`{"success": true, "reason": "Successfully updated org"}`))) - -}