(c *context.Context, f form.UploadRepoFile)
| 428 | } |
| 429 | |
| 430 | func UploadFilePost(c *context.Context, f form.UploadRepoFile) { |
| 431 | c.PageIs("Upload") |
| 432 | renderUploadSettings(c) |
| 433 | |
| 434 | oldBranchName := c.Repo.BranchName |
| 435 | branchName := oldBranchName |
| 436 | |
| 437 | if f.IsNewBrnach() { |
| 438 | branchName = f.NewBranchName |
| 439 | } |
| 440 | |
| 441 | // 🚨 SECURITY: Prevent path traversal. |
| 442 | f.TreePath = pathutil.Clean(f.TreePath) |
| 443 | treeNames, treePaths := getParentTreeFields(f.TreePath) |
| 444 | if len(treeNames) == 0 { |
| 445 | // We must at least have one element for user to input. |
| 446 | treeNames = []string{""} |
| 447 | } |
| 448 | |
| 449 | c.Data["TreePath"] = f.TreePath |
| 450 | c.Data["TreeNames"] = treeNames |
| 451 | c.Data["TreePaths"] = treePaths |
| 452 | c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + branchName |
| 453 | c.Data["commit_summary"] = f.CommitSummary |
| 454 | c.Data["commit_message"] = f.CommitMessage |
| 455 | c.Data["commit_choice"] = f.CommitChoice |
| 456 | c.Data["new_branch_name"] = branchName |
| 457 | |
| 458 | if c.HasError() { |
| 459 | c.HTML(http.StatusBadRequest, tmplEditorUpload) |
| 460 | return |
| 461 | } |
| 462 | |
| 463 | if oldBranchName != branchName { |
| 464 | if _, err := c.Repo.Repository.GetBranch(branchName); err == nil { |
| 465 | c.FormErr("NewBranchName") |
| 466 | c.RenderWithErr(c.Tr("repo.editor.branch_already_exists", branchName), http.StatusUnprocessableEntity, tmplEditorUpload, &f) |
| 467 | return |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | var newTreePath string |
| 472 | for _, part := range treeNames { |
| 473 | newTreePath = path.Join(newTreePath, part) |
| 474 | entry, err := c.Repo.Commit.TreeEntry(newTreePath) |
| 475 | if err != nil { |
| 476 | if gitutil.IsErrRevisionNotExist(err) { |
| 477 | // Means there is no item with that name, so we're good |
| 478 | break |
| 479 | } |
| 480 | |
| 481 | c.Error(err, "get tree entry") |
| 482 | return |
| 483 | } |
| 484 | |
| 485 | // User can only upload files to a directory. |
| 486 | if !entry.IsTree() { |
| 487 | c.FormErr("TreePath") |
nothing calls this directly
no test coverage detected