package rushlink

import (
	"log"
	"net/http"

	"github.com/pkg/errors"
)

// Where to store the uploaded files
var fileStoreDir = ""

// FileUploadFileSystem is a HTTP filesystem handler
type FileUploadFileSystem struct {
	fs http.FileSystem
}

// Open opens a file
func (fs FileUploadFileSystem) Open(path string) (http.File, error) {
	log.Println(path)
	file, err := fs.fs.Open(path)
	if err != nil {
		return nil, errors.Wrap(err, "opening file")
	}
	stat, err := file.Stat()
	if err != nil {
		return nil, errors.Wrap(err, "file.Stat()")
	}
	if stat.IsDir() {
		return nil, errors.New("directory index not allowed")
	}
	return file, nil
}