admin: Export more importer functions

This commit is contained in:
nemunaire 2023-11-25 17:13:31 +01:00
parent 092256d9e5
commit 1f833d39fc
7 changed files with 26 additions and 26 deletions

View File

@ -76,7 +76,7 @@ func BuildFilesListInto(i Importer, exercice *fic.Exercice, into string) (files
} }
// Read file list // Read file list
if flist, err := i.listDir(path.Join(exercice.Path, into)); err != nil { if flist, err := i.ListDir(path.Join(exercice.Path, into)); err != nil {
errs = multierr.Append(errs, NewExerciceError(exercice, err)) errs = multierr.Append(errs, NewExerciceError(exercice, err))
} else { } else {
for _, fname := range flist { for _, fname := range flist {

View File

@ -31,11 +31,11 @@ func GetExercices(i Importer, theme *fic.Theme) ([]string, error) {
if len(theme.Path) == 0 { if len(theme.Path) == 0 {
return []string{}, nil return []string{}, nil
} else if dirs, err := i.listDir(theme.Path); err != nil { } else if dirs, err := i.ListDir(theme.Path); err != nil {
return []string{}, err return []string{}, err
} else { } else {
for _, dir := range dirs { for _, dir := range dirs {
if _, err := i.listDir(path.Join(theme.Path, dir)); err == nil { if _, err := i.ListDir(path.Join(theme.Path, dir)); err == nil {
if dir[0] != '.' && strings.Contains(dir, "-") { if dir[0] != '.' && strings.Contains(dir, "-") {
exercices = append(exercices, dir) exercices = append(exercices, dir)
} }

View File

@ -35,11 +35,11 @@ type Importer interface {
// Callback return is forwarded. // Callback return is forwarded.
importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error) importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error)
// getFileReader returns a reader to the requested file. // getFileReader returns a reader to the requested file.
getFile(filename string) (io.Reader, error) GetFile(filename string) (io.Reader, error)
// listDir returns a list of the files and subdirectories contained inside the directory at the given location. // listDir returns a list of the files and subdirectories contained inside the directory at the given location.
listDir(filename string) ([]string, error) ListDir(filename string) ([]string, error)
// stat returns many information about the given file: such as last modification date, size, ... // stat returns many information about the given file: such as last modification date, size, ...
stat(filename string) (os.FileInfo, error) Stat(filename string) (os.FileInfo, error)
} }
// DirectAccessImporter abstracts importer that support direct file access through a local path // DirectAccessImporter abstracts importer that support direct file access through a local path
@ -65,7 +65,7 @@ var GlobalImporter Importer
// GetFileSize returns the size. // GetFileSize returns the size.
func GetFileSize(i Importer, URI string) (size int64, err error) { func GetFileSize(i Importer, URI string) (size int64, err error) {
if i.Exists(URI) { if i.Exists(URI) {
if fi, err := i.stat(URI); err != nil { if fi, err := i.Stat(URI); err != nil {
return 0, err return 0, err
} else { } else {
return fi.Size(), nil return fi.Size(), nil
@ -75,7 +75,7 @@ func GetFileSize(i Importer, URI string) (size int64, err error) {
dirname := path.Dir(URI) dirname := path.Dir(URI)
if i.Exists(dirname) { if i.Exists(dirname) {
filename := path.Base(URI) filename := path.Base(URI)
if files, err := i.listDir(dirname); err != nil { if files, err := i.ListDir(dirname); err != nil {
return size, err return size, err
} else { } else {
for _, fname := range []string{filename, filename + "."} { for _, fname := range []string{filename, filename + "."} {
@ -83,7 +83,7 @@ func GetFileSize(i Importer, URI string) (size int64, err error) {
for _, file := range files { for _, file := range files {
if matched, _ := path.Match(fname+"[0-9][0-9]", file); matched { if matched, _ := path.Match(fname+"[0-9][0-9]", file); matched {
found = true found = true
if fi, err := i.stat(path.Join(dirname, file)); err != nil { if fi, err := i.Stat(path.Join(dirname, file)); err != nil {
return size, err return size, err
} else { } else {
size += fi.Size() size += fi.Size()
@ -105,7 +105,7 @@ func GetFileSize(i Importer, URI string) (size int64, err error) {
func GetFile(i Importer, URI string) (io.Reader, func(), error) { func GetFile(i Importer, URI string) (io.Reader, func(), error) {
// Import file if it exists // Import file if it exists
if i.Exists(URI) { if i.Exists(URI) {
fd, err := i.getFile(URI) fd, err := i.GetFile(URI)
return fd, func() { return fd, func() {
if fdc, ok := fd.(io.ReadCloser); ok { if fdc, ok := fd.(io.ReadCloser); ok {
fdc.Close() fdc.Close()
@ -117,7 +117,7 @@ func GetFile(i Importer, URI string) (io.Reader, func(), error) {
dirname := path.Dir(URI) dirname := path.Dir(URI)
if i.Exists(dirname) { if i.Exists(dirname) {
filename := path.Base(URI) filename := path.Base(URI)
if files, err := i.listDir(dirname); err != nil { if files, err := i.ListDir(dirname); err != nil {
return nil, nil, err return nil, nil, err
} else { } else {
var readers []io.Reader var readers []io.Reader
@ -125,7 +125,7 @@ func GetFile(i Importer, URI string) (io.Reader, func(), error) {
for _, fname := range []string{filename, filename + "."} { for _, fname := range []string{filename, filename + "."} {
for _, file := range files { for _, file := range files {
if matched, _ := path.Match(fname+"[0-9][0-9]", file); matched { if matched, _ := path.Match(fname+"[0-9][0-9]", file); matched {
fd, err := i.getFile(path.Join(dirname, file)) fd, err := i.GetFile(path.Join(dirname, file))
if err != nil { if err != nil {
// Close already opened files to avoid leaks // Close already opened files to avoid leaks
for _, rd := range readers { for _, rd := range readers {

View File

@ -77,7 +77,7 @@ func (i CloudImporter) importFile(URI string, next func(string, string) (interfa
return ImportFile(i, URI, next) return ImportFile(i, URI, next)
} }
func (i CloudImporter) getFile(filename string) (io.Reader, error) { func (i CloudImporter) GetFile(filename string) (io.Reader, error) {
fullURL := i.baseDAV fullURL := i.baseDAV
fullURL.Path = path.Join(fullURL.Path, filename) fullURL.Path = path.Join(fullURL.Path, filename)
@ -122,7 +122,7 @@ func (i CloudImporter) writeFile(filename string, reader io.Reader) error {
} }
} }
func (i CloudImporter) listDir(filename string) ([]string, error) { func (i CloudImporter) ListDir(filename string) ([]string, error) {
client := gowebdav.NewClient(i.baseDAV.String(), i.username, i.password) client := gowebdav.NewClient(i.baseDAV.String(), i.username, i.password)
if files, err := client.ReadDir(strings.Replace(url.PathEscape(filename), "%2F", "/", -1)); err != nil { if files, err := client.ReadDir(strings.Replace(url.PathEscape(filename), "%2F", "/", -1)); err != nil {
@ -136,6 +136,6 @@ func (i CloudImporter) listDir(filename string) ([]string, error) {
} }
} }
func (i CloudImporter) stat(filename string) (os.FileInfo, error) { func (i CloudImporter) Stat(filename string) (os.FileInfo, error) {
return gowebdav.NewClient(i.baseDAV.String(), i.username, i.password).Stat(strings.Replace(url.PathEscape(filename), "%2F", "/", -1)) return gowebdav.NewClient(i.baseDAV.String(), i.username, i.password).Stat(strings.Replace(url.PathEscape(filename), "%2F", "/", -1))
} }

View File

@ -37,20 +37,20 @@ func (i GitImporter) importFile(URI string, next func(string, string) (interface
return i.li.importFile(URI, next) return i.li.importFile(URI, next)
} }
func (i GitImporter) getFile(filename string) (io.Reader, error) { func (i GitImporter) GetFile(filename string) (io.Reader, error) {
return i.li.getFile(filename) return i.li.GetFile(filename)
} }
func (i GitImporter) writeFile(filename string, reader io.Reader) error { func (i GitImporter) writeFile(filename string, reader io.Reader) error {
return i.li.writeFile(filename, reader) return i.li.writeFile(filename, reader)
} }
func (i GitImporter) listDir(filename string) ([]string, error) { func (i GitImporter) ListDir(filename string) ([]string, error) {
return i.li.listDir(filename) return i.li.ListDir(filename)
} }
func (i GitImporter) stat(filename string) (os.FileInfo, error) { func (i GitImporter) Stat(filename string) (os.FileInfo, error) {
return i.li.stat(filename) return i.li.Stat(filename)
} }
func (i GitImporter) Kind() string { func (i GitImporter) Kind() string {

View File

@ -80,7 +80,7 @@ func (i LocalImporter) importFile(URI string, next func(string, string) (interfa
} }
} }
func (i LocalImporter) getFile(filename string) (io.Reader, error) { func (i LocalImporter) GetFile(filename string) (io.Reader, error) {
if fd, err := os.Open(path.Join(i.Base, filename)); err != nil { if fd, err := os.Open(path.Join(i.Base, filename)); err != nil {
return nil, err return nil, err
} else { } else {
@ -98,7 +98,7 @@ func (i LocalImporter) writeFile(filename string, reader io.Reader) error {
} }
} }
func (i LocalImporter) listDir(filename string) ([]string, error) { func (i LocalImporter) ListDir(filename string) ([]string, error) {
if files, err := ioutil.ReadDir(path.Join(i.Base, filename)); err != nil { if files, err := ioutil.ReadDir(path.Join(i.Base, filename)); err != nil {
return nil, err return nil, err
} else { } else {
@ -110,6 +110,6 @@ func (i LocalImporter) listDir(filename string) ([]string, error) {
} }
} }
func (i LocalImporter) stat(filename string) (os.FileInfo, error) { func (i LocalImporter) Stat(filename string) (os.FileInfo, error) {
return os.Stat(path.Join(i.Base, filename)) return os.Stat(path.Join(i.Base, filename))
} }

View File

@ -23,12 +23,12 @@ import (
// GetThemes returns all theme directories in the base directory. // GetThemes returns all theme directories in the base directory.
func GetThemes(i Importer) (themes []string, err error) { func GetThemes(i Importer) (themes []string, err error) {
if dirs, err := i.listDir("/"); err != nil { if dirs, err := i.ListDir("/"); err != nil {
return nil, err return nil, err
} else { } else {
for _, dir := range dirs { for _, dir := range dirs {
if !strings.HasPrefix(dir, ".") && !strings.HasPrefix(dir, "_") { if !strings.HasPrefix(dir, ".") && !strings.HasPrefix(dir, "_") {
if _, err := i.listDir(dir); err == nil { if _, err := i.ListDir(dir); err == nil {
themes = append(themes, dir) themes = append(themes, dir)
} }
} }