From d15d62a427e7d92e2293699b8041d7451b980e48 Mon Sep 17 00:00:00 2001 From: Frikky Date: Thu, 24 Oct 2024 12:20:47 +0200 Subject: [PATCH] Update file fix --- files.go | 16 +++++++++++++++- shared.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/files.go b/files.go index c8ae35d..6d4271c 100755 --- a/files.go +++ b/files.go @@ -1170,6 +1170,7 @@ func HandleEditFile(resp http.ResponseWriter, request *http.Request) { resp.Write([]byte(`{"success": false}`)) return } + user.ActiveOrg.Id = orgId user.Username = "Execution File API" } @@ -1228,7 +1229,7 @@ func HandleEditFile(resp http.ResponseWriter, request *http.Request) { if project.Environment == "cloud" && len(body) > maxFileSize { log.Printf("[ERROR] Max filesize is 10MB in cloud environment") resp.WriteHeader(400) - resp.Write([]byte(`{"success": false, "reason": "File too large. Max is 10mb"}`)) + resp.Write([]byte(`{"success": false, "reason": "File too large. Max is 10mb per file."}`)) return } @@ -1249,6 +1250,19 @@ func HandleEditFile(resp http.ResponseWriter, request *http.Request) { parsedKey = fmt.Sprintf("%s_%s", user.ActiveOrg.Id, file.ReferenceFileId) } + if strings.HasPrefix(string(body), "--") && strings.Contains(string(body), "Content-Disposition") { + body = []byte(strings.TrimSpace(string(body))) + bodysplit := strings.Split(string(body), "\n") + if len(bodysplit) > 3 { + // Remove line 1, 2 and last + body = []byte(strings.Join(bodysplit[2:len(bodysplit)-1], "\n")) + } + + // Trim newlines + body = []byte(strings.TrimSpace(string(body))) + log.Printf("[DEBUG] Found multipart form data in the body itself - autocleanup ran.") + } + fileId, err = uploadFile(ctx, file, parsedKey, body) if err != nil { log.Printf("[ERROR] Failed to upload file with ID %s: %s", fileId, err) diff --git a/shared.go b/shared.go index 28da41b..2be1f05 100755 --- a/shared.go +++ b/shared.go @@ -11259,6 +11259,8 @@ func HandleCreateSubOrg(resp http.ResponseWriter, request *http.Request) { return } + + // Update all admins to have access to this suborg for _, loopUser := range parentOrg.Users { if loopUser.Role != "admin" { @@ -11311,6 +11313,42 @@ func HandleCreateSubOrg(resp http.ResponseWriter, request *http.Request) { return } + // 1. Get environments for parent + // 2. Create an environment for the child with the same name + if project.Environment != "cloud" { + environments, err := GetEnvironments(ctx, parentOrg.Id) + if err != nil { + log.Printf("[ERROR] Failed getting environments for parent org: %s", err) + } else { + defaultFoundEnv := Environment{} + for _, parentEnv := range environments { + if parentEnv.Default { + defaultFoundEnv = parentEnv + break + } + } + + if len(defaultFoundEnv.Name) > 0 && defaultFoundEnv.Type != "cloud" { + item := Environment{ + Name: defaultFoundEnv.Name, + Type: defaultFoundEnv.Type, + OrgId: newOrg.Id, + Default: true, + Id: uuid.NewV4().String(), + + Auth: defaultFoundEnv.Auth, + } + + err := SetEnvironment(ctx, &item) + if err != nil { + log.Printf("[ERROR] Failed setting up new environment for new org: %s", err) + } else { + log.Printf("[INFO] Successfully created new parent-duplicated environment for new suborg %s", newOrg.Id) + } + } + } + } + log.Printf("[INFO] User %s SUCCESSFULLY ADDED child org %s (%s) for parent %s (%s)", user.Username, newOrg.Name, newOrg.Id, parentOrg.Name, parentOrg.Id) resp.WriteHeader(200) resp.Write([]byte(fmt.Sprintf(`{"success": true, "reason": "Successfully created new sub-org"}`)))