Skip to content

Commit

Permalink
Use named pipe to decompress and save. Needs to be done manually righ…
Browse files Browse the repository at this point in the history
…t now.
  • Loading branch information
eliasbakken committed Jun 5, 2024
1 parent 366bd02 commit 3f48023
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 38 deletions.
6 changes: 0 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,9 @@ dev-clean:
dev-client:
cd client; npm run serve

dev-board:
cd board; npm run serve

build:
cd client; npm run build

build-board:
cd board; npm run build

upload:
scp -r client/dist [email protected]:/var/www/html/reflash

Expand Down
2 changes: 1 addition & 1 deletion client/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ export default {
reader.readAsDataURL(slice);
} else {
offset = filesize;
self.apiCall("upload_finish");
self.apiCall("upload_magic_finish");
}
} else {
self.apiCall("upload_cancel");
Expand Down
61 changes: 30 additions & 31 deletions reflash/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"github.com/grafana/tail"
"github.com/pelletier/go-toml/v2"
"github.com/ulikunitz/xz"
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -79,6 +78,7 @@ type State struct {
BytesTotal int `json:"bytes_total"`
Error string `json:"error"`
IPs []string `json:"ips"`
File *os.File
}

type Chunk struct {
Expand Down Expand Up @@ -113,7 +113,6 @@ var oldState *State
var oldRotation int

var static_dir string
var version_file string
var images_folder string
var options_file string
var log_file string
Expand Down Expand Up @@ -194,6 +193,7 @@ func ServerInit() {
http.HandleFunc("/api/cancel_magic", cancelMagic)
http.HandleFunc("/api/upload_magic_start", uploadMagicStart)
http.HandleFunc("/api/upload_magic_chunk", uploadMagicChunk)
http.HandleFunc("/api/upload_magic_finish", uploadMagicFinish)
http.HandleFunc("/api/get_progress", getProgress)
http.HandleFunc("/api/check_file_integrity", checkFileIntegrity)
http.HandleFunc("/api/run_install_finished_commands", runInstallFinishedCommands)
Expand Down Expand Up @@ -361,57 +361,56 @@ func uploadMagicStart(w http.ResponseWriter, r *http.Request) {
logInfo("Starting magic upload at " + timeStart.Format("15:04:05"))
logInfo("Filename: " + state.Filename)

path := "/tmp/decompressed.img"
os.Create(path)
path := "/tmp/mypipe"
var err error
state.File, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
sendResponse(w, nil)
}

func uploadMagicChunk(w http.ResponseWriter, r *http.Request) {
var chunk *Chunk = &Chunk{}
reqBody, _ := io.ReadAll(r.Body)
json.Unmarshal(reqBody, &chunk)

decoded, err := base64.StdEncoding.DecodeString(chunk.Encoded[37:])

path := "/tmp/decompressed.img"

if state.State == CANCELLED {
response := map[string]bool{"success": false}
json.NewEncoder(w).Encode(response)
return
}

reader, err := xz.NewReader(bytes.NewReader(decoded))
if err != nil {
log.Fatal(err)
}
var chunk *Chunk = &Chunk{}
reqBody, _ := io.ReadAll(r.Body)
json.Unmarshal(reqBody, &chunk)

var decompressedData bytes.Buffer
if _, err := io.Copy(&decompressedData, reader); err != nil {
log.Fatal("Failed to copy")
log.Fatal(err)
decoded, err := base64.StdEncoding.DecodeString(chunk.Encoded[37:])
if err != nil {
http.Error(w, "Failed to decode base64", http.StatusBadRequest)
return
}

outFile, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
_, err = state.File.Write(decoded)
if err != nil {
log.Fatal("Failed to open file")
log.Fatal(err)
http.Error(w, "Failed to write decompressed data to file", http.StatusInternalServerError)
return
}

if _, err := outFile.Write(decompressedData.Bytes()); err != nil {
log.Fatal("Failed write")
log.Fatal(err)
}
if err := outFile.Close(); err != nil {
log.Fatal(err)
}
state.BytesNow += len(decoded)
state.Progress = float64(state.BytesNow) * 100 / float64(state.BytesTotal)

response := map[string]bool{"success": true}
json.NewEncoder(w).Encode(response)
}

func uploadMagicFinish(w http.ResponseWriter, r *http.Request) {
if err := state.File.Close(); err != nil {
log.Fatal(err)
}
duration := time.Since(timeStart)
logInfo(fmt.Sprintf("Upload magic finished in %d minutes and %d seconds", int(duration.Minutes()), int(duration.Seconds())%60))
state.State = FINISHED
if saveOptionsWhenIdle {
saveOptions()
}
}

func uploadChunk(w http.ResponseWriter, r *http.Request) {
var chunk *Chunk = &Chunk{}
reqBody, _ := io.ReadAll(r.Body)
Expand Down

0 comments on commit 3f48023

Please sign in to comment.