diff --git a/sui/api/process.go b/sui/api/process.go index 90211e41..bd0e41bc 100644 --- a/sui/api/process.go +++ b/sui/api/process.go @@ -59,6 +59,8 @@ func init() { "build.all": BuildAll, "build.page": BuildPage, + "sync.assetfile": SyncAssetFile, // Will be deprecated or change in the future + // Will be deprecated or change in the future "types.QueryParam": TypesQueryParam, }) @@ -896,6 +898,43 @@ func PreviewRender(process *process.Process) interface{} { return html } +// SyncAssetFile handle the render page request +func SyncAssetFile(process *process.Process) interface{} { + + process.ValidateArgNums(4) + sui := get(process) + templateID := process.ArgsString(1) + filename := process.ArgsString(2) + + option := process.ArgsMap(3, map[string]interface{}{}) + ssr := true + if v, ok := option["ssr"].(bool); ok { + ssr = v + } + + assetRoot := "" + if v, ok := option["asset_root"].(string); ok { + assetRoot = v + } + + data := map[string]interface{}{} + if v, ok := option["data"].(map[string]interface{}); ok { + data = v + } + + tmpl, err := sui.GetTemplate(templateID) + if err != nil { + exception.New(err.Error(), 500).Throw() + } + + err = tmpl.SyncAssetFile(filename, &core.BuildOption{SSR: ssr, AssetRoot: assetRoot, Data: data}) + if err != nil { + exception.New(err.Error(), 500).Throw() + } + + return nil +} + // BuildAll handle the render page request func BuildAll(process *process.Process) interface{} { diff --git a/sui/api/process_test.go b/sui/api/process_test.go index 6fbb6370..7fe5beca 100644 --- a/sui/api/process_test.go +++ b/sui/api/process_test.go @@ -747,6 +747,24 @@ func TestBuildPage(t *testing.T) { assert.Nil(t, res) } +func TestSyncAssetFile(t *testing.T) { + load(t) + defer clean() + + // test demo + p, err := process.Of("sui.sync.assetfile", "demo", "tech-blue", "/images/about/ab01.jpg", map[string]interface{}{"ssr": true}) + if err != nil { + t.Fatal(err) + } + + res, err := p.Exec() + if err != nil { + t.Fatal(err) + } + + assert.Nil(t, res) +} + func load(t *testing.T) { prepare(t) err := Load(config.Conf) diff --git a/sui/core/interfaces.go b/sui/core/interfaces.go index 593b91ab..10611e30 100644 --- a/sui/core/interfaces.go +++ b/sui/core/interfaces.go @@ -60,6 +60,7 @@ type ITemplate interface { Build(option *BuildOption) error SyncAssets(option *BuildOption) error + SyncAssetFile(file string, option *BuildOption) error GetRoot() string } diff --git a/sui/storages/local/build.go b/sui/storages/local/build.go index bd6743a8..58d4dc4e 100644 --- a/sui/storages/local/build.go +++ b/sui/storages/local/build.go @@ -52,6 +52,39 @@ func (tmpl *Template) Build(option *core.BuildOption) error { return err } +// SyncAssetFile sync the assets +func (tmpl *Template) SyncAssetFile(file string, option *core.BuildOption) error { + + // get source abs path + sourceRoot := filepath.Join(tmpl.local.fs.Root(), tmpl.Root, "__assets") + if exist, _ := os.Stat(sourceRoot); exist == nil { + return nil + } + + //get target abs path + root, err := tmpl.local.DSL.PublicRoot(option.Data) + if err != nil { + log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), tmpl.local.DSL.Public.Root) + root = tmpl.local.DSL.Public.Root + } + targetRoot := filepath.Join(application.App.Root(), "public", root, "assets") + + if exist, _ := os.Stat(targetRoot); exist == nil { + os.MkdirAll(targetRoot, os.ModePerm) + } + os.RemoveAll(targetRoot) + + sourceFile := filepath.Join(sourceRoot, file) + targetFile := filepath.Join(targetRoot, file) + + // create the target directory + if exist, _ := os.Stat(targetFile); exist == nil { + os.MkdirAll(filepath.Dir(targetFile), os.ModePerm) + } + + return copy(sourceFile, targetFile) +} + // SyncAssets sync the assets func (tmpl *Template) SyncAssets(option *core.BuildOption) error {