Skip to content
Myr

fs.go

This file

Path

fs.go

50 lines · 1.4 KiB · Go

Last changed by

ci(golangci-lint): update configuration and fix lint issues (#4247)Flc゛before adoptionc4287b1

History of this file

Take it

1// Copyright 2017 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package gin67import (8	"net/http"9	"os"10)1112// OnlyFilesFS implements an http.FileSystem without `Readdir` functionality.13type OnlyFilesFS struct {14	FileSystem http.FileSystem15}1617// Open passes `Open` to the upstream implementation without `Readdir` functionality.18func (o OnlyFilesFS) Open(name string) (http.File, error) {19	f, err := o.FileSystem.Open(name)20	if err != nil {21		return nil, err22	}2324	return neutralizedReaddirFile{f}, nil25}2627// neutralizedReaddirFile wraps http.File with a specific implementation of `Readdir`.28type neutralizedReaddirFile struct {29	http.File30}3132// Readdir overrides the http.File default implementation and always returns nil.33func (n neutralizedReaddirFile) Readdir(_ int) ([]os.FileInfo, error) {34	// this disables directory listing35	return nil, nil36}3738// Dir returns an http.FileSystem that can be used by http.FileServer().39// It is used internally in router.Static().40// if listDirectory == true, then it works the same as http.Dir(),41// otherwise it returns a filesystem that prevents http.FileServer() to list the directory files.42func Dir(root string, listDirectory bool) http.FileSystem {43	fs := http.Dir(root)4445	if listDirectory {46		return fs47	}4849	return &OnlyFilesFS{FileSystem: fs}50}