Skip to content

Commit

Permalink
Expose real device sizes to JS for validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Sep 20, 2021
1 parent f6d6d9b commit 46f03d4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
28 changes: 25 additions & 3 deletions devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"os/exec"
"strconv"
"strings"
)

Expand All @@ -10,11 +11,12 @@ type Device struct {
Name string
Model string
Size string
Bytes int
}

// GetDevices returns the list of USB devices available to read/write from.
func GetDevices() ([]Device, error) {
res, err := exec.Command("lsblk", "-o", "KNAME,TYPE,SIZE,MODEL").Output()
res, err := exec.Command("lsblk", "-b", "-o", "KNAME,TYPE,SIZE,MODEL").Output()
if err != nil {
return nil, err
}
Expand All @@ -38,9 +40,11 @@ func GetDevices() ([]Device, error) {
if disk[1] == "disk" && !strings.HasPrefix(disk[0], "zram") &&
!strings.HasPrefix(rootPart, "/dev/"+disk[0]) &&
!strings.HasPrefix(homePart, "/dev/"+disk[0]) {
bytes, _ := strconv.Atoi(disk[2])
device := Device{
Name: "/dev/" + disk[0],
Size: disk[2],
Name: "/dev/" + disk[0],
Size: bytesToString(bytes),
Bytes: bytes,
}

if len(disk) >= 4 && disk[3] != "" {
Expand All @@ -53,3 +57,21 @@ func GetDevices() ([]Device, error) {

return disks, nil
}

func bytesToString(bytes int) string {
kb := float64(bytes) / 1000
mb := kb / 1000
gb := mb / 1000
tb := gb / 1000
if tb >= 1 {
return strconv.FormatFloat(tb, 'f', 1, 64) + "TB"
} else if gb >= 1 {
return strconv.FormatFloat(gb, 'f', 1, 64) + "GB"
} else if mb >= 1 {
return strconv.FormatFloat(mb, 'f', 1, 64) + "MB"
} else if kb >= 1 {
return strconv.FormatFloat(kb, 'f', 1, 64) + "KB"
} else {
return strconv.Itoa(bytes) + "B"
}
}
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/webview/webview"
)

// TODO: Design UI and check disk vs ISO sizes. Validate written image?
// TODO: Design UI (with live warnings/errors). Validate written image?

const html = `
<html lang="en">
Expand Down Expand Up @@ -73,10 +73,11 @@ func main() {
}
jsonifiedDevices := make([]string, len(devices))
for index, device := range devices {
base := strconv.Itoa(device.Bytes) + " " + device.Name
if device.Model == "" {
jsonifiedDevices[index] = ParseToJsString(device.Name + " (" + device.Size + ")")
jsonifiedDevices[index] = ParseToJsString(base + " (" + device.Size + ")")
} else {
jsonifiedDevices[index] = ParseToJsString(device.Name + " (" + device.Model + ", " + device.Size + ")")
jsonifiedDevices[index] = ParseToJsString(base + " (" + device.Model + ", " + device.Size + ")")
}
}
// Call setDevicesReact.
Expand Down
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import ReactDOM from 'react-dom'
import Dialog from './dialog'

const floor = num => Math.floor(num * 100) / 100
// const varToString = varObj => Object.keys(varObj)[0]; const s = (setObj, value) => {
// const name = varToString(setObj); setObj[name](value); window[name + 'Go'](value)}

const App = () => {
const [file, setFile] = useState('')
Expand Down Expand Up @@ -37,6 +39,9 @@ const App = () => {
setProgress(0)
if (selectedDevice === 'N/A') return setDialog('Error: Select a device to flash the ISO to!')
if (!file) return setDialog('Error: Select an ISO to flash to a device!')
if (BigInt(fileSize) > BigInt(selectedDevice.split(' ')[0])) {
return setDialog('Error: The ISO file is too big to fit on the selected drive!')
}
if (!confirm) return setConfirm(true)
setConfirm(false)
window.flash(file, selectedDevice.split(' ')[0])
Expand All @@ -63,7 +68,9 @@ const App = () => {
value={selectedDevice}
onChange={e => setSelectedDevice(e.target.value)}
>
{devices.map(device => <option key={device} value={device}>{device}</option>)}
{devices.map(device => (
<option key={device} value={device}>{device.substr(device.indexOf(' ') + 1)}</option>
))}
</select>
<button onClick={() => window.refreshDevices()} css={css`min-width: 69px;`}>
Refresh
Expand Down

0 comments on commit 46f03d4

Please sign in to comment.