2022-08-10 23:35:58 +05:30
package disk
import (
"io"
2023-05-04 01:43:41 +05:30
"io/fs"
2022-08-10 23:35:58 +05:30
"os"
2023-10-26 19:07:04 +05:30
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
2022-08-10 23:35:58 +05:30
)
// DiskCatalog is a template catalog helper implementation based on disk
type DiskCatalog struct {
templatesDirectory string
2024-06-22 15:42:01 -04:00
templatesFS fs . FS // Due to issues with how Go has implemented fs.FS, we'll have to also implement normal os operations, as well. See: https://github.com/golang/go/issues/44279
2022-08-10 23:35:58 +05:30
}
// NewCatalog creates a new Catalog structure using provided input items
// using disk based items
func NewCatalog ( directory string ) * DiskCatalog {
catalog := & DiskCatalog { templatesDirectory : directory }
2024-06-22 15:42:01 -04:00
if directory == "" {
catalog . templatesDirectory = config . DefaultConfig . GetTemplateDir ( )
}
return catalog
}
// NewFSCatalog creates a new Catalog structure using provided input items
// using the fs.FS as its filesystem.
func NewFSCatalog ( fs fs . FS , directory string ) * DiskCatalog {
catalog := & DiskCatalog {
templatesDirectory : directory ,
templatesFS : fs ,
2023-05-04 01:43:41 +05:30
}
2022-08-10 23:35:58 +05:30
return catalog
}
// OpenFile opens a file and returns an io.ReadCloser to the file.
// It is used to read template and payload files based on catalog responses.
func ( d * DiskCatalog ) OpenFile ( filename string ) ( io . ReadCloser , error ) {
2024-06-22 15:42:01 -04:00
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
}
2023-05-04 01:43:41 +05:30
}
2024-06-22 15:42:01 -04:00
return file , err
2023-05-04 01:43:41 +05:30
}
2024-06-22 15:42:01 -04:00
return d . templatesFS . Open ( filename )
2022-08-10 23:35:58 +05:30
}