Skip to content

Commit

Permalink
load preview file
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Aug 4, 2016
1 parent 479db24 commit f9b2518
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 41 deletions.
45 changes: 43 additions & 2 deletions httpstaticserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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" {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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()
}
18 changes: 9 additions & 9 deletions res/index.tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
{{breadcrumb.slice(-1)[0].name}}
</li>
</ol>
<table class="table table-hover">
<table class="table table-hover" v-if="!previewMode">
<thead>
<tr>
<td colspan=4>
Expand All @@ -93,7 +93,7 @@
<tbody>
<tr v-for="f in computedFiles">
<td>
<a v-on:click='clickFileOrDir(f, $event)' href="/{{f.path}}">
<a v-on:click='clickFileOrDir(f, $event)' href="/{{f.path + (f.type == 'dir' ? '' : '?raw=false')}}">
<i style="padding-right: 0.5em" class="fa" v-bind:class='genFileClass(f)'></i> {{f.name}}
</a>
</td>
Expand All @@ -111,32 +111,32 @@
<span class="hidden-xs">Download</span>
<span class="glyphicon glyphicon-download-alt"></span>
</a>
<a class="btn btn-default btn-xs hidden-xs" v-on:click="genQrcode(f.name)" href="javascript:void(0)">
<button class="btn btn-default btn-xs hidden-xs" v-on:click="genQrcode(f.name)">
<span v-if="shouldHaveQrcode(f.name)">QRCode</span>
<span class="glyphicon glyphicon-qrcode"></span>
</a>
</button>
<a class="btn btn-default btn-xs visible-xs" v-if="shouldHaveQrcode(f.name)" href="{{genInstallURL(f.name)}}">
Install <i class="fa fa-cube"></i>
</a>
<a class="btn btn-default btn-xs" v-if="auth.delete" v-on:click="deletePathConfirm(f, $event)" href="javascript:void(0)">
<button class="btn btn-default btn-xs" v-if="auth.delete" v-on:click="deletePathConfirm(f, $event)">
<span style="color:#CC3300" class="glyphicon glyphicon-trash"></span>
</a>
</button>
</template>
</td>
</tr>
</tbody>
</table>
</div>
<div class="col-md-12" id="preview" v-if="previewFile">
<div class="col-md-12" id="preview" v-if="preview.filename">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title" style="font-weight: normal">
<i class="fa" v-bind:class='genFileClass(previewFile)'></i>
{{previewFile.name}}
{{preview.filename}}
</h3>
</div>
<div class="panel-body">
<article class="markdown-body">{{{previewFile.contentHTML }}}
<article class="markdown-body">{{{preview.contentHTML }}}
</article>
</div>
</div>
Expand Down
92 changes: 62 additions & 30 deletions res/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand All @@ -41,26 +47,19 @@ 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;
}
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']),
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = '<pre>' + res + '</pre>';
console.log("Finally")
})
.done(function(res) {
console.log("done", res)
});
}
}
})
Expand All @@ -211,31 +235,39 @@ window.onpopstate = function(event) {
loadFileList()
}

function loadDirectory(reqPath) {
function loadFileOrDir(reqPath) {
window.history.pushState({}, "", reqPath);
loadFileList(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
Expand Down

0 comments on commit f9b2518

Please sign in to comment.