From f9b25187347af67786fefbd82f5894e827760aa0 Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Thu, 4 Aug 2016 19:59:00 +0800 Subject: [PATCH] load preview file --- httpstaticserver.go | 45 +++++++++++++++++++++- res/index.tmpl.html | 18 ++++----- res/js/index.js | 92 ++++++++++++++++++++++++++++++--------------- 3 files changed, 114 insertions(+), 41 deletions(-) diff --git a/httpstaticserver.go b/httpstaticserver.go index d764095..57dd76a 100644 --- a/httpstaticserver.go +++ b/httpstaticserver.go @@ -73,6 +73,7 @@ func NewHTTPStaticServer(root string) *HTTPStaticServer { m.HandleFunc("/-/ipa/plist/{path:.*}", s.hPlist) m.HandleFunc("/-/ipa/link/{path:.*}", s.hIpaLink) // TODO: /ipa/info + m.HandleFunc("/-/info/{path:.*}", s.hInfo) m.HandleFunc("/{path:.*}", s.hIndex).Methods("GET") m.HandleFunc("/{path:.*}", s.hUpload).Methods("POST") @@ -88,8 +89,7 @@ func (s *HTTPStaticServer) hIndex(w http.ResponseWriter, r *http.Request) { path := mux.Vars(r)["path"] relPath := filepath.Join(s.Root, path) - finfo, err := os.Stat(relPath) - if err == nil && finfo.IsDir() { + if r.FormValue("raw") == "false" || isDir(relPath) { tmpl.ExecuteTemplate(w, "index", s) } else { if r.FormValue("download") == "true" { @@ -166,6 +166,42 @@ func (s *HTTPStaticServer) hUpload(w http.ResponseWriter, req *http.Request) { w.Write([]byte("Upload success")) } +type FileJSONInfo struct { + Name string `json:"name"` + Type string `json:"type"` + Size int64 `json:"size"` + Path string `json:"path"` + ModTime int64 `json:"mtime"` +} + +func (s *HTTPStaticServer) hInfo(w http.ResponseWriter, r *http.Request) { + path := mux.Vars(r)["path"] + relPath := filepath.Join(s.Root, path) + if !isFile(relPath) { + http.Error(w, "Not a file", 403) + return + } + fi, err := os.Stat(relPath) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + fji := &FileJSONInfo{ + Name: fi.Name(), + Size: fi.Size(), + Path: path, + ModTime: fi.ModTime().UnixNano() / 1e6, + } + if filepath.Ext(path) == ".md" { + fji.Type = "markdown" + } else { + fji.Type = "text" + } + data, _ := json.Marshal(fji) + w.Header().Set("Content-Type", "application/json") + w.Write(data) +} + func (s *HTTPStaticServer) hZip(w http.ResponseWriter, r *http.Request) { path := mux.Vars(r)["path"] CompressToZip(w, filepath.Join(s.Root, path)) @@ -456,3 +492,8 @@ func isFile(path string) bool { info, err := os.Stat(path) return err == nil && info.Mode().IsRegular() } + +func isDir(path string) bool { + info, err := os.Stat(path) + return err == nil && info.Mode().IsDir() +} diff --git a/res/index.tmpl.html b/res/index.tmpl.html index 22f467e..9bc4018 100644 --- a/res/index.tmpl.html +++ b/res/index.tmpl.html @@ -66,7 +66,7 @@ {{breadcrumb.slice(-1)[0].name}} - +
@@ -111,32 +111,32 @@ - Install - +
@@ -93,7 +93,7 @@
- + {{f.name}}
-
+

- {{previewFile.name}} + {{preview.filename}}

-
{{{previewFile.contentHTML }}} +
{{{preview.contentHTML }}}
diff --git a/res/js/index.js b/res/js/index.js index 3a42396..8f8e141 100644 --- a/res/js/index.js +++ b/res/js/index.js @@ -26,7 +26,13 @@ var vm = new Vue({ location: window.location, breadcrumb: [], showHidden: false, - previewFile: null, + previewMode: false, + preview: { + filename: '', + filetype: '', + filesize: 0, + contentHTML: '', + }, version: "loading", mtimeTypeFromNow: false, // or fromNow auth: {}, @@ -41,17 +47,10 @@ var vm = new Vue({ computed: { computedFiles: function() { var that = this; - this.previewFile = null; var files = this.files.filter(function(f) { if (f.name == 'README.md') { - that.previewFile = { - name: f.name, - path: f.path, - size: f.size, - type: 'markdown', - contentHTML: '', - } + that.preview.filename = f.name; } if (!that.showHidden && f.name.slice(0, 1) === '.') { return false; @@ -59,8 +58,8 @@ var vm = new Vue({ return true; }); // console.log(this.previewFile) - if (this.previewFile) { - var name = this.previewFile.name; // For now only README.md + if (this.preview.filename) { + var name = this.preview.filename; // For now only README.md console.log(pathJoin([location.pathname, 'README.md'])) $.ajax({ url: pathJoin([location.pathname, 'README.md']), @@ -78,7 +77,7 @@ var vm = new Vue({ }); var html = converter.makeHtml(res); - that.previewFile.contentHTML = html; + that.preview.contentHTML = html; }, error: function(err) { console.log(err) @@ -160,15 +159,16 @@ var vm = new Vue({ return "fa-file-text-o" }, clickFileOrDir: function(f, e) { + // TODO: fix here tomorrow if (f.type == "file") { return true; } var reqPath = pathJoin([location.pathname, f.name]); - loadDirectory(reqPath); + loadFileOrDir(reqPath); e.preventDefault() }, changePath: function(reqPath, e) { - loadDirectory(reqPath); + loadFileOrDir(reqPath); e.preventDefault() }, deletePathConfirm: function(f, e) { @@ -202,6 +202,30 @@ var vm = new Vue({ }) } return this.breadcrumb; + }, + loadPreviewFile: function(filepath, e) { + if (e) { + e.preventDefault() // may be need a switch + } + var that = this; + $.getJSON(pathJoin(['/-/info', location.pathname])) + .then(function(res) { + console.log(res); + that.preview.filename = res.name; + that.preview.filesize = res.size; + return $.ajax({ + url: '/' + res.path, + dataType: 'text', + }); + }) + .then(function(res) { + console.log(res) + that.preview.contentHTML = '
' + res + '
'; + console.log("Finally") + }) + .done(function(res) { + console.log("done", res) + }); } } }) @@ -211,7 +235,7 @@ window.onpopstate = function(event) { loadFileList() } -function loadDirectory(reqPath) { +function loadFileOrDir(reqPath) { window.history.pushState({}, "", reqPath); loadFileList(reqPath) } @@ -219,23 +243,31 @@ function loadDirectory(reqPath) { function loadFileList(pathname) { var pathname = pathname || location.pathname; // console.log("load filelist:", pathname) - $.ajax({ - url: pathJoin(["/-/json", pathname]), - dataType: "json", - cache: false, - success: function(res) { - res.files = _.sortBy(res.files, function(f) { - return [f.type, f.name]; - }) + if (getQueryString("raw") !== "false") { // not a file preview + $.ajax({ + url: pathJoin(["/-/json", pathname]), + dataType: "json", + cache: false, + success: function(res) { + res.files = _.sortBy(res.files, function(f) { + return [f.type, f.name]; + }) + + vm.files = res.files; + vm.auth = res.auth; + }, + error: function(err) { + console.error(err) + }, + }); + + } - vm.files = res.files; - vm.auth = res.auth; - }, - error: function(err) { - console.error(err) - }, - }); vm.updateBreadcrumb(); + vm.previewMode = getQueryString("raw") == "false"; + if (vm.previewMode) { + vm.loadPreviewFile(); + } } // For page first loading