Skip to content

Commit

Permalink
Merge pull request #492 from trheyi/main
Browse files Browse the repository at this point in the history
[add] sui template asset upload api
  • Loading branch information
trheyi authored Oct 31, 2023
2 parents 5af56e9 + 92d22af commit 010c371
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
7 changes: 6 additions & 1 deletion sui/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var dsl = []byte(`
"paths": [
{
"path": "/:id/setting",
"guard": "-",
"method": "GET",
"process": "sui.Setting",
"in": ["$param.id"],
Expand Down Expand Up @@ -175,6 +174,12 @@ var dsl = []byte(`
"body": "?:content",
"headers": { "Content-Type": "?:type"}
}
},{
"path": "/:id/asset/:template_id/upload",
"method": "POST",
"process": "sui.Template.AssetUpload",
"in": ["$param.id", "$param.template_id", ":context"],
"out": { "status": 200, "type": "application/json" }
},{
"path": "/:id/asset/:template_id/@pages/*path",
"guard": "query-jwt",
Expand Down
63 changes: 60 additions & 3 deletions sui/api/process.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"encoding/base64"
"fmt"
"net/url"
"path/filepath"
Expand All @@ -17,9 +18,10 @@ func init() {
process.RegisterGroup("sui", map[string]process.Handler{
"setting": Setting,

"template.get": TemplateGet,
"template.find": TemplateFind,
"template.asset": TemplateAsset,
"template.get": TemplateGet,
"template.find": TemplateFind,
"template.asset": TemplateAsset,
"template.assetupload": TemplateAssetUpload,

"locale.get": LocaleGet,
"theme.get": ThemeGet,
Expand Down Expand Up @@ -108,6 +110,61 @@ func TemplateAsset(process *process.Process) interface{} {
}
}

// TemplateAssetUpload handle the find Template request
func TemplateAssetUpload(process *process.Process) interface{} {
process.ValidateArgNums(3)
sui := get(process)
tmpl, err := sui.GetTemplate(process.ArgsString(1))
if err != nil {
exception.New(err.Error(), 500).Throw()
}

switch v := process.Args[2].(type) {
case *gin.Context:
file, err := v.FormFile("file")
if err != nil {
exception.New(err.Error(), 500).Throw()
}

reader, err := file.Open()
if err != nil {
exception.New(err.Error(), 500).Throw()
}
defer reader.Close()

path, err := tmpl.AssetUpload(reader, file.Filename)
if err != nil {
exception.New(err.Error(), 500).Throw()
}

url := v.PostForm("url")
fileurl := fmt.Sprintf("%s/%s", url, path)
// time.Sleep(10 * time.Second)
return map[string]interface{}{
"data": []interface{}{fileurl},
"header": file.Header,
}

case string:
data, err := base64.StdEncoding.DecodeString(v)
if err != nil {
exception.New(err.Error(), 500).Throw()
}

name := process.ArgsString(3, "file.png")
path, err := tmpl.AssetUpload(strings.NewReader(string(data)), name)
if err != nil {
exception.New(err.Error(), 500).Throw()
}

return path

default:
exception.New("the file is required", 400).Throw()
return nil
}
}

// LocaleGet handle the find Template request
func LocaleGet(process *process.Process) interface{} {
process.ValidateArgNums(2)
Expand Down
3 changes: 3 additions & 0 deletions sui/core/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package core

import "io"

// SUIs the loaded SUI instances
var SUIs = map[string]SUI{}

Expand Down Expand Up @@ -35,6 +37,7 @@ type ITemplate interface {
Themes() []SelectOption

Asset(file string) (*Asset, error)
AssetUpload(reader io.Reader, name string) (string, error)

Build(option *BuildOption) error
SyncAssets(option *BuildOption) error
Expand Down
19 changes: 19 additions & 0 deletions sui/storages/local/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package local

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"

"github.com/google/uuid"
"github.com/yaoapp/yao/sui/core"
"golang.org/x/text/language"
)
Expand Down Expand Up @@ -44,6 +49,20 @@ func (tmpl *Template) Themes() []core.SelectOption {
return tmpl.Template.Themes
}

// AssetUpload upload the asset
func (tmpl *Template) AssetUpload(reader io.Reader, name string) (string, error) {

fingerprint := strings.ToUpper(uuid.NewString())
dir := strings.Join([]string{string(os.PathSeparator), time.Now().Format("20060102")}, "")
ext := filepath.Ext(name)
file := filepath.Join(tmpl.Root, "__assets", "upload", dir, fmt.Sprintf("%s%s", fingerprint, ext))
_, err := tmpl.local.fs.Write(file, reader, 0644)
if err != nil {
return "", err
}
return filepath.Join("upload", dir, fmt.Sprintf("%s%s", fingerprint, ext)), nil
}

// Asset get the asset
func (tmpl *Template) Asset(file string) (*core.Asset, error) {

Expand Down

0 comments on commit 010c371

Please sign in to comment.