Skip to content

Commit 2722ecc

Browse files
committed
feat(www): handle compressed files
Signed-off-by: Cedric Hombourger <[email protected]>
1 parent 5bf7a48 commit 2722ecc

File tree

4 files changed

+49
-29
lines changed

4 files changed

+49
-29
lines changed

mtda-www

+11-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import uuid
2323
import zmq.asyncio
2424

2525
from mtda.client import Client
26+
from mtda.utils import Compression
2627
import mtda.constants as CONSTS
2728
from mtda.console.remote import RemoteConsole
2829
from mtda.console.screen import ScreenOutput
@@ -155,9 +156,17 @@ class StorageOpenHandler(BaseHandler):
155156
mtda = self.application.settings['mtda']
156157
result = ''
157158
if mtda is not None:
159+
compr = CONSTS.IMAGE.RAW.value
160+
file = self.get_argument('file')
161+
if file:
162+
mtda.debug(2, f'file to be uploaded: {file}')
163+
compr = Compression.from_extension(file)
164+
mtda.storage_compression(compr)
165+
158166
sid = self.get_argument('session')
159167
zmq_socket = mtda.storage_open(session=sid)
160168
self.application.settings['sockets'][sid] = zmq_socket
169+
161170
self.result_as_json({"result": result})
162171

163172

@@ -241,7 +250,8 @@ class WebOutput(ScreenOutput):
241250
def session_event(self, info):
242251
if info[0] == 'INACTIVE':
243252
sid = info[1]
244-
del self.application.settings['sockets'][sid]
253+
if sid in self.application.settings['sockets']:
254+
del self.application.settings['sockets'][sid]
245255

246256
def write(self, data):
247257
data = base64.b64encode(data).decode('utf-8')

mtda/client.py

+5-16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import zstandard as zstd
2222

2323
from mtda.main import MultiTenantDeviceAccess
24+
from mtda.utils import Compression
2425
import mtda.constants as CONSTS
2526

2627

@@ -327,20 +328,6 @@ def __init__(self, path, agent, session, blksz, callback=None):
327328
def bmap(self, path):
328329
return None
329330

330-
def compression(self):
331-
path = self._path
332-
if path.endswith(".bz2"):
333-
result = CONSTS.IMAGE.BZ2.value
334-
elif path.endswith(".gz"):
335-
result = CONSTS.IMAGE.GZ.value
336-
elif path.endswith(".zst"):
337-
result = CONSTS.IMAGE.ZST.value
338-
elif path.endswith(".xz"):
339-
result = CONSTS.IMAGE.XZ.value
340-
else:
341-
result = CONSTS.IMAGE.RAW.value
342-
return result
343-
344331
def flush(self):
345332
# Wait for background writes to complete
346333
agent = self._agent
@@ -366,7 +353,9 @@ def path(self):
366353
return self._path
367354

368355
def prepare(self, socket, output_size=None, compression=None):
369-
compr = self.compression() if compression is None else compression
356+
compr = None
357+
if compression is None:
358+
compr = Compression.from_extension(self._path)
370359
self._inputsize = self.size()
371360
self._outputsize = output_size
372361
self._socket = socket
@@ -416,7 +405,7 @@ def copy(self):
416405

417406
image = open(self._path, 'rb')
418407
comp_on_the_fly = False
419-
if self.compression() == CONSTS.IMAGE.RAW.value:
408+
if Compression.from_extension(self._path) == CONSTS.IMAGE.RAW.value:
420409
cctx = zstd.ZstdCompressor(level=1)
421410
comp_on_the_fly = True
422411
inputstream = cctx.stream_reader(image)

mtda/templates/index.html

+17-12
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,6 @@
157157
uploadWindow.focus();
158158
});
159159
});
160-
$(function() {
161-
$('#upload').bind('click', function() {
162-
$.getJSON('./storage-open', {session: localStorage.getItem('session')}, function(data) {
163-
// do nothing
164-
});
165-
return false;
166-
});
167-
});
168160
</script>
169161
<script src="./assets/xterm.js"></script>
170162
<script src="./assets/addon-fit.js"></script>
@@ -190,8 +182,24 @@
190182
const video = document.getElementById("video");
191183
const power_status = document.getElementById("power-status-icon")
192184
const storage_status = document.getElementById("storage-status-icon")
185+
193186
const dropzone = document.getElementById('dropzone');
194187
const upload = document.getElementById('upload');
188+
const CHUNK_SIZE = 512 * 1024;
189+
let selectedFile = null;
190+
let maySend = false;
191+
192+
$(function() {
193+
$('#upload').bind('click', function() {
194+
$.getJSON('./storage-open', {
195+
file: selectedFile.name,
196+
session: localStorage.getItem('session')
197+
}, function(data) {
198+
// do nothing
199+
});
200+
return false;
201+
});
202+
});
195203

196204
function console_output(data) {
197205
if (data.output != null) {
@@ -330,10 +338,6 @@
330338
}
331339
}
332340

333-
const CHUNK_SIZE = 512 * 1024;
334-
let selectedFile = null;
335-
let maySend = false;
336-
337341
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
338342
dropzone.addEventListener(eventName, (e) => e.preventDefault());
339343
});
@@ -350,6 +354,7 @@
350354
const files = event.dataTransfer.files;
351355
if (files.length) {
352356
selectedFile = files[0];
357+
console.log('will upload '+selectedFile.name);
353358
dropzone.textContent = `${selectedFile.name}`;
354359
upload.disabled = false;
355360
}

mtda/utils.py

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@
1010
# ---------------------------------------------------------------------------
1111

1212
import threading
13+
import mtda.constants as CONSTS
14+
15+
16+
class Compression:
17+
def from_extension(path):
18+
if path.endswith(".bz2"):
19+
result = CONSTS.IMAGE.BZ2.value
20+
elif path.endswith(".gz"):
21+
result = CONSTS.IMAGE.GZ.value
22+
elif path.endswith(".zst"):
23+
result = CONSTS.IMAGE.ZST.value
24+
elif path.endswith(".xz"):
25+
result = CONSTS.IMAGE.XZ.value
26+
else:
27+
result = CONSTS.IMAGE.RAW.value
28+
return result
1329

1430

1531
class RepeatTimer(threading.Timer):

0 commit comments

Comments
 (0)