finnally doooo and close #11

This commit is contained in:
Henrique Dias 2017-04-23 15:14:59 +01:00
parent 9d19467ad5
commit eb22fd20ce
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
6 changed files with 101 additions and 11 deletions

View File

@ -45,7 +45,6 @@ button {
padding: .5em 1em;
margin-left: .5em;
border-radius: .1em;
background-color;
cursor: pointer;
background: #2196f3;
color: #fff;
@ -376,10 +375,6 @@ header>div div {
position: relative;
}
header .actions {
/* margin-left: auto; */
}
#logout {
border-radius: 0;
margin-left: auto;
@ -1010,6 +1005,10 @@ header .actions {
padding: .3em;
}
.prompt code {
word-wrap: break-word;
}
.prompt div,
.help div {
margin-top: 1em;

View File

@ -302,17 +302,40 @@ function openEvent (event) {
return false
}
function getHash (event, hash) {
event.preventDefault()
let request = new window.XMLHttpRequest()
request.open('GET', `${window.location.pathname}?checksum=${hash}`, true)
request.onload = () => {
if (request.status >= 300) {
console.log(request.statusText)
return
}
event.target.parentElement.innerHTML = request.responseText
}
request.onerror = (e) => console.log(e)
request.send()
}
function infoEvent (event) {
event.preventDefault()
if (event.currentTarget.classList.contains('disabled')) {
return
}
let dir = false
let link
if (selectedItems.length) {
link = document.getElementById(selectedItems[0]).dataset.url
dir = document.getElementById(selectedItems[0]).dataset.dir
} else {
if (document.getElementById('listing') !== null) {
dir = true
}
link = window.location.pathname
}
@ -326,6 +349,10 @@ function infoEvent (event) {
clone.getElementById('content_length').innerHTML = xml.getElementsByTagName('getcontentlength')[0].innerHTML
clone.getElementById('last_modified').innerHTML = xml.getElementsByTagName('getlastmodified')[0].innerHTML
if (dir === true || dir === 'true') {
clone.querySelector('.file-only').style.display = 'none'
}
document.querySelector('body').appendChild(clone)
document.querySelector('.overlay').classList.add('active')
document.querySelector('.prompt').classList.add('active')

View File

@ -215,6 +215,14 @@
<p><strong>Display Name:</strong> <span id="display_name"></span></p>
<p><strong>Content Length:</strong> <span id="content_length"></span> Bytes</p>
<p><strong>Last Modified:</strong> <span id="last_modified"></span></p>
<section class="file-only">
<p><strong>MD5:</strong> <code id="md5"><a href="#" onclick="getHash(event, 'md5')">show</a></code></p>
<p><strong>SHA1:</strong> <code id="sha1"><a href="#" onclick="getHash(event, 'sha1')">show</a></code></p>
<p><strong>SHA256:</strong> <code id="sha256"><a href="#" onclick="getHash(event, 'sha256')">show</a></code></p>
<p><strong>SHA512:</strong> <code id="sha512"><a href="#" onclick="getHash(event, 'sha512')">show</a></code></p>
</section>
<div>
<button type="submit" onclick="closePrompt(event);" class="ok">OK</button>
</div>

File diff suppressed because one or more lines are too long

View File

@ -173,6 +173,8 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
case r.URL.Query().Get("raw") == "true" && !fi.IsDir:
http.ServeFile(w, r, fi.Path)
code, err = 0, nil
case !fi.IsDir && r.URL.Query().Get("checksum") != "":
code, err = handlers.Checksum(w, r, c, fi)
case fi.IsDir:
code, err = handlers.ServeListing(w, r, c, user, fi)
default:

54
handlers/checksum.go Normal file
View File

@ -0,0 +1,54 @@
package handlers
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
e "errors"
"hash"
"io"
"net/http"
"os"
"github.com/hacdias/caddy-filemanager/config"
"github.com/hacdias/caddy-filemanager/file"
"github.com/hacdias/caddy-filemanager/utils/errors"
)
// Checksum calculates the hash of a file. Supports MD5, SHA1, SHA256 and SHA512.
func Checksum(w http.ResponseWriter, r *http.Request, c *config.Config, i *file.Info) (int, error) {
query := r.URL.Query().Get("checksum")
file, err := os.Open(i.Path)
if err != nil {
return errors.ErrorToHTTPCode(err, true), err
}
defer file.Close()
var h hash.Hash
switch query {
case "md5":
h = md5.New()
case "sha1":
h = sha1.New()
case "sha256":
h = sha256.New()
case "sha512":
h = sha512.New()
default:
return http.StatusBadRequest, e.New("Unknown HASH type")
}
_, err = io.Copy(h, file)
if err != nil {
return http.StatusInternalServerError, err
}
val := hex.EncodeToString(h.Sum(nil))
w.Write([]byte(val))
return http.StatusOK, nil
}