github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libfs/httprootfs.go (about)

     1  // Copyright 2019 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package libfs
     6  
     7  import (
     8  	"net/http"
     9  	"os"
    10  	"strings"
    11  
    12  	"github.com/pkg/errors"
    13  	billy "gopkg.in/src-d/go-billy.v4"
    14  )
    15  
    16  type httpRootFileSystem struct {
    17  	rfs *RootFS
    18  }
    19  
    20  var _ http.FileSystem = httpRootFileSystem{}
    21  
    22  type fileOnly struct {
    23  	billy.File
    24  	rfs *RootFS
    25  }
    26  
    27  // Readdir implements the http.File interface.
    28  func (fo fileOnly) Readdir(count int) ([]os.FileInfo, error) {
    29  	return nil, errors.New("not supported")
    30  }
    31  
    32  // Stat implements the http.File interface.
    33  func (fo fileOnly) Stat() (os.FileInfo, error) {
    34  	return fo.rfs.Stat(fo.File.Name())
    35  }
    36  
    37  func (hrfs httpRootFileSystem) Open(filename string) (entry http.File, err error) {
    38  	defer func() {
    39  		if err != nil {
    40  			err = translateErr(err)
    41  		}
    42  	}()
    43  
    44  	filename = strings.TrimPrefix(filename, "/")
    45  
    46  	f, err := hrfs.rfs.Open(filename)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	return fileOnly{File: f, rfs: hrfs.rfs}, nil
    52  }