|
6 | 6 | "errors" |
7 | 7 | "fmt" |
8 | 8 | "io" |
| 9 | + "io/fs" |
9 | 10 | "net/http" |
10 | 11 | "path" |
11 | 12 | "strings" |
@@ -98,20 +99,14 @@ func detectSingleRootDir(reader archives.Reader) string { |
98 | 99 | return "" |
99 | 100 | } |
100 | 101 |
|
101 | | - var root rootDetector |
102 | | - for _, f := range files { |
103 | | - if !root.add(f.Path) { |
104 | | - break |
105 | | - } |
106 | | - } |
107 | | - return root.prefix() |
| 102 | + return archives.CommonRoot(files) |
108 | 103 | } |
109 | 104 |
|
110 | 105 | // openArchive opens a cached artifact as a random-access archive reader, |
111 | 106 | // auto-detecting and stripping a single top-level directory prefix (like |
112 | 107 | // GitHub zipballs). For npm, the hardcoded "package/" prefix takes precedence. |
113 | 108 | // The whole artifact is buffered, so only the version diff uses it; listing |
114 | | -// and file reads stream through browseArchive instead. |
| 109 | +// and file reads use the streaming browser instead. |
115 | 110 | func openArchive(filename string, content io.Reader, ecosystem string) (archives.Reader, error) { //nolint:ireturn // wraps multiple archive implementations |
116 | 111 | data, err := readBrowseInput(content, maxBrowseArchiveSize) |
117 | 112 | if err != nil { |
@@ -142,7 +137,7 @@ func (s *Server) browseLimitsOrDefault() archives.StreamOptions { |
142 | 137 |
|
143 | 138 | // openBrowseArchive prepares a cached artifact for streaming. content is the |
144 | 139 | // already open storage reader; later passes reopen the artifact. |
145 | | -func (s *Server) openBrowseArchive(r *http.Request, artifact *database.Artifact, content io.Reader, ecosystem string) (*browseArchive, error) { |
| 140 | +func (s *Server) openBrowseArchive(r *http.Request, artifact *database.Artifact, content io.Reader, ecosystem string) (*archives.Browser, error) { |
146 | 141 | storagePath := artifact.StoragePath.String |
147 | 142 | reopen := func() (io.ReadCloser, error) { |
148 | 143 | return s.storage.Open(r.Context(), storagePath) |
@@ -398,13 +393,9 @@ func (s *Server) browseFile(w http.ResponseWriter, r *http.Request, ecosystem, n |
398 | 393 | // Find the file and stream it straight from the archive |
399 | 394 | fileReader, err := archive.Extract(filePath) |
400 | 395 | switch { |
401 | | - case errors.Is(err, errBrowseNotFound): |
| 396 | + case errors.Is(err, fs.ErrNotExist), errors.Is(err, archives.ErrIsDir): |
402 | 397 | notFound(w, "file not found") |
403 | 398 | return |
404 | | - case errors.Is(err, errBrowseIsDir): |
405 | | - s.logger.Error("failed to extract file", "error", err, "path", filePath) |
406 | | - internalError(w, "failed to extract file") |
407 | | - return |
408 | 399 | case err != nil: |
409 | 400 | s.browseArchiveError(w, err, cachedArtifact.Filename) |
410 | 401 | return |
|
0 commit comments