diff --git a/gitea/apiclient.go b/gitea/apiclient.go index 9f113f8..ce06231 100644 --- a/gitea/apiclient.go +++ b/gitea/apiclient.go @@ -68,7 +68,6 @@ func (ac *APIClient) PopulateCommitInfo(ctx context.Context, rr *Repo) error { func (ac *APIClient) PopulateTopics(ctx context.Context, rr *Repo) error { t, err := ac.topicsForRepo(ctx, rr.Name) - if err != nil { return err } @@ -77,6 +76,16 @@ func (ac *APIClient) PopulateTopics(ctx context.Context, rr *Repo) error { return nil } +func (ac *APIClient) PopulateImages(ctx context.Context, rr *Repo) error { + img, err := ac.ImageFilesForRepo(ctx, rr.Name) + if err != nil { + return err + } + + rr.Images = img + return nil +} + func (ac *APIClient) apiRequest(ctx context.Context, endpoint string, target interface{}) error { err := ac.apiSem.Acquire(ctx, 1) if err != nil { @@ -248,7 +257,7 @@ func (ac *APIClient) filesInDirectory(ctx context.Context, repo, dir string) ([] return ret, nil } -// imageFilesForRepo finds documentation images for the repository. +// ImageFilesForRepo finds documentation images for the repository. // It searches the top-level directory and the dist/ and doc/ subdirectories. func (ac *APIClient) ImageFilesForRepo(ctx context.Context, repo string) ([]ReaddirEntry, error) { diff --git a/gitea/types.go b/gitea/types.go index 3211769..7e4b93d 100644 --- a/gitea/types.go +++ b/gitea/types.go @@ -13,8 +13,10 @@ type Repo struct { // NewestCommit is populated via PopulateCommitInfo(). NewestCommit time.Time - // Topics is populated via topicsForRepo(). + // Topics is populated via PopulateTopics(). Topics []string + // Images are populated via PopulateImages(). + Images []ReaddirEntry } type ContentsResponse struct { diff --git a/page_imageredir.go b/page_imageredir.go index 4f06eaf..6866519 100644 --- a/page_imageredir.go +++ b/page_imageredir.go @@ -1,19 +1,22 @@ package main import ( - "fmt" "net/http" ) func (this *Application) Bannerpage(w http.ResponseWriter, r *http.Request, repoName string) { - ctx := r.Context() - images, err := this.gitea.ImageFilesForRepo(ctx, repoName) - if err != nil { - this.internalError(w, r, fmt.Errorf("listing images: %w", err)) + this.reposMut.RLock() + defer this.reposMut.RUnlock() + + ridx, ok := this.reposAlphabeticalOrder[repoName] + if !ok { + w.Header().Set(`Location`, `/static/no_image.png`) + w.WriteHeader(301) return } + images := this.reposCache[ridx].Images if len(images) == 0 { w.Header().Set(`Location`, `/static/no_image.png`) w.WriteHeader(301)