forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
322 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ | |
.tmp | ||
dist | ||
ui | ||
^ui/index.html | ||
^ui/index.html | ||
app/data/*/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
文件上传 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package global | ||
|
||
import ( | ||
jsoniter "github.com/json-iterator/go" | ||
"github.com/yaoapp/kun/exception" | ||
"github.com/yaoapp/xiang/data" | ||
"github.com/yaoapp/xiang/xfs" | ||
) | ||
|
||
// App 应用信息 | ||
var App AppInfo | ||
|
||
// AppInfo 应用信息 | ||
type AppInfo struct { | ||
Name string `json:"name,omitempty"` | ||
Short string `json:"short,omitempty"` | ||
Version string `json:"version,omitempty"` | ||
Description string `json:"description,omitempty"` | ||
Icons map[string]string `json:"icons,omitempty"` | ||
Storage AppStorage `json:"storage,omitempty"` | ||
Option map[string]interface{} `json:"option,omitempty"` | ||
} | ||
|
||
// AppStorage 应用存储 | ||
type AppStorage struct { | ||
Default string `json:"default"` | ||
Buckets map[string]string `json:"buckets,omitempty"` | ||
S3 map[string]interface{} `json:"s3,omitempty"` | ||
OSS map[string]interface{} `json:"oss,omitempty"` | ||
COS map[string]interface{} `json:"cos,omitempty"` | ||
} | ||
|
||
// LoadAppInfo 读取应用信息 | ||
func LoadAppInfo(root string) { | ||
info := defaultAppInfo() | ||
fs := xfs.New(root) | ||
if fs.MustExists("/app.json") { | ||
err := jsoniter.Unmarshal(fs.MustReadFile("/app.json"), &info) | ||
if err != nil { | ||
exception.New("解析应用失败 %s", 500, err).Throw() | ||
} | ||
} | ||
|
||
if fs.MustExists("/xiang/icons/icon.icns") { | ||
info.Icons["icns"] = xfs.Encode(fs.MustReadFile("/xiang/icons/icon.icns")) | ||
} | ||
|
||
if fs.MustExists("/xiang/icons/icon.ico") { | ||
info.Icons["ico"] = xfs.Encode(fs.MustReadFile("/xiang/icons/icon.ico")) | ||
} | ||
|
||
if fs.MustExists("/xiang/icons/icon.png") { | ||
info.Icons["png"] = xfs.Encode(fs.MustReadFile("/xiang/icons/icon.png")) | ||
} | ||
|
||
App = info | ||
} | ||
|
||
// defaultAppInfo 读取默认应用信息 | ||
func defaultAppInfo() AppInfo { | ||
info := AppInfo{} | ||
err := jsoniter.Unmarshal(data.MustAsset("xiang/data/app.json"), &info) | ||
if err != nil { | ||
exception.New("解析默认应用失败 %s", 500, err).Throw() | ||
} | ||
|
||
info.Version = VERSION | ||
info.Icons["icns"] = xfs.Encode(data.MustAsset("xiang/data/icons/icon.icns")) | ||
info.Icons["ico"] = xfs.Encode(data.MustAsset("xiang/data/icons/icon.ico")) | ||
info.Icons["png"] = xfs.Encode(data.MustAsset("xiang/data/icons/icon.png")) | ||
|
||
return info | ||
} | ||
|
||
// Public 输出公共信息 | ||
func (app AppInfo) Public() AppInfo { | ||
app.Storage.COS = nil | ||
app.Storage.OSS = nil | ||
app.Storage.S3 = nil | ||
return app | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package xfs | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/yaoapp/gou" | ||
"github.com/yaoapp/kun/exception" | ||
"github.com/yaoapp/kun/maps" | ||
"github.com/yaoapp/xun" | ||
) | ||
|
||
func init() { | ||
// 注册处理器 | ||
gou.RegisterProcessHandler("xiang.fs.Upload", processUpload) | ||
gou.RegisterProcessHandler("xiang.fs.GetToken", processGetToken) | ||
gou.RegisterProcessHandler("xiang.fs.GetURL", processGetURL) | ||
gou.RegisterProcessHandler("xiang.fs.ReadFile", processReadFile) | ||
} | ||
|
||
// processUpload 上传文件到本地服务器 | ||
func processUpload(process *gou.Process) interface{} { | ||
process.ValidateArgNums(1) | ||
tmpfile, ok := process.Args[0].(xun.UploadFile) | ||
if !ok { | ||
exception.New("上传文件参数错误", 400, process.Args[0]).Throw() | ||
} | ||
|
||
hash := md5.Sum([]byte(time.Now().Format("20060102-15:04:05"))) | ||
fingerprint := string(hex.EncodeToString(hash[:])) | ||
fingerprint = strings.ToUpper(fingerprint) | ||
|
||
dir := time.Now().Format("20060102") | ||
ext := filepath.Ext(tmpfile.Name) | ||
filename := filepath.Join(dir, fmt.Sprintf("%s%s", fingerprint, ext)) | ||
Stor.MustMkdirAll(dir, os.ModePerm) | ||
|
||
content, err := New("/").ReadFile(tmpfile.TempFile) | ||
if err != nil { | ||
exception.New("不能读取上传文件 %s", 500, err.Error()).Throw() | ||
} | ||
|
||
Stor.MustWriteFile(filename, content, os.ModePerm) | ||
return filename | ||
} | ||
|
||
// processGetContent 返回文件正文 | ||
func processReadFile(process *gou.Process) interface{} { | ||
process.ValidateArgNums(1) | ||
filename := process.ArgsString(0) | ||
encode := process.ArgsBool(1, true) | ||
|
||
stats, err := Stor.Stat(filename) | ||
if err != nil { | ||
exception.New("读取文件信息失败 %s", 500, err.Error()).Throw() | ||
} | ||
|
||
var content string | ||
body := Stor.MustReadFile(filename) | ||
if encode { | ||
content = Encode(body) | ||
} else { | ||
content = string(body) | ||
} | ||
|
||
return maps.Map{ | ||
"size": stats.Size(), | ||
"content": content, | ||
} | ||
} | ||
|
||
// processGetToken 上传文件到腾讯云对象存储 COS | ||
func processGetToken(process *gou.Process) interface{} { | ||
return nil | ||
} | ||
|
||
// processGetURL 返回文件CDN地址 | ||
func processGetURL(process *gou.Process) interface{} { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.