Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Developed the feature: Calculate md5sum and sha #197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@
<span class="hidden-xs">Download</span>
<span class="glyphicon glyphicon-download-alt"></span>
</a>
<a class="btn btn-default btn-xs hidden-xs" @click="openHashModal(f)">
<span class="hidden-xs">Hash</span>
<span class="glyphicon glyphicon-credit-card"></span>
</a>
<button class="btn btn-default btn-xs bstooltip" data-trigger="manual" data-title="Copied!"
data-clipboard-text="{{genDownloadURL(f)}}">
<i class="fa fa-copy"></i>
Expand Down Expand Up @@ -256,6 +260,47 @@ <h4 class="modal-title">
</div>
</div>
</div>

<!-- File hash modal -->
<div id="file-hash-modal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">
<span id="file-hash-title"></span>
</h4>
</div>
<div class="modal-body">
<pre id="file-hash-content">
<p
:style="{
color: hashModal.err ? 'red' : undefined,
whiteSpace: 'pre-wrap'
}"
>{{hashModal.content}}</p>
<div
:style="{
display: 'flex',
justifyContent: 'space-around',
}"
>
<button
v-for="(k, val) in ['sha256','sha512','md5']"
@click="genHash(val)"
class="
btn
btn-default
"
:key="val"
>{{val}}</button>
</div>

</pre>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div id="footer" class="pull-right" style="margin: 2em 1em">
Expand Down
70 changes: 49 additions & 21 deletions assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ var vm = new Vue({
breadcrumb: [],
showHidden: false,
previewMode: false,
hashModal: {
err: false,
title: 'Hash',
content: undefined,
filename: undefined,
},
preview: {
filename: '',
filetype: '',
Expand Down Expand Up @@ -187,6 +193,28 @@ var vm = new Vue({
$("#qrcodeRight a").attr("href", urlPath);
$("#qrcode-modal").modal("show");
},
openHashModal: function (f) {
console.log(this)
this.hashModal.filename = f?.name
this.hashModal.content = undefined
this.hashModal.err = false
$("#file-hash-modal").modal("show");
},
genHash: async function (hashType) {
try {
const url = new URL(this.getEncodePath(this.hashModal.filename)+`?op=queryHash&hashType=${hashType}`, location.origin)
const res = await $.ajax(url.toString())
if (!Boolean(res?.success)) {
throw Error(res.message)
}
this.hashModal.err = false
this.hashModal.content = `${res.type} : ${res.digestText}`
} catch (error) {
this.hashModal.err = true
this.hashModal.content = error?.message ?? 'Error, please try again.'
}

},
genDownloadURL: function (f) {
var search = location.search;
var sep = search == "" ? "?" : "&"
Expand Down Expand Up @@ -274,7 +302,7 @@ var vm = new Vue({
if (!name) {
return
}
if(!checkPathNameLegal(name)) {
if (!checkPathNameLegal(name)) {
alert("Name should not contains any of \\/:*<>|")
return
}
Expand Down Expand Up @@ -336,23 +364,23 @@ var vm = new Vue({
}
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)
.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)
});
},
loadAll: function () {
// TODO: move loadFileList here
Expand Down Expand Up @@ -443,10 +471,10 @@ $(function () {
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
$(e.trigger)
.tooltip('show')
.mouseleave(function () {
$(this).tooltip('hide');
})
.tooltip('show')
.mouseleave(function () {
$(this).tooltip('hide');
})

e.clearSelection();
});
Expand Down
56 changes: 56 additions & 0 deletions httpstaticserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package main

import (
"bytes"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/json"
"errors"
"fmt"
"hash"
"html/template"
"io"
"io/ioutil"
Expand Down Expand Up @@ -132,6 +137,12 @@ func (s *HTTPStaticServer) getRealPath(r *http.Request) string {
func (s *HTTPStaticServer) hIndex(w http.ResponseWriter, r *http.Request) {
path := mux.Vars(r)["path"]
realPath := s.getRealPath(r)
query := r.URL.Query()
if query.Get("op") == "queryHash" {
s.hDiget(w, r)
return
}

if r.FormValue("json") == "true" {
s.hJSONList(w, r)
return
Expand Down Expand Up @@ -168,6 +179,51 @@ func (s *HTTPStaticServer) hIndex(w http.ResponseWriter, r *http.Request) {
}
}

func (s *HTTPStaticServer) hDiget(w http.ResponseWriter, r *http.Request) {
path := mux.Vars(r)["path"]
hashType := r.URL.Query().Get("hashType")

realPath := s.getRealPath(r)

if filepath.Base(path) == YAMLCONF {
auth := s.readAccessConf(realPath)
fmt.Println(auth)
if !auth.Delete {
http.Error(w, "Security warning, not allowed to read", http.StatusForbidden)
return
}
}
w.Header().Set("Content-Type", "application/json;charset=utf-8") // for ajax

file, err := os.ReadFile(realPath)
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{
"success": false,
"message": "No Such File",
})
return
}
hashTypeDic := map[string](func() hash.Hash){"sha256": sha256.New, "sha512": sha512.New, "md5": md5.New, "sha1": sha1.New}

var digest hash.Hash
digestHandler, ok := hashTypeDic[hashType]
if !ok {
json.NewEncoder(w).Encode(map[string]interface{}{
"success": false,
"message": "Failed to Create Hash Text",
})
return
}
digest = digestHandler()
digest.Write(file)
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"type": hashType,
"digestText": digest.Sum(nil),
})

}

func (s *HTTPStaticServer) hDelete(w http.ResponseWriter, req *http.Request) {
path := mux.Vars(req)["path"]
realPath := s.getRealPath(req)
Expand Down