Path: blob/main/vendor/github.com/spf13/afero/httpFs.go
2875 views
// Copyright © 2014 Steve Francia <[email protected]>.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5// http://www.apache.org/licenses/LICENSE-2.06//7// Unless required by applicable law or agreed to in writing, software8// distributed under the License is distributed on an "AS IS" BASIS,9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.10// See the License for the specific language governing permissions and11// limitations under the License.1213package afero1415import (16"errors"17"net/http"18"os"19"path"20"path/filepath"21"strings"22"time"23)2425type httpDir struct {26basePath string27fs HttpFs28}2930func (d httpDir) Open(name string) (http.File, error) {31if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) ||32strings.Contains(name, "\x00") {33return nil, errors.New("http: invalid character in file path")34}35dir := string(d.basePath)36if dir == "" {37dir = "."38}3940f, err := d.fs.Open(filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name))))41if err != nil {42return nil, err43}44return f, nil45}4647type HttpFs struct {48source Fs49}5051func NewHttpFs(source Fs) *HttpFs {52return &HttpFs{source: source}53}5455func (h HttpFs) Dir(s string) *httpDir {56return &httpDir{basePath: s, fs: h}57}5859func (h HttpFs) Name() string { return "h HttpFs" }6061func (h HttpFs) Create(name string) (File, error) {62return h.source.Create(name)63}6465func (h HttpFs) Chmod(name string, mode os.FileMode) error {66return h.source.Chmod(name, mode)67}6869func (h HttpFs) Chown(name string, uid, gid int) error {70return h.source.Chown(name, uid, gid)71}7273func (h HttpFs) Chtimes(name string, atime time.Time, mtime time.Time) error {74return h.source.Chtimes(name, atime, mtime)75}7677func (h HttpFs) Mkdir(name string, perm os.FileMode) error {78return h.source.Mkdir(name, perm)79}8081func (h HttpFs) MkdirAll(path string, perm os.FileMode) error {82return h.source.MkdirAll(path, perm)83}8485func (h HttpFs) Open(name string) (http.File, error) {86f, err := h.source.Open(name)87if err == nil {88if httpfile, ok := f.(http.File); ok {89return httpfile, nil90}91}92return nil, err93}9495func (h HttpFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {96return h.source.OpenFile(name, flag, perm)97}9899func (h HttpFs) Remove(name string) error {100return h.source.Remove(name)101}102103func (h HttpFs) RemoveAll(path string) error {104return h.source.RemoveAll(path)105}106107func (h HttpFs) Rename(oldname, newname string) error {108return h.source.Rename(oldname, newname)109}110111func (h HttpFs) Stat(name string) (os.FileInfo, error) {112return h.source.Stat(name)113}114115116