Skip to content

Commit

Permalink
Merge pull request #54 from ctrliq/mirror-web-fix
Browse files Browse the repository at this point in the history
Mirror Plugin: Ensure empty directories have web pages generated
  • Loading branch information
kyleishie authored Apr 24, 2024
2 parents 0e442fa + 8610100 commit b354186
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
12 changes: 8 additions & 4 deletions internal/plugins/mirror/pkg/mirrorrepository/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/RussellLuo/kun/pkg/werror"
"github.com/RussellLuo/kun/pkg/werror/gcode"
"go.ciq.dev/beskar/internal/plugins/mirror/pkg/mirrordb"
"go.ciq.dev/go-rsync/rsync"
)

func (h *Handler) generateFileReference(file string) string {
Expand Down Expand Up @@ -132,20 +133,23 @@ func (h *Handler) listRepositoryFilesByConfigID(ctx context.Context, configID ui
return repositoryFiles, nil
}

func (h *Handler) listRepositoryDistinctParents(ctx context.Context) (repositoryParents []string, err error) {
func (h *Handler) listRepositoryDirectories(ctx context.Context) (repositoryFiles []*mirrordb.RepositoryFile, err error) {
db, err := h.getRepositoryDB(ctx)
if err != nil {
return nil, werror.Wrap(gcode.ErrInternal, err)
}
defer db.Close(false)

err = db.WalkFilesByDistinctParent(ctx, func(parent *string) error {
repositoryParents = append(repositoryParents, *parent)
err = db.WalkFiles(ctx, func(file *mirrordb.RepositoryFile) error {
if rsync.FileMode(file.Mode).IsDIR() {
repositoryFiles = append(repositoryFiles, file)
}

return nil
})
if err != nil {
return nil, werror.Wrap(gcode.ErrInternal, err)
}

return repositoryParents, nil
return repositoryFiles, nil
}
46 changes: 21 additions & 25 deletions internal/plugins/mirror/pkg/mirrorrepository/index_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,31 @@ func (h *Handler) GenerateIndexes() error {
webPrefix = h.getWebConfig().Prefix
}

parents, err := h.listRepositoryDistinctParents(context.Background())
directories, err := h.listRepositoryDirectories(context.Background())
if err != nil {
h.logger.Error("Failed to list distinct parents", "error", err.Error())
return err
}

sort.Strings(parents)

for _, p := range parents {
// Get parentInfo file info
parentInfo, err := h.getRepositoryFile(context.Background(), p)
if err != nil {
h.logger.Error("Failed to get parent info", "error", err.Error(), "parent", p)
return err
}
// Sort parents to ensure consistent index generation
sort.Slice(directories, func(i, j int) bool {
return directories[i].Name < directories[j].Name
})

for _, directory := range directories {
// Generate index config for parent
c := index.Config{
Current: path.Join(webPrefix, filepath.Clean(parentInfo.Name)),
Current: path.Join(webPrefix, filepath.Clean(directory.Name)),
}

// Don't set previous for root directory
if filepath.Clean(parentInfo.Name) != filepath.Dir(parentInfo.Name) {
c.Previous = path.Join(webPrefix, filepath.Dir(parentInfo.Name))
if filepath.Clean(directory.Name) != filepath.Dir(directory.Name) {
c.Previous = path.Join(webPrefix, filepath.Dir(directory.Name))
}

files, err := h.listRepositoryFilesByParent(context.Background(), p)
files, err := h.listRepositoryFilesByParent(context.Background(), directory.Name)
if err != nil {
h.logger.Error("Failed to list files by parent", "error", err.Error(), "parent", p)
h.logger.Error("Failed to list files by parent", "error", err.Error(), "directory", directory.Name)
return err
}

Expand Down Expand Up @@ -131,7 +127,7 @@ func (h *Handler) GenerateIndexes() error {
return err
}

pusher, err := orasmirror.NewStaticFileStreamPusher(bytes.NewReader(rawIndex), "index.html", parentInfo.Reference, h.Params.NameOptions...)
pusher, err := orasmirror.NewStaticFileStreamPusher(bytes.NewReader(rawIndex), "index.html", directory.Reference, h.Params.NameOptions...)
if err != nil {
return err
}
Expand All @@ -142,24 +138,24 @@ func (h *Handler) GenerateIndexes() error {
}

//nolint:gosec
s := md5.Sum([]byte(parentInfo.Reference))
s := md5.Sum([]byte(directory.Reference))
tag := hex.EncodeToString(s[:])

err = h.addFileToRepositoryDatabase(context.Background(), &mirrordb.RepositoryFile{
Tag: tag,
Name: parentInfo.Name,
Reference: parentInfo.Reference,
Parent: parentInfo.Parent,
ModifiedTime: parentInfo.ModifiedTime,
Mode: parentInfo.Mode,
Size: parentInfo.Size,
ConfigID: parentInfo.ConfigID,
Name: directory.Name,
Reference: directory.Reference,
Parent: directory.Parent,
ModifiedTime: directory.ModifiedTime,
Mode: directory.Mode,
Size: directory.Size,
ConfigID: directory.ConfigID,
})
if err != nil {
return err
}

h.logger.Debug("Generated Index", "Name", parentInfo.Name, "Reference", parentInfo.Reference)
h.logger.Debug("Generated Index", "Name", directory.Name, "Reference", directory.Reference)
}

return nil
Expand Down

0 comments on commit b354186

Please sign in to comment.