From fce39ce4e2b077e22c08639864bf4a85cbfb9c68 Mon Sep 17 00:00:00 2001 From: Van Date: Sat, 21 Mar 2015 14:39:26 +0800 Subject: [PATCH] Fix #222 --- file/decompress.go | 57 +++++++++++++++++++++++++++++++++++++++++++++ i18n/en_US.json | 3 ++- i18n/ja_JP.json | 3 ++- i18n/zh_CN.json | 3 ++- i18n/zh_TW.json | 3 ++- main.go | 1 + static/js/tree.js | 29 ++++++++++++++++++++++- util/zip.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++ views/index.html | 5 +++- 9 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 file/decompress.go diff --git a/file/decompress.go b/file/decompress.go new file mode 100644 index 0000000..fd431cd --- /dev/null +++ b/file/decompress.go @@ -0,0 +1,57 @@ +// Copyright (c) 2014-2015, b3log.org +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package file + +import ( + "encoding/json" + "net/http" + "path/filepath" + + "github.com/b3log/wide/util" +) + +// DecompressHandler handles request of decompressing zip/tar.gz. +func DecompressHandler(w http.ResponseWriter, r *http.Request) { + data := map[string]interface{}{"succ": true} + defer util.RetJSON(w, r, data) + + var args map[string]interface{} + if err := json.NewDecoder(r.Body).Decode(&args); err != nil { + logger.Error(err) + data["succ"] = false + + return + } + + path := args["path"].(string) + // base := filepath.Base(path) + dir := filepath.Dir(path) + + if !util.File.IsExist(path) { + data["succ"] = false + data["msg"] = "Can't find file [" + path + "] to descompress" + + return + } + + err := util.Zip.Unzip(path, dir) + if nil != err { + logger.Error(err) + data["succ"] = false + + return + } + +} diff --git a/i18n/en_US.json b/i18n/en_US.json index 17e26b7..aabe531 100644 --- a/i18n/en_US.json +++ b/i18n/en_US.json @@ -176,5 +176,6 @@ "embeded": "Embeded", "git_clone": "Git Clone", "terms": "Terms", - "download": "Download" + "download": "Download", + "decompress": "Decompress" } \ No newline at end of file diff --git a/i18n/ja_JP.json b/i18n/ja_JP.json index c339013..e7fe0d4 100644 --- a/i18n/ja_JP.json +++ b/i18n/ja_JP.json @@ -176,5 +176,6 @@ "embeded": "埋め込む", "git_clone": "Git クローン", "terms": "利用規約", - "download": "ダウンロード" + "download": "ダウンロード", + "decompress": "解凍する" } diff --git a/i18n/zh_CN.json b/i18n/zh_CN.json index c24cdc0..d5e2b7e 100644 --- a/i18n/zh_CN.json +++ b/i18n/zh_CN.json @@ -176,5 +176,6 @@ "embeded": "嵌入", "git_clone": "Git 克隆", "terms": "使用条款", - "download": "下载" + "download": "下载", + "decompress": "解压缩" } \ No newline at end of file diff --git a/i18n/zh_TW.json b/i18n/zh_TW.json index ce40975..210716e 100644 --- a/i18n/zh_TW.json +++ b/i18n/zh_TW.json @@ -176,5 +176,6 @@ "embeded": "嵌入", "git_clone": "Git 克隆", "terms": "使用條款", - "download": "下載" + "download": "下載", + "decompress": "解壓縮" } \ No newline at end of file diff --git a/main.go b/main.go index 92fac8e..9c13a7c 100644 --- a/main.go +++ b/main.go @@ -143,6 +143,7 @@ func main() { http.HandleFunc(conf.Wide.Context+"/file/zip/new", handlerWrapper(file.CreateZipHandler)) http.HandleFunc(conf.Wide.Context+"/file/zip", handlerWrapper(file.GetZipHandler)) http.HandleFunc(conf.Wide.Context+"/file/upload", handlerWrapper(file.UploadHandler)) + http.HandleFunc(conf.Wide.Context+"/file/decompress", handlerWrapper(file.DecompressHandler)) // editor http.HandleFunc(conf.Wide.Context+"/editor/ws", handlerWrapper(editor.WSHandler)) diff --git a/static/js/tree.js b/static/js/tree.js index 0a1974a..131ba34 100644 --- a/static/js/tree.js +++ b/static/js/tree.js @@ -170,6 +170,27 @@ var tree = { window.open(config.context + '/file/zip?path=' + wide.curNode.path + ".zip"); } }, + decompress: function () { + var request = newWideRequest(); + request.path = wide.curNode.path; + + $.ajax({ + async: false, + type: 'POST', + url: config.context + '/file/decompress', + data: JSON.stringify(request), + dataType: "json", + success: function (data) { + if (!data.succ) { + $("#dialogAlert").dialog("open", data.msg); + + return false; + } + + isSucc = true; + } + }); + }, refresh: function (it) { if (it) { if ($(it).hasClass("disabled")) { @@ -248,12 +269,18 @@ var tree = { wide.curNode = treeNode; tree.fileTree.selectNode(treeNode); - if (!tree.isDir()) { // 如果右击了文件 + if (!tree.isDir()) { // if right click on a file if (wide.curNode.removable) { $fileRMenu.find(".remove").removeClass("disabled"); } else { $fileRMenu.find(".remove").addClass("disabled"); } + + if (wide.curNode.path.indexOf("zip", wide.curNode.path.length - "zip".length) === -1) { // !path.endsWith("zip") + $fileRMenu.find(".decompress").hide(); + } else { + $fileRMenu.find(".decompress").show(); + } var top = event.clientY - 10; if ($fileRMenu.height() + top > $('.content').height()) { diff --git a/util/zip.go b/util/zip.go index c4c62d3..3ace068 100644 --- a/util/zip.go +++ b/util/zip.go @@ -141,3 +141,61 @@ func (z *ZipFile) AddDirectory(path, dirName string) error { return nil } + +func cloneZipItem(f *zip.File, dest string) error { + // create full directory path + path := filepath.Join(dest, f.Name) + + err := os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm) + if nil != err { + return err + } + + if f.FileInfo().IsDir() { + return nil + } + + // clone if item is a file + + rc, err := f.Open() + if nil != err { + return err + } + + defer rc.Close() + + // use os.Create() since Zip don't store file permissions. + fileCopy, err := os.Create(path) + if nil != err { + return err + } + + defer fileCopy.Close() + + _, err = io.Copy(fileCopy, rc) + if nil != err { + return err + } + + return nil +} + +// Unzip extracts a zip file specified by the zipFilePath to the destination. +func (*myzip) Unzip(zipFilePath, destination string) error { + r, err := zip.OpenReader(zipFilePath) + + if nil != err { + return err + } + + defer r.Close() + + for _, f := range r.File { + err = cloneZipItem(f, destination) + if nil != err { + return err + } + } + + return nil +} diff --git a/views/index.html b/views/index.html index 5557074..d9ecf99 100644 --- a/views/index.html +++ b/views/index.html @@ -457,9 +457,12 @@ {{.i18n.rename}}
  • -
  • +
  • {{.i18n.export}}
  • +
  • + {{.i18n.decompress}} +