From 5c7289bb14d4f594fe6c0d3340a2da50fe1aeaa4 Mon Sep 17 00:00:00 2001 From: Nick Guenther Date: Fri, 24 Feb 2023 11:48:23 -0500 Subject: [PATCH] Place results in subdirs Subdirs are simply named by taking the prefix of the input UUID. This is a common technique to prevent the chance of overflowing filesystem limits on the number of files allowed in a single directory. e.g. Before - /srv/gitea/custom/public/90fa9a89-5e50-4bd4-834d-d42655e1ee8e.html - http://127.0.0.1:3000/assets/90fa9a89-5e50-4bd4-834d-d42655e1ee8e.html After: - /srv/gitea/custom/public/bids-validator/84/b1/84b10e1c-b188-41e2-a92d-fb6ded9c6889.html - http://127.0.0.1:3000/assets/bids-validator/84/b1/84b10e1c-b188-41e2-a92d-fb6ded9c6889.html --- bids-hook.go | 29 +++++++++++------------------ go.mod | 2 +- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/bids-hook.go b/bids-hook.go index 15cc1d2..339d430 100644 --- a/bids-hook.go +++ b/bids-hook.go @@ -278,18 +278,9 @@ type job struct { uuid string } -// web link to the results page for this job -// see also j.resultPath() -func (j job) resultUrl() string { - url := *giteaPublicUrl - url.Path = path.Join(url.Path, fmt.Sprintf("%s.html", j.uuid)) - return url.String() -} - -// file path to the results page for this job -// see also j.resultUrl() +// file path to the results page for this job, relative to the output directory func (j job) resultPath() string { - return filepath.Join(giteaPublicPath, fmt.Sprintf("%s.html", j.uuid)) + return filepath.Join("bids-validator", j.uuid[:2], j.uuid[2:4], fmt.Sprintf("%s.html", j.uuid)) } // file path to the log file for this job @@ -300,23 +291,21 @@ func (j job) logPath() string { // postStatus posts a commit status to Gitea // 'state' should be one of the constants defined at the top of this module func (j job) postStatus(ctx context.Context, state string) error { - url := *giteaApiUrl - url.Path = path.Join(url.Path, "repos", j.user, j.repo, "statuses", j.commit) + url := giteaRootUrl.JoinPath("api", "v1", "repos", j.user, j.repo, "statuses", j.commit) + targetUrl := giteaRootUrl.JoinPath("assets", j.resultPath()).String() + + var description string - var description, targetUrl string switch state { case statePending: description = "waiting for results" targetUrl = "" case stateSuccess: description = "validation passed" - targetUrl = j.resultUrl() case stateFailure: description = "validation failed" - targetUrl = j.resultUrl() case stateWarning: description = "validation passed with warnings" - targetUrl = j.resultUrl() case stateError: description = "internal error" targetUrl = "" @@ -365,7 +354,11 @@ func (j job) run() (state string, _ error) { ) // redirect stdout to the result file - stdout, err := os.OpenFile(j.resultPath(), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) + err := os.MkdirAll(path.Dir(path.Join(giteaPublicPath, j.resultPath())), 0755) + if err != nil { + return stateError, err + } + stdout, err := os.OpenFile(path.Join(giteaPublicPath, j.resultPath()), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) if err != nil { return stateError, err } diff --git a/go.mod b/go.mod index 7b69f53..33f5627 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/neuropoly/bids-hook -go 1.18 +go 1.19