2017-11-27 01:45:33 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2021-10-26 17:56:40 +00:00
|
|
|
"fmt"
|
2022-05-24 20:13:38 +00:00
|
|
|
"io"
|
2017-11-27 01:45:33 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
2018-05-11 23:08:37 +00:00
|
|
|
// LocalImporter implements an Importer, where files to imports are located
|
|
|
|
// inside a local directory from your filesystem.
|
2017-11-27 01:45:33 +00:00
|
|
|
type LocalImporter struct {
|
2018-05-11 23:08:37 +00:00
|
|
|
// Base is the root directory used by the LocalImporter. It should contains all themes.
|
2021-10-26 17:56:40 +00:00
|
|
|
Base string
|
2018-05-11 23:08:37 +00:00
|
|
|
// Symlink changes the normal file copy/concatenate behaviour to symlink/concatenate.
|
|
|
|
// If enable, your base directory must be accessible by the frontend processus as it will follow the symlink.
|
2017-12-12 06:13:38 +00:00
|
|
|
Symlink bool
|
2017-11-27 01:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i LocalImporter) Kind() string {
|
2017-12-12 06:13:38 +00:00
|
|
|
if i.Symlink {
|
|
|
|
return "local file importer (through symlink): " + i.Base
|
|
|
|
} else {
|
|
|
|
return "local file importer: " + i.Base
|
|
|
|
}
|
2017-11-27 01:45:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 14:29:49 +00:00
|
|
|
func (i LocalImporter) Id() *string {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-26 17:56:40 +00:00
|
|
|
func (i LocalImporter) Init() error {
|
|
|
|
if f, err := os.Stat(i.Base); os.IsNotExist(err) {
|
|
|
|
if err = os.Mkdir(i.Base, 0751); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !f.IsDir() {
|
|
|
|
return fmt.Errorf("%q exists and is not a directory", i.Base)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i LocalImporter) Sync() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:54:02 +00:00
|
|
|
func (i LocalImporter) Exists(filename string) bool {
|
2017-12-12 06:13:38 +00:00
|
|
|
_, err := os.Stat(i.toURL(filename))
|
2017-11-27 01:45:33 +00:00
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i LocalImporter) toURL(filename string) string {
|
|
|
|
return path.Join(i.Base, filename)
|
|
|
|
}
|
|
|
|
|
2022-05-22 23:39:35 +00:00
|
|
|
func (i LocalImporter) GetLocalPath(p ...string) string {
|
|
|
|
return i.toURL(path.Join(p...))
|
|
|
|
}
|
|
|
|
|
2017-12-12 06:13:38 +00:00
|
|
|
func (i LocalImporter) importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error) {
|
|
|
|
if i.Symlink {
|
2023-11-25 17:38:12 +00:00
|
|
|
dest := GetDestinationFilePath(URI, nil)
|
2017-12-12 06:13:38 +00:00
|
|
|
|
2023-07-14 14:49:57 +00:00
|
|
|
if err := os.MkdirAll(path.Dir(dest), 0751); err != nil {
|
2017-12-12 06:13:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:54:02 +00:00
|
|
|
if i.Exists(URI) {
|
2017-12-12 06:13:38 +00:00
|
|
|
os.Symlink(i.toURL(URI), dest)
|
2020-01-20 15:02:40 +00:00
|
|
|
return next(dest, URI)
|
2017-12-12 06:13:38 +00:00
|
|
|
} else {
|
2021-10-26 17:56:40 +00:00
|
|
|
os.Symlink(i.toURL(URI)+"_MERGED", dest)
|
2020-01-20 15:02:40 +00:00
|
|
|
return ImportFile(i, URI, next)
|
2017-12-12 06:13:38 +00:00
|
|
|
}
|
2019-09-05 00:16:30 +00:00
|
|
|
} else {
|
|
|
|
return ImportFile(i, URI, next)
|
|
|
|
}
|
2017-12-12 06:13:38 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 16:13:31 +00:00
|
|
|
func (i LocalImporter) GetFile(filename string) (io.Reader, error) {
|
2017-11-27 01:45:33 +00:00
|
|
|
if fd, err := os.Open(path.Join(i.Base, filename)); err != nil {
|
2022-11-21 11:30:13 +00:00
|
|
|
return nil, err
|
2017-11-27 01:45:33 +00:00
|
|
|
} else {
|
2022-11-21 11:30:13 +00:00
|
|
|
return fd, nil
|
2017-11-27 01:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 20:13:38 +00:00
|
|
|
func (i LocalImporter) writeFile(filename string, reader io.Reader) error {
|
|
|
|
if fd, err := os.Create(path.Join(i.Base, filename)); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
defer fd.Close()
|
|
|
|
io.Copy(fd, reader)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-25 16:13:31 +00:00
|
|
|
func (i LocalImporter) ListDir(filename string) ([]string, error) {
|
2017-11-27 01:45:33 +00:00
|
|
|
if files, err := ioutil.ReadDir(path.Join(i.Base, filename)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
res := make([]string, 0)
|
|
|
|
for _, file := range files {
|
|
|
|
res = append(res, file.Name())
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
2018-01-07 21:21:13 +00:00
|
|
|
|
2023-11-25 16:13:31 +00:00
|
|
|
func (i LocalImporter) Stat(filename string) (os.FileInfo, error) {
|
2018-01-07 21:21:13 +00:00
|
|
|
return os.Stat(path.Join(i.Base, filename))
|
|
|
|
}
|