77 "errors"
88 "fmt"
99 "io"
10- "io/fs"
1110 "net/http"
1211 "os"
1312 "path"
@@ -16,7 +15,6 @@ import (
1615 "sync"
1716 "time"
1817
19- "github.com/spf13/afero/mem"
2018 "golang.org/x/sync/errgroup"
2119 "golang.org/x/xerrors"
2220
@@ -277,37 +275,25 @@ func (s *Artifactory) AddExtension(ctx context.Context, manifest *VSIXManifest,
277275 return s .uri + dir , nil
278276}
279277
280- // Open returns a file from Artifactory.
281- // TODO: Since we only extract a subset of files perhaps if the file does not
282- // exist we should download the vsix and extract the requested file as a
283- // fallback. Obviously this seems like quite a bit of overhead so we would
284- // then emit a warning so we can notice that VS Code has added new asset types
285- // that we should be extracting to avoid that overhead. Other solutions could
286- // be implemented though like extracting the VSIX to disk locally and only
287- // going to Artifactory for the VSIX when it is missing on disk (basically
288- // using the disk as a cache).
289- func (s * Artifactory ) Open (ctx context.Context , fp string ) (fs.File , error ) {
290- resp , code , err := s .read (ctx , fp )
291- if code != http .StatusOK || err != nil {
292- switch code {
293- case http .StatusNotFound :
294- return nil , fs .ErrNotExist
295- case http .StatusForbidden :
296- return nil , fs .ErrPermission
297- default :
298- return nil , err
278+ func (s * Artifactory ) FileServer () http.Handler {
279+ // TODO: Since we only extract a subset of files perhaps if the file does not
280+ // exist we should download the vsix and extract the requested file as a
281+ // fallback. Obviously this seems like quite a bit of overhead so we would
282+ // then emit a warning so we can notice that VS Code has added new asset types
283+ // that we should be extracting to avoid that overhead. Other solutions could
284+ // be implemented though like extracting the VSIX to disk locally and only
285+ // going to Artifactory for the VSIX when it is missing on disk (basically
286+ // using the disk as a cache).
287+ return http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
288+ reader , code , err := s .read (r .Context (), r .URL .Path )
289+ if err != nil {
290+ http .Error (rw , err .Error (), code )
291+ return
299292 }
300- }
301-
302- // TODO: Do no copy the bytes into memory, stream them rather than
303- // storing the entire file into memory.
304- f := mem .NewFileHandle (mem .CreateFile (fp ))
305- _ , err = io .Copy (f , resp )
306- if err != nil {
307- return nil , err
308- }
309-
310- return f , nil
293+ defer reader .Close ()
294+ rw .WriteHeader (http .StatusOK )
295+ _ , _ = io .Copy (rw , reader )
296+ })
311297}
312298
313299func (s * Artifactory ) Manifest (ctx context.Context , publisher , name string , version Version ) (* VSIXManifest , error ) {
0 commit comments