Skip to content

Commit

Permalink
Update file fix
Browse files Browse the repository at this point in the history
  • Loading branch information
frikky committed Oct 24, 2024
1 parent a73cad0 commit d15d62a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
16 changes: 15 additions & 1 deletion files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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"}`)))
Expand Down

0 comments on commit d15d62a

Please sign in to comment.