From b7ae17c92aca5eb78c5efca6e6494242067475b5 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 31 Mar 2026 21:19:33 +0200 Subject: [PATCH] simplify --- routers/web/devtest/mock_actions.go | 58 +++++-------------- routers/web/repo/actions/view.go | 6 +- .../devtest/repo-action-artifact-preview.tmpl | 20 ------- templates/repo/actions/artifact_preview.tmpl | 21 ------- .../actions/artifact_preview_content.tmpl | 21 +++++++ web_src/js/components/RepoActionView.vue | 6 +- 6 files changed, 37 insertions(+), 95 deletions(-) diff --git a/routers/web/devtest/mock_actions.go b/routers/web/devtest/mock_actions.go index 48acb90094..f5daf70f3f 100644 --- a/routers/web/devtest/mock_actions.go +++ b/routers/web/devtest/mock_actions.go @@ -29,21 +29,6 @@ type mockArtifactFile struct { Content string } -type mockArtifactPreviewTemplateData struct { - ArtifactName string - PreviewFiles []mockArtifactPreviewTemplateFile - RunURL string - PreviewURL string - PreviewRawURL string - DownloadURL string - SelectedPath string -} - -type mockArtifactPreviewTemplateFile struct { - Path string - Selected bool -} - var mockActionsArtifactFiles = map[string][]mockArtifactFile{ "artifact-b": { { @@ -77,11 +62,6 @@ func normalizeMockArtifactPath(path string) string { return path } -func getMockArtifactFiles(name string) ([]mockArtifactFile, bool) { - files, ok := mockActionsArtifactFiles[name] - return files, ok -} - func chooseMockArtifactPath(files []mockArtifactFile, requestedPath string) string { if len(files) == 0 { return "" @@ -305,7 +285,7 @@ func fillViewRunResponseCurrentJob(ctx *context.Context, resp *actions.ViewRespo func MockActionsArtifactDownload(ctx *context.Context) { artifactName := ctx.PathParam("artifact_name") - files, ok := getMockArtifactFiles(artifactName) + files, ok := mockActionsArtifactFiles[artifactName] if !ok { ctx.NotFound(nil) return @@ -330,7 +310,7 @@ func MockActionsArtifactDownload(ctx *context.Context) { func MockActionsArtifactPreview(ctx *context.Context) { runID := ctx.PathParamInt64("run") artifactName := ctx.PathParam("artifact_name") - files, ok := getMockArtifactFiles(artifactName) + files, ok := mockActionsArtifactFiles[artifactName] if !ok { ctx.NotFound(nil) return @@ -341,40 +321,30 @@ func MockActionsArtifactPreview(ctx *context.Context) { selectedPath = normalizeMockArtifactPath(ctx.Req.URL.Query().Get("path")) } selectedPath = chooseMockArtifactPath(files, selectedPath) - templateFiles := make([]mockArtifactPreviewTemplateFile, 0, len(files)) + previewFiles := make([]actions.ArtifactPreviewFile, 0, len(files)) for _, file := range files { - templateFiles = append(templateFiles, mockArtifactPreviewTemplateFile{ + previewFiles = append(previewFiles, actions.ArtifactPreviewFile{ Path: file.Path, Selected: file.Path == selectedPath, }) } - previewURL := fmt.Sprintf("%s/devtest/repo-action-view/runs/%d/artifacts/%s/preview", setting.AppSubURL, runID, url.PathEscape(artifactName)) - previewRawURL := previewURL + "/raw" - downloadURL := fmt.Sprintf("%s/devtest/repo-action-view/runs/%d/artifacts/%s", setting.AppSubURL, runID, url.PathEscape(artifactName)) - data := mockArtifactPreviewTemplateData{ - ArtifactName: artifactName, - PreviewFiles: templateFiles, - RunURL: previewURL, - PreviewURL: previewURL, - PreviewRawURL: previewRawURL, - DownloadURL: downloadURL, - SelectedPath: selectedPath, - } + runURL := fmt.Sprintf("%s/devtest/repo-action-view/runs/%d", setting.AppSubURL, runID) + previewURL := runURL + "/artifacts/" + url.PathEscape(artifactName) + "/preview" - ctx.Data["ArtifactName"] = data.ArtifactName - ctx.Data["PreviewFiles"] = data.PreviewFiles - ctx.Data["RunURL"] = data.RunURL - ctx.Data["PreviewURL"] = data.PreviewURL - ctx.Data["PreviewRawURL"] = data.PreviewRawURL - ctx.Data["DownloadURL"] = data.DownloadURL - ctx.Data["SelectedPath"] = data.SelectedPath + ctx.Data["ArtifactName"] = artifactName + ctx.Data["PreviewFiles"] = previewFiles + ctx.Data["RunURL"] = runURL + ctx.Data["PreviewURL"] = previewURL + ctx.Data["PreviewRawURL"] = previewURL + "/raw" + ctx.Data["DownloadURL"] = runURL + "/artifacts/" + url.PathEscape(artifactName) + ctx.Data["SelectedPath"] = selectedPath ctx.HTML(http.StatusOK, "devtest/repo-action-artifact-preview") } func MockActionsArtifactPreviewRaw(ctx *context.Context) { artifactName := ctx.PathParam("artifact_name") - files, ok := getMockArtifactFiles(artifactName) + files, ok := mockActionsArtifactFiles[artifactName] if !ok { ctx.NotFound(nil) return diff --git a/routers/web/repo/actions/view.go b/routers/web/repo/actions/view.go index d2ce14bcd0..547429a05a 100644 --- a/routers/web/repo/actions/view.go +++ b/routers/web/repo/actions/view.go @@ -861,7 +861,7 @@ func setArtifactPreviewCSP(ctx *context_module.Context, st typesniffer.SniffedTy func previewArtifactByReader(ctx *context_module.Context, path string, _ int64, reader io.Reader) { buf := filebuffer.New(int(setting.UI.MaxDisplayFileSize), "") defer buf.Close() - if _, err := io.Copy(buf, reader); err != nil { + if _, err := io.Copy(buf, io.LimitReader(reader, setting.UI.MaxDisplayFileSize)); err != nil { ctx.ServerError("io.Copy", err) return } @@ -879,16 +879,12 @@ func previewArtifactByReadSeeker(ctx *context_module.Context, path string, reade ctx.ServerError("ReadAtMost", err) return } - if n < 0 { - n = 0 - } buf = buf[:n] if _, err := reader.Seek(0, io.SeekStart); err != nil { ctx.ServerError("Seek", err) return } - buf = buf[:n] st := typesniffer.DetectContentType(buf) if !isPreviewableArtifactType(st) { diff --git a/templates/devtest/repo-action-artifact-preview.tmpl b/templates/devtest/repo-action-artifact-preview.tmpl index 2855f431a3..01f2732994 100644 --- a/templates/devtest/repo-action-artifact-preview.tmpl +++ b/templates/devtest/repo-action-artifact-preview.tmpl @@ -5,24 +5,4 @@ - {{template "base/footer" .}} diff --git a/templates/repo/actions/artifact_preview.tmpl b/templates/repo/actions/artifact_preview.tmpl index d557f21518..6886ed9bc6 100644 --- a/templates/repo/actions/artifact_preview.tmpl +++ b/templates/repo/actions/artifact_preview.tmpl @@ -9,25 +9,4 @@ - - {{template "base/footer" .}} diff --git a/templates/shared/actions/artifact_preview_content.tmpl b/templates/shared/actions/artifact_preview_content.tmpl index 0c787777a6..dd16648574 100644 --- a/templates/shared/actions/artifact_preview_content.tmpl +++ b/templates/shared/actions/artifact_preview_content.tmpl @@ -1,3 +1,24 @@ + +
{{ctx.Locale.Tr "preview"}}: {{.ArtifactName}} diff --git a/web_src/js/components/RepoActionView.vue b/web_src/js/components/RepoActionView.vue index 46460ba501..ea12f75db3 100644 --- a/web_src/js/components/RepoActionView.vue +++ b/web_src/js/components/RepoActionView.vue @@ -38,10 +38,6 @@ function artifactPreviewURL(name: string): string { return `${artifactBaseURL(name)}/preview`; } -function artifactDownloadURL(name: string): string { - return artifactBaseURL(name); -} - async function deleteArtifact(name: string) { if (!window.confirm(locale.confirmDeleteArtifact.replace('%s', name))) return; await DELETE(artifactBaseURL(name)); @@ -138,7 +134,7 @@ async function deleteArtifact(name: string) { {{ artifact.name }} - +