⚡ Fix #156
This commit is contained in:
		
							parent
							
								
									26823c4cb9
								
							
						
					
					
						commit
						62947f4501
					
				|  | @ -3,6 +3,7 @@ package file | |||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"net/http" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 
 | ||||
| 	"github.com/b3log/wide/util" | ||||
|  | @ -26,8 +27,13 @@ func GetZip(w http.ResponseWriter, r *http.Request) { | |||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	filename := filepath.Base(path) | ||||
| 
 | ||||
| 	w.Header().Set("Content-Disposition", "attachment; filename="+filename) | ||||
| 	w.Header().Set("Content-type", "application/zip") | ||||
| 	http.ServeFile(w, r, path) | ||||
| 
 | ||||
| 	os.Remove(path) | ||||
| } | ||||
| 
 | ||||
| // CreateZip handles request of creating zip.
 | ||||
|  |  | |||
|  | @ -133,5 +133,6 @@ | |||
|     "go_format": "Go Format", | ||||
|     "locale": "Locale", | ||||
|     "apply": "Apply", | ||||
|     "clearOutput": "Clear Output" | ||||
|     "clearOutput": "Clear Output", | ||||
|     "export": "Export" | ||||
| } | ||||
|  | @ -133,5 +133,6 @@ | |||
|     "go_format": "Go フォーマット", | ||||
|     "locale": "ロケール", | ||||
|     "apply": "適用する", | ||||
|     "clearOutput": "空の出力" | ||||
|     "clearOutput": "空の出力", | ||||
|     "export": "輸出" | ||||
| } | ||||
|  |  | |||
|  | @ -133,5 +133,6 @@ | |||
|     "go_format": "Go 格式化", | ||||
|     "locale": "语言环境", | ||||
|     "apply": "应用", | ||||
|     "clearOutput": "清空输出" | ||||
|     "clearOutput": "清空输出", | ||||
|     "export": "导出" | ||||
| } | ||||
|  | @ -133,5 +133,6 @@ | |||
|     "go_format": "Go 格式化", | ||||
|     "locale": "語言環境", | ||||
|     "apply": "應用", | ||||
|     "clearOutput": "清空輸出" | ||||
|     "clearOutput": "清空輸出", | ||||
|     "export": "導出" | ||||
| } | ||||
|  | @ -150,6 +150,35 @@ var tree = { | |||
|         $("#fileRMenu").hide(); | ||||
|         $("#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 () { | ||||
|         $("#file").click(function () { | ||||
|             $(this).focus(); | ||||
|  | @ -289,7 +318,7 @@ var tree = { | |||
|                         var w = window.open(data.path); | ||||
|                         return false; | ||||
|                     } | ||||
|                      | ||||
| 
 | ||||
|                     if (!tempCursor) { | ||||
|                         tempCursor = CodeMirror.Pos(0, 0); | ||||
|                     } | ||||
|  |  | |||
|  | @ -1,6 +1,7 @@ | |||
| package util | ||||
| 
 | ||||
| import ( | ||||
| 	"path/filepath" | ||||
| 	"path" | ||||
| 	"runtime" | ||||
| 	"strings" | ||||
|  | @ -21,7 +22,7 @@ func (*mygo) GetAPIPath() string { | |||
| 		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.
 | ||||
|  |  | |||
							
								
								
									
										46
									
								
								util/zip.go
								
								
								
								
							
							
						
						
									
										46
									
								
								util/zip.go
								
								
								
								
							|  | @ -28,32 +28,52 @@ func (*myzip) Create(filename string) (*ZipFile, error) { | |||
| } | ||||
| 
 | ||||
| func (z *ZipFile) Close() error { | ||||
| 	err := z.zipFile.Close() // close the underlying writer
 | ||||
| 	err := z.writer.Close() | ||||
| 	if nil != 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 { | ||||
| 		zipPath := filepath.Join(dir, name) | ||||
| 		zipPath := filepath.Join(path, name) | ||||
| 		err := z.AddEntry(zipPath, name) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| func (z *ZipFile) AddEntry(dir, name string) error { | ||||
| 	entry, err := z.writer.Create(dir) | ||||
| func (z *ZipFile) AddEntry(path, name string) error { | ||||
| 	fi, err := os.Stat(name) | ||||
| 	if err != nil { | ||||
| 		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) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
|  | @ -65,18 +85,17 @@ func (z *ZipFile) AddEntry(dir, name string) error { | |||
| 	return err | ||||
| } | ||||
| 
 | ||||
| func (z *ZipFile) AddDirectoryN(dir string, names ...string) error { | ||||
| func (z *ZipFile) AddDirectoryN(path string, names ...string) error { | ||||
| 	for _, name := range names { | ||||
| 		err := z.AddDirectory(dir, name) | ||||
| 		err := z.AddDirectory(path, name) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| func (z *ZipFile) AddDirectory(dir, dirName string) error { | ||||
| func (z *ZipFile) AddDirectory(path, dirName string) error { | ||||
| 	files, err := ioutil.ReadDir(dirName) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
|  | @ -84,17 +103,16 @@ func (z *ZipFile) AddDirectory(dir, dirName string) error { | |||
| 
 | ||||
| 	for _, file := range files { | ||||
| 		localPath := filepath.Join(dirName, file.Name()) | ||||
| 		zipPath := filepath.Join(dir, file.Name()) | ||||
| 		zipPath := filepath.Join(path, file.Name()) | ||||
| 
 | ||||
| 		err = nil | ||||
| 		if file.IsDir() { | ||||
| 			z.AddEntry(dir, dirName) | ||||
| 			z.AddEntry(path, dirName) | ||||
| 
 | ||||
| 			err = z.AddDirectory(zipPath, localPath) | ||||
| 		} else { | ||||
| 			err = z.AddEntry(zipPath, localPath) | ||||
| 		} | ||||
| 
 | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  |  | |||
|  | @ -138,6 +138,8 @@ | |||
|                                 <li class="create" onclick="tree.newDir(this);">{{.i18n.create_dir}}</li> | ||||
|                                 <li class="remove" onclick="tree.removeIt(this);">{{.i18n.delete}}</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> | ||||
|                         </div> | ||||
| 
 | ||||
|  | @ -146,6 +148,8 @@ | |||
|                             <ul> | ||||
|                                 <li class="remove" onclick="tree.removeIt(this);">{{.i18n.delete}}</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> | ||||
|                         </div> | ||||
|                     </div> | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue