⚡ Fix #156
This commit is contained in:
parent
26823c4cb9
commit
62947f4501
|
@ -3,6 +3,7 @@ package file
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/b3log/wide/util"
|
"github.com/b3log/wide/util"
|
||||||
|
@ -26,8 +27,13 @@ func GetZip(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filename := filepath.Base(path)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Disposition", "attachment; filename="+filename)
|
||||||
w.Header().Set("Content-type", "application/zip")
|
w.Header().Set("Content-type", "application/zip")
|
||||||
http.ServeFile(w, r, path)
|
http.ServeFile(w, r, path)
|
||||||
|
|
||||||
|
os.Remove(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateZip handles request of creating zip.
|
// CreateZip handles request of creating zip.
|
||||||
|
|
|
@ -133,5 +133,6 @@
|
||||||
"go_format": "Go Format",
|
"go_format": "Go Format",
|
||||||
"locale": "Locale",
|
"locale": "Locale",
|
||||||
"apply": "Apply",
|
"apply": "Apply",
|
||||||
"clearOutput": "Clear Output"
|
"clearOutput": "Clear Output",
|
||||||
|
"export": "Export"
|
||||||
}
|
}
|
|
@ -133,5 +133,6 @@
|
||||||
"go_format": "Go フォーマット",
|
"go_format": "Go フォーマット",
|
||||||
"locale": "ロケール",
|
"locale": "ロケール",
|
||||||
"apply": "適用する",
|
"apply": "適用する",
|
||||||
"clearOutput": "空の出力"
|
"clearOutput": "空の出力",
|
||||||
|
"export": "輸出"
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,5 +133,6 @@
|
||||||
"go_format": "Go 格式化",
|
"go_format": "Go 格式化",
|
||||||
"locale": "语言环境",
|
"locale": "语言环境",
|
||||||
"apply": "应用",
|
"apply": "应用",
|
||||||
"clearOutput": "清空输出"
|
"clearOutput": "清空输出",
|
||||||
|
"export": "导出"
|
||||||
}
|
}
|
|
@ -133,5 +133,6 @@
|
||||||
"go_format": "Go 格式化",
|
"go_format": "Go 格式化",
|
||||||
"locale": "語言環境",
|
"locale": "語言環境",
|
||||||
"apply": "應用",
|
"apply": "應用",
|
||||||
"clearOutput": "清空輸出"
|
"clearOutput": "清空輸出",
|
||||||
|
"export": "導出"
|
||||||
}
|
}
|
|
@ -150,6 +150,35 @@ var tree = {
|
||||||
$("#fileRMenu").hide();
|
$("#fileRMenu").hide();
|
||||||
$("#dialogRenamePrompt").dialog("open");
|
$("#dialogRenamePrompt").dialog("open");
|
||||||
},
|
},
|
||||||
|
export: function (it) {
|
||||||
|
if (it) {
|
||||||
|
if ($(it).hasClass("disabled")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var request = newWideRequest();
|
||||||
|
request.path = wide.curNode.path;
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
type: 'POST',
|
||||||
|
url: '/file/zip/new',
|
||||||
|
data: JSON.stringify(request),
|
||||||
|
dataType: "json",
|
||||||
|
success: function (data) {
|
||||||
|
if (!data.succ) {
|
||||||
|
$("#dialogAlert").dialog("open", data.msg);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
window.open('/file/zip?path=' + wide.curNode.path + '.zip');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$("#dirRMenu").hide();
|
||||||
|
$("#fileRMenu").hide();
|
||||||
|
},
|
||||||
init: function () {
|
init: function () {
|
||||||
$("#file").click(function () {
|
$("#file").click(function () {
|
||||||
$(this).focus();
|
$(this).focus();
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
"path"
|
"path"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -21,7 +22,7 @@ func (*mygo) GetAPIPath() string {
|
||||||
ret = runtime.GOROOT() + "/src" // Go 1.4 and after
|
ret = runtime.GOROOT() + "/src" // Go 1.4 and after
|
||||||
}
|
}
|
||||||
|
|
||||||
return path.Clean(ret)
|
return filepath.FromSlash(path.Clean(ret))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsAPI determines whether the specified path belongs to Go API.
|
// IsAPI determines whether the specified path belongs to Go API.
|
||||||
|
|
46
util/zip.go
46
util/zip.go
|
@ -28,32 +28,52 @@ func (*myzip) Create(filename string) (*ZipFile, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *ZipFile) Close() error {
|
func (z *ZipFile) Close() error {
|
||||||
err := z.zipFile.Close() // close the underlying writer
|
err := z.writer.Close()
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return z.writer.Close()
|
return z.zipFile.Close() // close the underlying writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *ZipFile) AddEntryN(dir string, names ...string) error {
|
func (z *ZipFile) AddEntryN(path string, names ...string) error {
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
zipPath := filepath.Join(dir, name)
|
zipPath := filepath.Join(path, name)
|
||||||
err := z.AddEntry(zipPath, name)
|
err := z.AddEntry(zipPath, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *ZipFile) AddEntry(dir, name string) error {
|
func (z *ZipFile) AddEntry(path, name string) error {
|
||||||
entry, err := z.writer.Create(dir)
|
fi, err := os.Stat(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fh, err := zip.FileInfoHeader(fi)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fh.Name = filepath.ToSlash(filepath.Clean(path))
|
||||||
|
fh.Method = zip.Deflate // data compression algorithm
|
||||||
|
|
||||||
|
if fi.IsDir() {
|
||||||
|
fh.Name = fh.Name + "/" // be care the ending separator
|
||||||
|
}
|
||||||
|
|
||||||
|
entry, err := z.writer.CreateHeader(fh)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
file, err := os.Open(name)
|
file, err := os.Open(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -65,18 +85,17 @@ func (z *ZipFile) AddEntry(dir, name string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *ZipFile) AddDirectoryN(dir string, names ...string) error {
|
func (z *ZipFile) AddDirectoryN(path string, names ...string) error {
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
err := z.AddDirectory(dir, name)
|
err := z.AddDirectory(path, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *ZipFile) AddDirectory(dir, dirName string) error {
|
func (z *ZipFile) AddDirectory(path, dirName string) error {
|
||||||
files, err := ioutil.ReadDir(dirName)
|
files, err := ioutil.ReadDir(dirName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -84,17 +103,16 @@ func (z *ZipFile) AddDirectory(dir, dirName string) error {
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
localPath := filepath.Join(dirName, file.Name())
|
localPath := filepath.Join(dirName, file.Name())
|
||||||
zipPath := filepath.Join(dir, file.Name())
|
zipPath := filepath.Join(path, file.Name())
|
||||||
|
|
||||||
err = nil
|
err = nil
|
||||||
if file.IsDir() {
|
if file.IsDir() {
|
||||||
z.AddEntry(dir, dirName)
|
z.AddEntry(path, dirName)
|
||||||
|
|
||||||
err = z.AddDirectory(zipPath, localPath)
|
err = z.AddDirectory(zipPath, localPath)
|
||||||
} else {
|
} else {
|
||||||
err = z.AddEntry(zipPath, localPath)
|
err = z.AddEntry(zipPath, localPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -138,6 +138,8 @@
|
||||||
<li class="create" onclick="tree.newDir(this);">{{.i18n.create_dir}}</li>
|
<li class="create" onclick="tree.newDir(this);">{{.i18n.create_dir}}</li>
|
||||||
<li class="remove" onclick="tree.removeIt(this);">{{.i18n.delete}}</li>
|
<li class="remove" onclick="tree.removeIt(this);">{{.i18n.delete}}</li>
|
||||||
<li class="rename" onclick="tree.rename(this);">{{.i18n.rename}}</li>
|
<li class="rename" onclick="tree.rename(this);">{{.i18n.rename}}</li>
|
||||||
|
<li class="hr"></li>
|
||||||
|
<li class="export" onclick="tree.export(this);">{{.i18n.export}}</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -146,6 +148,8 @@
|
||||||
<ul>
|
<ul>
|
||||||
<li class="remove" onclick="tree.removeIt(this);">{{.i18n.delete}}</li>
|
<li class="remove" onclick="tree.removeIt(this);">{{.i18n.delete}}</li>
|
||||||
<li class="rename" onclick="tree.rename(this);">{{.i18n.rename}}</li>
|
<li class="rename" onclick="tree.rename(this);">{{.i18n.rename}}</li>
|
||||||
|
<li class="hr"></li>
|
||||||
|
<li class="export" onclick="tree.export(this);">{{.i18n.export}}</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue