1
0
forked from x/ContextOS

mac add web.upload_tpl

This commit is contained in:
shaoying 2018-05-22 01:07:09 +08:00
parent 350d2b7ad9
commit d7caa7815a
4 changed files with 152 additions and 151 deletions

View File

@ -1,3 +1,2 @@
source etc/local.shy
~aaa login root root

View File

@ -2,6 +2,7 @@ package web // {{{
// }}}
import ( // {{{
"contexts"
"strconv"
"toolkit"
"encoding/json"
@ -193,6 +194,8 @@ func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.C
web.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) {
msg := m.Spawn(m.Target()).Set("detail", key)
msg.Add("option", "method", r.Method)
for k, v := range r.Form {
msg.Add("option", k, v...)
}
@ -370,7 +373,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
"query": &ctx.Config{Name: "query", Value: "", Help: "主机参数"},
"output": &ctx.Config{Name: "output", Value: "stdout", Help: "响应输出"},
"editor": &ctx.Config{Name: "editor", Value: "vim", Help: "响应编辑器"},
"upload_dir": &ctx.Config{Name: "upload_dir", Value: "tmp", Help: "上传文件路径"},
"upload_tpl": &ctx.Config{Name: "upload_tpl", Value: "usr/up.html", Help: "上传文件路径"},
},
Commands: map[string]*ctx.Command{
"serve": &ctx.Command{Name: "serve [directory [address [protocol]]]", Help: "开启应用服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
@ -597,95 +600,90 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
}},
"/upload": &ctx.Command{Name: "/upload", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
r := m.Data["request"].(*http.Request) // {{{
file, header, e := r.FormFile("file")
m.Assert(e)
dir := r.FormValue("path")
if dir == "" {
dir = m.Conf("upload_dir")
}
name := path.Join(dir, header.Filename)
m.Append("path", name)
if _, e := os.Stat(name); e == nil {
m.Echo("failure, file already exist!")
return
}
f, e := os.Create(name)
m.Assert(e)
n, e := io.Copy(f, file)
m.Assert(e)
m.Echo("%d", n)
// }}}
}},
"download": &ctx.Command{Name: "download file", Help: "下载文件", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
msg := m.Spawn(m.Target()) // {{{
msg.Cmd("get", "/upload", "method", "POST", "file", "file", arg[0])
m.Copy(msg, "result")
// }}}
}},
"/download": &ctx.Command{Name: "/download", Help: "文件下载", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
r := m.Data["request"].(*http.Request) // {{{
w := m.Data["response"].(http.ResponseWriter)
if !m.Options("file") {
m.Option("file", m.Cap("directory"))
}
dir := m.Option("file")
if m.Option("method") == "POST" {
file, header, e := r.FormFile("file")
m.Assert(e)
name := path.Join(dir, header.Filename)
if _, e := os.Stat(name); e != nil {
f, e := os.Create(name)
m.Assert(e)
_, e = io.Copy(f, file)
m.Assert(e)
m.Option("message", "", "\n", name)
} else {
m.Option("message", "", "\n", name, "already exist!")
}
}
file := m.Option("file")
s, e := os.Stat(file)
if m.Assert(e); s.IsDir() {
if m.Assert(e); !s.IsDir() {
http.ServeFile(w, r, file)
return
}
fs, e := ioutil.ReadDir(file)
m.Assert(e)
max := true
if i, e := strconv.Atoi(m.Option("order")); e == nil {
max = i%2 == 1
}
switch m.Option("list") {
case "time":
if m.Option("order") == "max" {
if max {
m.Option("message", "sort by time")
sort.Sort(listtime(fs))
} else {
m.Option("message", "sort by time reverse")
sort.Sort(sort.Reverse(listtime(fs)))
}
case "size":
if m.Option("order") == "max" {
if max {
m.Option("message", "sort by size")
sort.Sort(listsize(fs))
} else {
m.Option("message", "sort by size reverse")
sort.Sort(sort.Reverse(listsize(fs)))
}
case "name":
if m.Option("order") == "max" {
if max {
m.Option("message", "sort by name")
sort.Sort(listname(fs))
} else {
m.Option("message", "sort by name reverse")
sort.Sort(sort.Reverse(listname(fs)))
}
}
for _, v := range fs {
m.Add("append", "time", v.ModTime().Format("2006-01-02 15:04:05"))
if v.IsDir() {
m.Add("append", "size", "---")
} else {
m.Add("append", "size", kit.FmtSize(v.Size()))
name := v.Name()
if v.IsDir() {
name += "/"
}
m.Add("append", "name", v.Name())
m.Add("append", "name", name)
}
w.Header().Add("Content-Type", "text/html")
m.Assert(template.Must(template.ParseGlob("usr/up.tpl")).Execute(w, m.Meta))
m.Assert(template.Must(template.ParseGlob(m.Conf("upload_tpl"))).Execute(w, m.Meta))
delete(m.Meta, "result")
delete(m.Meta, "append")
} else {
m.Log("fuck", nil, "why %s", file)
http.ServeFile(w, r, file)
}
/*
{{range $key := .append}}
<td>{{index $meta $key $i}}</td>
{{end}}
*/
// }}}
}},
"temp": &ctx.Command{Name: "temp", Help: "应用示例", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {

View File

@ -1,9 +1,79 @@
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<style>
th {
cursor:pointer;
background-color:lightgray;
}
.time {
padding-right:20px;
}
.size {
text-align:right;
padding-right:20px;
}
.name {
padding-left:10px;
text-align:left;
}
code {
font-size:14px;
}
</style>
</head>
<body>
<form method="POST" action="/upload" enctype="multipart/form-data">
<input type="text" name="path" value="tmp">
<input type="file" name="file">
<input type="submit">
<fieldset><legend>upload</legend>
<input type="file" name="file"><input type="submit">
</fieldset>
<fieldset><legend>message</legend>
{{index . "message"}}
</fieldset>
</form>
<table>
<colgroup>{{range .append}}<col class="{{.}}">{{end}}</colgroup>
<tr>{{range .append}}<th class="{{.}}" onclick="list('{{.}}')">{{.}}</th>{{end}}</tr>
{{$meta := .}} {{$first := index .append 0}}
{{range $i, $k := index . $first}}
<tr>
{{range $key := index $meta "append"}}
{{if eq $key "name"}}
<td class="{{$key}}">
<a href="/upload?file={{index $meta "file" 0}}/{{index $meta $key $i}}"><code>{{index $meta $key $i}}</code></a>
</td>
{{else}}
<td class="{{$key}}">
<code>{{index $meta $key $i}}</code>
</td>
{{end}}
{{end}}
</tr>
{{end}}
</table>
<script>
{{meta["append"]}}
function Cookie(name, value) {
if (value == undefined) {
var pattern = new RegExp(name+"=([^;]*);?");
var result = pattern.exec(document.cookie);
if (result && result.length > 0) {
return result[1];
}
return ""
}
document.cookie = name+"="+value;
return Cookie(name);
}
function list(what) {
Cookie("list", what);
Cookie("order", Cookie("order")*1 + 1)
location.assign(location.href)
}
</script>
</body>

View File

@ -1,66 +0,0 @@
<!DOCTYPE html>
<head>
<style>
th {
cursor:pointer;
background-color:lightgray;
}
.time {
padding-right:20px;
}
.size {
text-align:right;
padding-right:20px;
}
.name {
text-align:left;
}
code {
font-size:16px;
}
</style>
</head>
<body>
<form method="POST" action="/upload" enctype="multipart/form-data">
<input type="text" name="path" value="{{index . "file" 0}}">
<input type="file" name="file">
<input type="submit">
</form>
<button onclick="order('max')">max</button>
<button onclick="order('min')">min</button>
<script>
function getCookie(name) {
pattern = /([^=]+)=([^;]+);?\s*/g;
}
function order(what) {
document.cookie = "order="+what;
location.reload()
}
function list(what) {
document.cookie = "list="+what;
location.reload()
}
</script>
<table>
<colgroup>{{range .append}}<col class="{{.}}">{{end}}</colgroup>
<tr>{{range .append}}<th class="{{.}}" onclick="list('{{.}}')">{{.}}</th>{{end}}</tr>
{{$meta := .}} {{$first := index .append 0}}
{{range $i, $k := index . $first}}
<tr>
{{range $key := index $meta "append"}}
{{if eq $key "name"}}
<td class="{{$key}}">
<a href="/download?file={{index $meta "file" 0}}/{{index $meta $key $i}}"><code>{{index $meta $key $i}}</code></a>
</td>
{{else}}
<td class="{{$key}}">
<code>{{index $meta $key $i}}</code>
</td>
{{end}}
{{end}}
</tr>
{{end}}
</table>
</body>