Skip to content

Commit 404d3a7

Browse files
committed
fix artifactory mem files
1 parent 62d0a17 commit 404d3a7

File tree

5 files changed

+43
-42
lines changed

5 files changed

+43
-42
lines changed

api/api.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package api
22

33
import (
4-
"context"
54
"encoding/json"
6-
"io/fs"
75
"net/http"
86
"os"
97
"strconv"
@@ -114,13 +112,7 @@ func New(options *Options) *API {
114112
r.Post("/api/extensionquery", api.extensionQuery)
115113

116114
// Endpoint for getting an extension's files or the extension zip.
117-
r.Mount("/files", http.StripPrefix("/files", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
118-
// Convert the storage into a fs.FS, including the request context
119-
http.FileServerFS(&contextFs{
120-
ctx: r.Context(),
121-
open: options.Storage.Open,
122-
}).ServeHTTP(rw, r)
123-
})))
115+
r.Mount("/files", http.StripPrefix("/files", storage.HTTPFileServer(options.Storage)))
124116

125117
// VS Code can use the files in the response to get file paths but it will
126118
// sometimes ignore that and use requests to /assets with hardcoded types to
@@ -264,12 +256,3 @@ func (api *API) assetRedirect(rw http.ResponseWriter, r *http.Request) {
264256

265257
http.Redirect(rw, r, url, http.StatusMovedPermanently)
266258
}
267-
268-
type contextFs struct {
269-
ctx context.Context
270-
open func(ctx context.Context, name string) (fs.File, error)
271-
}
272-
273-
func (c *contextFs) Open(name string) (fs.File, error) {
274-
return c.open(c.ctx, name)
275-
}

storage/artifactory.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
"net/http"
1212
"os"
1313
"path"
14-
"path/filepath"
1514
"sort"
1615
"strings"
1716
"sync"
1817
"time"
1918

19+
"github.com/spf13/afero/mem"
2020
"golang.org/x/sync/errgroup"
2121
"golang.org/x/xerrors"
2222

@@ -300,10 +300,13 @@ func (s *Artifactory) Open(ctx context.Context, fp string) (fs.File, error) {
300300
}
301301
}
302302

303-
return artifactoryFile{
304-
Response: resp,
305-
path: fp,
306-
}, nil
303+
f := mem.NewFileHandle(mem.CreateFile(fp))
304+
_, err = io.Copy(f, resp.Body)
305+
if err != nil {
306+
return nil, err
307+
}
308+
309+
return f, nil
307310
}
308311

309312
func (s *Artifactory) Manifest(ctx context.Context, publisher, name string, version Version) (*VSIXManifest, error) {
@@ -452,21 +455,3 @@ func (s *Artifactory) Versions(ctx context.Context, publisher, name string) ([]V
452455
sort.Sort(ByVersion(versions))
453456
return versions, nil
454457
}
455-
456-
var _ fs.File = (*artifactoryFile)(nil)
457-
var _ fs.FileInfo = (*artifactoryFile)(nil)
458-
459-
type artifactoryFile struct {
460-
*http.Response
461-
path string
462-
}
463-
464-
func (a artifactoryFile) Name() string { return filepath.Base(a.path) }
465-
func (a artifactoryFile) Size() int64 { return a.Response.ContentLength }
466-
func (a artifactoryFile) Mode() fs.FileMode { return fs.FileMode(0) } // ?
467-
func (a artifactoryFile) ModTime() time.Time { return time.Now() }
468-
func (a artifactoryFile) IsDir() bool { return false }
469-
func (a artifactoryFile) Sys() any { return nil }
470-
func (a artifactoryFile) Stat() (fs.FileInfo, error) { return a, nil }
471-
func (a artifactoryFile) Read(i []byte) (int, error) { return a.Response.Body.Read(i) }
472-
func (a artifactoryFile) Close() error { return a.Response.Body.Close() }

storage/storage.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ type Storage interface {
237237
WalkExtensions(ctx context.Context, fn func(manifest *VSIXManifest, versions []Version) error) error
238238
}
239239

240+
// HTTPFileServer creates an http.Handler that serves files from the provided
241+
// storage.
242+
func HTTPFileServer(s Storage) http.Handler {
243+
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
244+
http.FileServerFS(&contextFs{
245+
ctx: r.Context(),
246+
open: s.Open,
247+
}).ServeHTTP(rw, r)
248+
})
249+
}
250+
240251
type File struct {
241252
RelativePath string
242253
Content []byte
@@ -429,3 +440,12 @@ func ParseExtensionID(id string) (string, string, string, error) {
429440
}
430441
return match[0][1], match[0][2], match[0][3], nil
431442
}
443+
444+
type contextFs struct {
445+
ctx context.Context
446+
open func(ctx context.Context, name string) (fs.File, error)
447+
}
448+
449+
func (c *contextFs) Open(name string) (fs.File, error) {
450+
return c.open(c.ctx, name)
451+
}

storage/storage_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func testFileServer(t *testing.T, factory storageFactory) {
189189
req := httptest.NewRequest("GET", test.path, nil)
190190
rec := httptest.NewRecorder()
191191

192-
server := f.storage.FileServer()
192+
server := storage.HTTPFileServer(f.storage)
193193
server.ServeHTTP(rec, req)
194194

195195
resp := rec.Result()

testutil/mockstorage.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ package testutil
33
import (
44
"context"
55
"errors"
6+
"io/fs"
67
"net/http"
78
"os"
9+
"path/filepath"
810
"sort"
911

12+
"github.com/spf13/afero/mem"
13+
1014
"github.com/coder/code-marketplace/storage"
1115
)
1216

@@ -22,6 +26,15 @@ func NewMockStorage() *MockStorage {
2226
func (s *MockStorage) AddExtension(ctx context.Context, manifest *storage.VSIXManifest, vsix []byte, extra ...storage.File) (string, error) {
2327
return "", errors.New("not implemented")
2428
}
29+
func (s *MockStorage) Open(ctx context.Context, path string) (fs.File, error) {
30+
if filepath.Base(path) == "/nonexistent" {
31+
return nil, fs.ErrNotExist
32+
}
33+
34+
f := mem.NewFileHandle(mem.CreateFile(path))
35+
_, _ = f.Write([]byte("foobar"))
36+
return f, nil
37+
}
2538

2639
func (s *MockStorage) FileServer() http.Handler {
2740
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)