mirror of
https://shylinux.com/x/icebergs
synced 2025-04-26 01:24:05 +08:00
opt some
This commit is contained in:
parent
46193f5f2f
commit
e39c6cdf8f
@ -150,12 +150,24 @@ func CloseFile(m *ice.Message, p ice.Any) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CopyFile(m *ice.Message, to io.WriteCloser, from io.ReadCloser, cb func(int)) {
|
func CopyFile(m *ice.Message, to io.WriteCloser, from io.ReadCloser, total int, cb ice.Any) {
|
||||||
buf := make([]byte, 1024*1024)
|
size, buf := 0, make([]byte, ice.MOD_BUFS)
|
||||||
for {
|
for {
|
||||||
n, e := from.Read(buf)
|
n, e := from.Read(buf)
|
||||||
to.Write(buf[:n])
|
to.Write(buf[0:n])
|
||||||
if cb(n); e != nil {
|
if size += n; size > total {
|
||||||
|
total = size
|
||||||
|
}
|
||||||
|
switch step := size * 100 / total; cb := cb.(type) {
|
||||||
|
case func(int, int, int):
|
||||||
|
cb(size, total, step)
|
||||||
|
case func(int, int):
|
||||||
|
cb(size, total)
|
||||||
|
case nil:
|
||||||
|
default:
|
||||||
|
m.ErrorNotImplement(cb)
|
||||||
|
}
|
||||||
|
if m.Warn(e) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,13 +27,15 @@ func _cache_type(m *ice.Message, kind, name string) string {
|
|||||||
return kind
|
return kind
|
||||||
}
|
}
|
||||||
func _cache_save(m *ice.Message, kind, name, text string, arg ...string) {
|
func _cache_save(m *ice.Message, kind, name, text string, arg ...string) {
|
||||||
m.Assert(name == "", ice.ErrNotValid, mdb.NAME)
|
if m.Warn(name == "", ice.ErrNotValid, mdb.NAME) {
|
||||||
|
return
|
||||||
|
}
|
||||||
if len(text) > 512 || kind == nfs.GO {
|
if len(text) > 512 || kind == nfs.GO {
|
||||||
p := m.Cmdx(nfs.SAVE, _cache_name(m, kit.Hashs(text)), text)
|
p := m.Cmdx(nfs.SAVE, _cache_name(m, kit.Hashs(text)), text)
|
||||||
text, arg = p, kit.Simple(p, len(text))
|
text, arg = p, kit.Simple(p, len(text))
|
||||||
}
|
}
|
||||||
file, size := kit.Select("", arg, 0), kit.Int(kit.Select(kit.Format(len(text)), arg, 1))
|
file, size := kit.Select("", arg, 0), kit.Int(kit.Select(kit.Format(len(text)), arg, 1))
|
||||||
kind, text = _cache_type(kind, name), kit.Select(file, text)
|
kind, text = _cache_type(m, kind, name), kit.Select(file, text)
|
||||||
h := mdb.HashCreate(m, kit.SimpleKV("", kind, name, text), nfs.FILE, file, nfs.SIZE, size)
|
h := mdb.HashCreate(m, kit.SimpleKV("", kind, name, text), nfs.FILE, file, nfs.SIZE, size)
|
||||||
m.Push(mdb.TIME, m.Time())
|
m.Push(mdb.TIME, m.Time())
|
||||||
m.Push(mdb.TYPE, kind)
|
m.Push(mdb.TYPE, kind)
|
||||||
@ -55,7 +57,7 @@ func _cache_watch(m *ice.Message, key, file string) {
|
|||||||
}
|
}
|
||||||
func _cache_catch(m *ice.Message, name string) (file, size string) {
|
func _cache_catch(m *ice.Message, name string) (file, size string) {
|
||||||
if msg := m.Cmd(nfs.DIR, name, "hash,size"); msg.Length() > 0 {
|
if msg := m.Cmd(nfs.DIR, name, "hash,size"); msg.Length() > 0 {
|
||||||
return m.Cmdx(nfs.LINK, _cache_name(m, msg.Append(mdb.HASH)), name), mdb.Append(mdb.SIZE)
|
return m.Cmdx(nfs.LINK, _cache_name(m, msg.Append(mdb.HASH)), name), msg.Append(nfs.SIZE)
|
||||||
}
|
}
|
||||||
return "", "0"
|
return "", "0"
|
||||||
}
|
}
|
||||||
@ -73,39 +75,14 @@ func _cache_upload(m *ice.Message, r *http.Request) (kind, name, file, size stri
|
|||||||
}
|
}
|
||||||
return "", "", "", "0"
|
return "", "", "", "0"
|
||||||
}
|
}
|
||||||
func _cache_download(m *ice.Message, r *http.Response) (file, size string) {
|
func _cache_download(m *ice.Message, r *http.Response, file string) string {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
if f, p, e := miss.CreateFile(file); m.Warn(e, ice.ErrNotValid, DOWNLOAD) {
|
||||||
if f, p, e := miss.CreateFile(path.Join(ice.VAR_TMP, kit.Hashs(mdb.UNIQ))); m.Assert(e) {
|
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
nfs.CopyFile(m, f, r.Body, kit.Int(kit.Select("100", r.Header.Get(ContentLength))), m.OptionCB(SPIDE))
|
||||||
step, total := 0, kit.Int(kit.Select("100", r.Header.Get(ContentLength)))
|
return p
|
||||||
size, buf := 0, make([]byte, ice.MOD_BUFS)
|
|
||||||
|
|
||||||
for {
|
|
||||||
if n, e := r.Body.Read(buf); n > 0 && e == nil {
|
|
||||||
size += n
|
|
||||||
f.Write(buf[0:n])
|
|
||||||
s := size * 100 / total
|
|
||||||
|
|
||||||
switch cb := m.OptionCB(SPIDE).(type) {
|
|
||||||
case func(int, int, int):
|
|
||||||
cb(size, total, s)
|
|
||||||
case func(int, int):
|
|
||||||
cb(size, total)
|
|
||||||
default:
|
|
||||||
if s != step && s%10 == 0 {
|
|
||||||
m.Logs(mdb.IMPORT, nfs.FILE, p, mdb.VALUE, s, mdb.COUNT, kit.FmtSize(int64(size)), mdb.TOTAL, kit.FmtSize(int64(total)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
step = s
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return p, kit.Format(size)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return "", "0"
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -139,8 +116,7 @@ func init() {
|
|||||||
}},
|
}},
|
||||||
DOWNLOAD: {Name: "download type name", Help: "下载", Hand: func(m *ice.Message, arg ...string) {
|
DOWNLOAD: {Name: "download type name", Help: "下载", Hand: func(m *ice.Message, arg ...string) {
|
||||||
if r, ok := m.Optionv(RESPONSE).(*http.Response); ok {
|
if r, ok := m.Optionv(RESPONSE).(*http.Response); ok {
|
||||||
file, size := _cache_download(m, r)
|
file, size := _cache_catch(m, _cache_download(m, r, path.Join(ice.VAR_TMP, kit.Hashs(mdb.UNIQ))))
|
||||||
file, size = _cache_catch(m, file)
|
|
||||||
_cache_save(m, arg[0], arg[1], "", file, size)
|
_cache_save(m, arg[0], arg[1], "", file, size)
|
||||||
}
|
}
|
||||||
}},
|
}},
|
||||||
@ -150,10 +126,9 @@ func init() {
|
|||||||
msg := m.Cmd(CACHE, UPLOAD)
|
msg := m.Cmd(CACHE, UPLOAD)
|
||||||
up = kit.Simple(msg.Append(mdb.HASH), msg.Append(mdb.NAME), msg.Append(nfs.SIZE))
|
up = kit.Simple(msg.Append(mdb.HASH), msg.Append(mdb.NAME), msg.Append(nfs.SIZE))
|
||||||
}
|
}
|
||||||
|
|
||||||
if p := path.Join(arg[0], up[1]); m.Option(ice.MSG_USERPOD) == "" {
|
if p := path.Join(arg[0], up[1]); m.Option(ice.MSG_USERPOD) == "" {
|
||||||
m.Cmdy(CACHE, WATCH, up[0], p) // 本机文件
|
m.Cmdy(CACHE, WATCH, up[0], p)
|
||||||
} else { // 下发文件
|
} else {
|
||||||
m.Cmdy(SPIDE, ice.DEV, nfs.SAVE, p, SPIDE_GET, MergeURL2(m, path.Join(SHARE_CACHE, up[0])))
|
m.Cmdy(SPIDE, ice.DEV, nfs.SAVE, p, SPIDE_GET, MergeURL2(m, path.Join(SHARE_CACHE, up[0])))
|
||||||
}
|
}
|
||||||
}},
|
}},
|
||||||
|
@ -202,7 +202,7 @@ func init() {
|
|||||||
_share_local(m, ice.USR_PUBLISH, path.Join(arg...))
|
_share_local(m, ice.USR_PUBLISH, path.Join(arg...))
|
||||||
}},
|
}},
|
||||||
PP(ice.REQUIRE): {Name: "/require/shylinux.com/x/volcanos/proto.js", Help: "代码库", Hand: func(m *ice.Message, arg ...string) {
|
PP(ice.REQUIRE): {Name: "/require/shylinux.com/x/volcanos/proto.js", Help: "代码库", Hand: func(m *ice.Message, arg ...string) {
|
||||||
_share_repos(m, path.Join(arg[0], arg[1], arg[2]), arg[3:]...)
|
// _share_repos(m, path.Join(arg[0], arg[1], arg[2]), arg[3:]...)
|
||||||
}},
|
}},
|
||||||
PP(ice.REQUIRE, ice.NODE_MODULES): {Name: "/require/node_modules/", Help: "依赖库", Hand: func(m *ice.Message, arg ...string) {
|
PP(ice.REQUIRE, ice.NODE_MODULES): {Name: "/require/node_modules/", Help: "依赖库", Hand: func(m *ice.Message, arg ...string) {
|
||||||
p := path.Join(ice.SRC, ice.NODE_MODULES, path.Join(arg...))
|
p := path.Join(ice.SRC, ice.NODE_MODULES, path.Join(arg...))
|
||||||
|
@ -261,24 +261,7 @@ func _spide_save(m *ice.Message, cache, save, uri string, res *http.Response) {
|
|||||||
m.Resultv(data[ice.MSG_RESULT])
|
m.Resultv(data[ice.MSG_RESULT])
|
||||||
|
|
||||||
case SPIDE_SAVE:
|
case SPIDE_SAVE:
|
||||||
if f, p, e := nfs.CreateFile(m, save); m.Assert(e) {
|
_cache_download(m, res, save)
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
total := kit.Int(res.Header.Get(ContentLength)) + 1
|
|
||||||
switch cb := m.OptionCB("").(type) {
|
|
||||||
case func(int, int, int):
|
|
||||||
count := 0
|
|
||||||
nfs.CopyFile(m, f, res.Body, func(n int) {
|
|
||||||
count += n
|
|
||||||
cb(count, total, count*100/total)
|
|
||||||
})
|
|
||||||
default:
|
|
||||||
if n, e := io.Copy(f, res.Body); m.Assert(e) {
|
|
||||||
m.Logs(mdb.EXPORT, nfs.SIZE, n, nfs.FILE, p)
|
|
||||||
m.Echo(p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case SPIDE_CACHE:
|
case SPIDE_CACHE:
|
||||||
m.Optionv(RESPONSE, res)
|
m.Optionv(RESPONSE, res)
|
||||||
@ -299,6 +282,15 @@ func _spide_save(m *ice.Message, cache, save, uri string, res *http.Response) {
|
|||||||
m.Push("", data)
|
m.Push("", data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func _cache_download(m *ice.Message, r *http.Response, file string) string {
|
||||||
|
defer r.Body.Close()
|
||||||
|
if f, p, e := miss.CreateFile(file); m.Warn(e, ice.ErrNotValid, DOWNLOAD) {
|
||||||
|
defer f.Close()
|
||||||
|
nfs.CopyFile(m, f, r.Body, kit.Int(kit.Select("100", r.Header.Get(ContentLength))), m.OptionCB(SPIDE))
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// 缓存方法
|
// 缓存方法
|
||||||
|
Loading…
x
Reference in New Issue
Block a user