package disk
import (
"io"
"io/fs"
"os"
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
)
type DiskCatalog struct {
templatesDirectory string
templatesFS fs.FS
}
func NewCatalog(directory string) *DiskCatalog {
catalog := &DiskCatalog{templatesDirectory: directory}
if directory == "" {
catalog.templatesDirectory = config.DefaultConfig.GetTemplateDir()
}
return catalog
}
func NewFSCatalog(fs fs.FS, directory string) *DiskCatalog {
catalog := &DiskCatalog{
templatesDirectory: directory,
templatesFS: fs,
}
return catalog
}
func (d *DiskCatalog) OpenFile(filename string) (io.ReadCloser, error) {
if d.templatesFS == nil {
file, err := os.Open(filename)
if err != nil {
if file, errx := os.Open(BackwardsCompatiblePaths(d.templatesDirectory, filename)); errx == nil {
return file, nil
}
}
return file, err
}
return d.templatesFS.Open(filename)
}