hash: nit

This commit is contained in:
Pierre Dubouilh 2025-02-24 21:16:07 +01:00
parent 2659e2de93
commit bcacb0f24b
2 changed files with 18 additions and 20 deletions

View file

@ -232,10 +232,11 @@ func zipRPC(w http.ResponseWriter, r *http.Request) {
func rpc(w http.ResponseWriter, r *http.Request) { func rpc(w http.ResponseWriter, r *http.Request) {
var err error var err error
var rpc rpcCall var rpc rpcCall
defer exitPath(w, "rpc", rpc) defer exitPath(w, "rpc", &rpc)
bodyBytes, err := io.ReadAll(r.Body) bodyBytes, err := io.ReadAll(r.Body)
check(err) check(err)
json.Unmarshal(bodyBytes, &rpc) json.Unmarshal(bodyBytes, &rpc)
ret := []byte("ok")
switch rpc.Call { switch rpc.Call {
case "mkdirp": case "mkdirp":
@ -245,9 +246,8 @@ func rpc(w http.ResponseWriter, r *http.Request) {
case "rm": case "rm":
err = os.RemoveAll(enforcePath(rpc.Args[0])) err = os.RemoveAll(enforcePath(rpc.Args[0]))
case "sum": case "sum":
var file *os.File file, err := os.Open(enforcePath(rpc.Args[0]))
enforcedPath := enforcePath(rpc.Args[0]) check(err)
file, err = os.Open(enforcedPath)
var hash hash.Hash var hash hash.Hash
switch rpc.Args[1] { switch rpc.Args[1] {
case "md5": case "md5":
@ -262,14 +262,12 @@ func rpc(w http.ResponseWriter, r *http.Request) {
_, err = io.Copy(hash, file) _, err = io.Copy(hash, file)
check(err) check(err)
checksum := hash.Sum(nil) checksum := hash.Sum(nil)
checksumHex := make([]byte, hex.EncodedLen(len(checksum))) ret = make([]byte, hex.EncodedLen(len(checksum)))
hex.Encode(checksumHex, checksum) hex.Encode(ret, checksum)
w.Write(checksumHex)
return
} }
check(err) check(err)
w.Write([]byte("ok")) w.Write(ret)
} }
func enforcePath(p string) string { func enforcePath(p string) string {

22
ui/script.js vendored
View file

@ -153,21 +153,14 @@ function rpc (call, args, cb) {
xhr.open('POST', location.origin + window.extraPath + '/rpc') xhr.open('POST', location.origin + window.extraPath + '/rpc')
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8') xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8')
xhr.send(JSON.stringify({ call, args })) xhr.send(JSON.stringify({ call, args }))
// set the callback function (cb) to true to copy the response to the clipboard xhr.onload = cb
if (cb === true) {
xhr.onload = () => {
navigator.clipboard.writeText(xhr.responseText)
};
} else {
xhr.onload = cb
}
xhr.onerror = () => flicker(sadBadge) xhr.onerror = () => flicker(sadBadge)
} }
const mkdirCall = (path, cb) => rpc('mkdirp', [prependPath(path)], cb) const mkdirCall = (path, cb) => rpc('mkdirp', [prependPath(path)], cb)
const rmCall = (path1, cb) => rpc('rm', [prependPath(path1)], cb) const rmCall = (path1, cb) => rpc('rm', [prependPath(path1)], cb)
const mvCall = (path1, path2, cb) => rpc('mv', [path1, path2], cb) const mvCall = (path1, path2, cb) => rpc('mv', [path1, path2], cb)
const sumCall = (path, type) => {rpc('sum', [prependPath(path), type], true)} const sumCall = (path, type, cb) => rpc('sum', [prependPath(path), type], cb)
// File upload // File upload
let totalDone = 0 let totalDone = 0
@ -669,8 +662,15 @@ function helpOff () {
// checksums // checksums
function getSum (type) { function getSum (type) {
sumCall(getASelected().innerText, type) upBarPc.style.display = 'block'
upBarPc.innerText = 'computing checksum...'
upBarPc.style.width = '100%'
sumsOff() sumsOff()
sumCall(getASelected().innerText, type, loaded => {
navigator.clipboard.writeText(loaded.target.responseText)
upBarPc.style.display = 'none'
flicker(okBadge)
})
} }
const isSumsMode = () => sums.style.display === 'block' const isSumsMode = () => sums.style.display === 'block'
@ -678,7 +678,7 @@ const isSumsMode = () => sums.style.display === 'block'
const sumsToggle = () => isSumsMode() ? sumsOff() : sumsOn() const sumsToggle = () => isSumsMode() ? sumsOff() : sumsOn()
function sumsOn () { function sumsOn () {
if (getASelected().innerText.endsWith('/')) { if (isFolder(getASelected())) {
alert('cannot checksum a directory') alert('cannot checksum a directory')
return return
} }