|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package markup |
| 5 | + |
| 6 | +import ( |
| 7 | + "bufio" |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "html/template" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "code.gitea.io/gitea/models/perm/access" |
| 14 | + "code.gitea.io/gitea/models/repo" |
| 15 | + "code.gitea.io/gitea/models/unit" |
| 16 | + "code.gitea.io/gitea/modules/gitrepo" |
| 17 | + "code.gitea.io/gitea/modules/indexer/code" |
| 18 | + "code.gitea.io/gitea/modules/markup" |
| 19 | + "code.gitea.io/gitea/modules/setting" |
| 20 | + gitea_context "code.gitea.io/gitea/services/context" |
| 21 | + "code.gitea.io/gitea/services/repository/files" |
| 22 | +) |
| 23 | + |
| 24 | +func renderRepoFileCodePreview(ctx context.Context, opts markup.RenderCodePreviewOptions) (template.HTML, error) { |
| 25 | + opts.LineStop = max(opts.LineStop, 1) |
| 26 | + lineCount := opts.LineStop - opts.LineStart + 1 |
| 27 | + if lineCount <= 0 || lineCount > 140 /* GitHub at most show 140 lines */ { |
| 28 | + lineCount = 10 |
| 29 | + opts.LineStop = opts.LineStart + lineCount |
| 30 | + } |
| 31 | + |
| 32 | + dbRepo, err := repo.GetRepositoryByOwnerAndName(ctx, opts.OwnerName, opts.RepoName) |
| 33 | + if err != nil { |
| 34 | + return "", err |
| 35 | + } |
| 36 | + |
| 37 | + webCtx, ok := ctx.Value(gitea_context.WebContextKey).(*gitea_context.Context) |
| 38 | + if !ok { |
| 39 | + return "", fmt.Errorf("context is not a web context") |
| 40 | + } |
| 41 | + doer := webCtx.Doer |
| 42 | + |
| 43 | + perms, err := access.GetUserRepoPermission(ctx, dbRepo, doer) |
| 44 | + if err != nil { |
| 45 | + return "", err |
| 46 | + } |
| 47 | + if !perms.CanRead(unit.TypeCode) { |
| 48 | + return "", fmt.Errorf("no permission") |
| 49 | + } |
| 50 | + |
| 51 | + gitRepo, err := gitrepo.OpenRepository(ctx, dbRepo) |
| 52 | + if err != nil { |
| 53 | + return "", err |
| 54 | + } |
| 55 | + defer gitRepo.Close() |
| 56 | + |
| 57 | + commit, err := gitRepo.GetCommit(opts.CommitID) |
| 58 | + if err != nil { |
| 59 | + return "", err |
| 60 | + } |
| 61 | + |
| 62 | + language, _ := files.TryGetContentLanguage(gitRepo, opts.CommitID, opts.FilePath) |
| 63 | + blob, err := commit.GetBlobByPath(opts.FilePath) |
| 64 | + if err != nil { |
| 65 | + return "", err |
| 66 | + } |
| 67 | + |
| 68 | + if blob.Size() > setting.UI.MaxDisplayFileSize { |
| 69 | + return "", fmt.Errorf("file is too large") |
| 70 | + } |
| 71 | + |
| 72 | + dataRc, err := blob.DataAsync() |
| 73 | + if err != nil { |
| 74 | + return "", err |
| 75 | + } |
| 76 | + defer dataRc.Close() |
| 77 | + |
| 78 | + reader := bufio.NewReader(dataRc) |
| 79 | + |
| 80 | + for i := 1; i < opts.LineStart; i++ { |
| 81 | + if _, err = reader.ReadBytes('\n'); err != nil { |
| 82 | + return "", err |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + lineNums := make([]int, 0, lineCount) |
| 87 | + lineCodes := make([]string, 0, lineCount) |
| 88 | + for i := opts.LineStart; i <= opts.LineStop; i++ { |
| 89 | + if line, err := reader.ReadString('\n'); err != nil { |
| 90 | + break |
| 91 | + } else { |
| 92 | + lineNums = append(lineNums, i) |
| 93 | + lineCodes = append(lineCodes, line) |
| 94 | + } |
| 95 | + } |
| 96 | + highlightLines := code.HighlightSearchResultCode(opts.FilePath, language, lineNums, strings.Join(lineCodes, "")) |
| 97 | + return webCtx.RenderToHTML("base/markup_codepreview", map[string]any{ |
| 98 | + "FullURL": opts.FullURL, |
| 99 | + "FilePath": opts.FilePath, |
| 100 | + "LineStart": opts.LineStart, |
| 101 | + "LineStop": opts.LineStop, |
| 102 | + "RepoLink": dbRepo.Link(), |
| 103 | + "CommitID": opts.CommitID, |
| 104 | + "HighlightLines": highlightLines, |
| 105 | + }) |
| 106 | +} |
0 commit comments