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) {
var err error
var rpc rpcCall
defer exitPath(w, "rpc", rpc)
defer exitPath(w, "rpc", &rpc)
bodyBytes, err := io.ReadAll(r.Body)
check(err)
json.Unmarshal(bodyBytes, &rpc)
ret := []byte("ok")
switch rpc.Call {
case "mkdirp":
@ -245,9 +246,8 @@ func rpc(w http.ResponseWriter, r *http.Request) {
case "rm":
err = os.RemoveAll(enforcePath(rpc.Args[0]))
case "sum":
var file *os.File
enforcedPath := enforcePath(rpc.Args[0])
file, err = os.Open(enforcedPath)
file, err := os.Open(enforcePath(rpc.Args[0]))
check(err)
var hash hash.Hash
switch rpc.Args[1] {
case "md5":
@ -262,14 +262,12 @@ func rpc(w http.ResponseWriter, r *http.Request) {
_, err = io.Copy(hash, file)
check(err)
checksum := hash.Sum(nil)
checksumHex := make([]byte, hex.EncodedLen(len(checksum)))
hex.Encode(checksumHex, checksum)
w.Write(checksumHex)
return
ret = make([]byte, hex.EncodedLen(len(checksum)))
hex.Encode(ret, checksum)
}
check(err)
w.Write([]byte("ok"))
w.Write(ret)
}
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.setRequestHeader('Content-Type', 'application/json;charset=UTF-8')
xhr.send(JSON.stringify({ call, args }))
// set the callback function (cb) to true to copy the response to the clipboard
if (cb === true) {
xhr.onload = () => {
navigator.clipboard.writeText(xhr.responseText)
};
} else {
xhr.onload = cb
}
xhr.onload = cb
xhr.onerror = () => flicker(sadBadge)
}
const mkdirCall = (path, cb) => rpc('mkdirp', [prependPath(path)], cb)
const rmCall = (path1, cb) => rpc('rm', [prependPath(path1)], 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
let totalDone = 0
@ -669,8 +662,15 @@ function helpOff () {
// checksums
function getSum (type) {
sumCall(getASelected().innerText, type)
upBarPc.style.display = 'block'
upBarPc.innerText = 'computing checksum...'
upBarPc.style.width = '100%'
sumsOff()
sumCall(getASelected().innerText, type, loaded => {
navigator.clipboard.writeText(loaded.target.responseText)
upBarPc.style.display = 'none'
flicker(okBadge)
})
}
const isSumsMode = () => sums.style.display === 'block'
@ -678,7 +678,7 @@ const isSumsMode = () => sums.style.display === 'block'
const sumsToggle = () => isSumsMode() ? sumsOff() : sumsOn()
function sumsOn () {
if (getASelected().innerText.endsWith('/')) {
if (isFolder(getASelected())) {
alert('cannot checksum a directory')
return
}