forked from x/ContextOS
mac add web.upload_tpl
This commit is contained in:
parent
350d2b7ad9
commit
d7caa7815a
@ -1,3 +1,2 @@
|
|||||||
source etc/local.shy
|
source etc/local.shy
|
||||||
~aaa login root root
|
~aaa login root root
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package web // {{{
|
|||||||
// }}}
|
// }}}
|
||||||
import ( // {{{
|
import ( // {{{
|
||||||
"contexts"
|
"contexts"
|
||||||
|
"strconv"
|
||||||
"toolkit"
|
"toolkit"
|
||||||
|
|
||||||
"encoding/json"
|
"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) {
|
web.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) {
|
||||||
msg := m.Spawn(m.Target()).Set("detail", key)
|
msg := m.Spawn(m.Target()).Set("detail", key)
|
||||||
|
|
||||||
|
msg.Add("option", "method", r.Method)
|
||||||
|
|
||||||
for k, v := range r.Form {
|
for k, v := range r.Form {
|
||||||
msg.Add("option", k, v...)
|
msg.Add("option", k, v...)
|
||||||
}
|
}
|
||||||
@ -370,7 +373,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
|||||||
"query": &ctx.Config{Name: "query", Value: "", Help: "主机参数"},
|
"query": &ctx.Config{Name: "query", Value: "", Help: "主机参数"},
|
||||||
"output": &ctx.Config{Name: "output", Value: "stdout", Help: "响应输出"},
|
"output": &ctx.Config{Name: "output", Value: "stdout", Help: "响应输出"},
|
||||||
"editor": &ctx.Config{Name: "editor", Value: "vim", 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{
|
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) {
|
"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) {
|
"/upload": &ctx.Command{Name: "/upload", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
r := m.Data["request"].(*http.Request) // {{{
|
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)
|
w := m.Data["response"].(http.ResponseWriter)
|
||||||
|
|
||||||
if !m.Options("file") {
|
if !m.Options("file") {
|
||||||
m.Option("file", m.Cap("directory"))
|
m.Option("file", m.Cap("directory"))
|
||||||
}
|
}
|
||||||
|
dir := m.Option("file")
|
||||||
|
|
||||||
file := m.Option("file")
|
if m.Option("method") == "POST" {
|
||||||
s, e := os.Stat(file)
|
file, header, e := r.FormFile("file")
|
||||||
if m.Assert(e); s.IsDir() {
|
|
||||||
fs, e := ioutil.ReadDir(file)
|
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
|
|
||||||
switch m.Option("list") {
|
name := path.Join(dir, header.Filename)
|
||||||
case "time":
|
|
||||||
if m.Option("order") == "max" {
|
if _, e := os.Stat(name); e != nil {
|
||||||
sort.Sort(listtime(fs))
|
f, e := os.Create(name)
|
||||||
} else {
|
m.Assert(e)
|
||||||
sort.Sort(sort.Reverse(listtime(fs)))
|
|
||||||
}
|
_, e = io.Copy(f, file)
|
||||||
case "size":
|
m.Assert(e)
|
||||||
if m.Option("order") == "max" {
|
m.Option("message", "", "\n", name)
|
||||||
sort.Sort(listsize(fs))
|
} else {
|
||||||
} else {
|
m.Option("message", "", "\n", name, "already exist!")
|
||||||
sort.Sort(sort.Reverse(listsize(fs)))
|
|
||||||
}
|
|
||||||
case "name":
|
|
||||||
if m.Option("order") == "max" {
|
|
||||||
sort.Sort(listname(fs))
|
|
||||||
} else {
|
|
||||||
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()))
|
|
||||||
}
|
|
||||||
|
|
||||||
m.Add("append", "name", v.Name())
|
|
||||||
}
|
|
||||||
w.Header().Add("Content-Type", "text/html")
|
|
||||||
m.Assert(template.Must(template.ParseGlob("usr/up.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}}
|
file := m.Option("file")
|
||||||
<td>{{index $meta $key $i}}</td>
|
|
||||||
{{end}}
|
s, e := os.Stat(file)
|
||||||
*/
|
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 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 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 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"))
|
||||||
|
m.Add("append", "size", kit.FmtSize(v.Size()))
|
||||||
|
|
||||||
|
name := v.Name()
|
||||||
|
if v.IsDir() {
|
||||||
|
name += "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Add("append", "name", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Add("Content-Type", "text/html")
|
||||||
|
m.Assert(template.Must(template.ParseGlob(m.Conf("upload_tpl"))).Execute(w, m.Meta))
|
||||||
|
delete(m.Meta, "result")
|
||||||
|
delete(m.Meta, "append")
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"temp": &ctx.Command{Name: "temp", Help: "应用示例", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"temp": &ctx.Command{Name: "temp", Help: "应用示例", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
|
78
usr/up.html
78
usr/up.html
@ -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">
|
<form method="POST" action="/upload" enctype="multipart/form-data">
|
||||||
<input type="text" name="path" value="tmp">
|
<fieldset><legend>upload</legend>
|
||||||
<input type="file" name="file">
|
<input type="file" name="file"><input type="submit">
|
||||||
<input type="submit">
|
</fieldset>
|
||||||
|
<fieldset><legend>message</legend>
|
||||||
|
{{index . "message"}}
|
||||||
|
</fieldset>
|
||||||
</form>
|
</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>
|
<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>
|
</script>
|
||||||
|
</body>
|
||||||
|
66
usr/up.tpl
66
usr/up.tpl
@ -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>
|
|
Loading…
x
Reference in New Issue
Block a user