Skip to content

Commit c42ec10

Browse files
authored
Merge pull request #35 from 45Drives/dev-josh
Fix mangling large files during upload
2 parents d111f0f + 17e4a3c commit c42ec10

File tree

7 files changed

+75
-85
lines changed

7 files changed

+75
-85
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
## Cockpit Navigator 0.5.5-2
1+
## Cockpit Navigator 0.5.6-1
22

3-
* Fix sed command for packaging EL7.
3+
* Fix mangling of large files during upload.

manifest.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"name": "cockpit-navigator",
44
"title": "Cockpit Navigator",
55
"prerelease": false,
6-
"version": "0.5.5",
7-
"buildVersion": "2",
6+
"version": "0.5.6",
7+
"buildVersion": "1",
88
"author": "Josh Boudreau <jboudreau@45drives.com>",
99
"url": "https://github.com/45Drives/cockpit-navigator",
1010
"category": "utils",
@@ -54,8 +54,8 @@
5454
],
5555
"changelog": {
5656
"urgency": "medium",
57-
"version": "0.5.5",
58-
"buildVersion": "2",
57+
"version": "0.5.6",
58+
"buildVersion": "1",
5959
"ignore": [],
6060
"date": null,
6161
"packager": "Josh Boudreau <jboudreau@45drives.com>",

navigator/components/FileUpload.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ export class FileUpload {
148148
this.chunk_index++;
149149
this.progress.value = this.chunk_index;
150150
if (this.chunk_index < this.num_chunks)
151-
this.reader.readAsArrayBuffer(this.chunks[this.chunk_index]);
151+
this.reader.readAsDataURL(this.chunks[this.chunk_index]);
152152
else {
153153
this.done();
154154
}
155155
};
156156
try {
157-
this.reader.readAsArrayBuffer(this.chunks[0]);
157+
this.reader.readAsDataURL(this.chunks[0]);
158158
} catch {
159159
this.reader.onload = () => {};
160160
if (this.using_webkit) {
@@ -167,27 +167,12 @@ export class FileUpload {
167167
this.update_rates_interval = setInterval(this.display_xfr_rate.bind(this), 1000);
168168
}
169169

170-
/**
171-
*
172-
* @param {ArrayBuffer} buffer
173-
* @returns
174-
*/
175-
arrayBufferToBase64(buffer) {
176-
let binary = '';
177-
let bytes = new Uint8Array(buffer);
178-
let len = bytes.byteLength;
179-
for (let i = 0; i < len; i++) {
180-
binary += String.fromCharCode(bytes[i]);
181-
}
182-
return window.btoa(binary);
183-
}
184-
185170
/**
186171
*
187172
* @param {Event} evt
188173
*/
189174
write_to_file(evt) {
190-
var chunk_b64 = this.arrayBufferToBase64(evt.target.result);
175+
var chunk_b64 = evt.target.result.replace(/^data:[^\/]+\/[^;]+;base64,/, "");
191176
const seek = this.chunk_index * this.chunk_size;
192177
var obj = {
193178
seek: seek,

navigator/scripts/write-chunks.py3

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
#!/usr/bin/env python3
22

33
"""
4-
Cockpit Navigator - A File System Browser for Cockpit.
5-
Copyright (C) 2021 Josh Boudreau <jboudreau@45drives.com>
4+
Cockpit Navigator - A File System Browser for Cockpit.
5+
Copyright (C) 2021 Josh Boudreau <jboudreau@45drives.com>
66
7-
This file is part of Cockpit Navigator.
8-
Cockpit Navigator is free software: you can redistribute it and/or modify
9-
it under the terms of the GNU General Public License as published by
10-
the Free Software Foundation, either version 3 of the License, or
11-
(at your option) any later version.
12-
Cockpit Navigator is distributed in the hope that it will be useful,
13-
but WITHOUT ANY WARRANTY; without even the implied warranty of
14-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15-
GNU General Public License for more details.
16-
You should have received a copy of the GNU General Public License
17-
along with Cockpit Navigator. If not, see <https://www.gnu.org/licenses/>.
7+
This file is part of Cockpit Navigator.
8+
Cockpit Navigator is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation, either version 3 of the License, or
11+
(at your option) any later version.
12+
Cockpit Navigator is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
You should have received a copy of the GNU General Public License
17+
along with Cockpit Navigator. If not, see <https://www.gnu.org/licenses/>.
1818
"""
1919

2020
"""
2121
Synopsis: `echo <newline delimited JSON objects> | write-chunks.py3`
2222
JSON objects are of form:
2323
obj = {
24-
seek: <byte offset>
25-
chunk: <base64 encoded data chunk>
24+
seek: <byte offset>
25+
chunk: <base64 encoded data chunk>
2626
}
2727
"""
2828

@@ -31,53 +31,48 @@ import os
3131
import sys
3232
import json
3333

34-
def write_chunk(chunk, file):
35-
if not file:
36-
path = sys.argv[1]
37-
parent_path = os.path.dirname(path)
38-
try:
39-
if not os.path.exists(parent_path):
40-
os.makedirs(parent_path, exist_ok=True)
41-
elif os.path.isfile(parent_path):
42-
print(parent_path + ": exists and is not a directory.")
43-
sys.exit(1)
44-
file = open(path, "wb")
45-
except Exception as e:
46-
print(e)
47-
sys.exit(1)
48-
seek = chunk["seek"]
49-
data = base64.b64decode(chunk["chunk"])
50-
try:
51-
file.seek(seek)
52-
file.write(data)
53-
except Exception as e:
54-
print(e)
55-
sys.exit(1)
34+
def write_chunk(chunk, fd):
35+
seek = chunk["seek"]
36+
data = base64.b64decode(chunk["chunk"])
37+
os.lseek(fd, seek, os.SEEK_SET)
38+
os.write(fd, data)
39+
40+
def create_path(path):
41+
try:
42+
if not os.path.exists(path):
43+
os.makedirs(path, exist_ok=True)
44+
elif os.path.isfile(path):
45+
print(path + ": exists and is not a directory.")
46+
sys.exit(1)
47+
except Exception as e:
48+
print(e)
49+
sys.exit(1)
5650

5751
def main():
58-
file = None
59-
if len(sys.argv) != 2:
60-
print("Invalid number of arguments.")
61-
sys.exit(1)
62-
while True:
63-
try:
64-
json_in = input()
65-
except EOFError:
66-
break
67-
json_list = json_in.split("\n") # need to split in case writes happen faster than reads
68-
for json_obj in json_list:
69-
try:
70-
obj_in = json.loads(json_obj)
71-
except Exception as e:
72-
print(e)
73-
log = open("/var/log/navigator.log", "w")
74-
log.write(json_in)
75-
log.close()
76-
sys.exit(1)
77-
write_chunk(obj_in, file)
78-
if file:
79-
file.close()
80-
sys.exit(0)
52+
if len(sys.argv) != 2:
53+
print("Invalid number of arguments.")
54+
sys.exit(1)
55+
fd = None
56+
path = sys.argv[1]
57+
parent_path = os.path.dirname(path)
58+
create_path(parent_path)
59+
try:
60+
fd = os.open(sys.argv[1], os.O_WRONLY | os.O_TRUNC | os.O_CREAT)
61+
while True:
62+
try:
63+
json_in = input()
64+
except EOFError:
65+
break
66+
json_list = json_in.split("\n") # need to split in case writes happen faster than reads
67+
for json_obj in json_list:
68+
obj_in = json.loads(json_obj)
69+
write_chunk(obj_in, fd)
70+
except Exception as e:
71+
print(e)
72+
os.close(fd)
73+
sys.exit(1)
74+
os.close(fd)
75+
sys.exit(0)
8176

8277
if __name__ == "__main__":
83-
main()
78+
main()

packaging/el7/main.spec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ rm -rf %{buildroot}
3232
/usr/share/cockpit/navigator/*
3333

3434
%changelog
35+
* Fri Nov 12 2021 Joshua Boudreau <jboudreau@45drives.com> 0.5.6-1
36+
- Fix mangling of large files during upload.
3537
* Mon Oct 04 2021 Joshua Boudreau <jboudreau@45drives.com> 0.5.5-2
3638
- Fix sed command for packaging EL7.
3739
* Mon Oct 04 2021 Joshua Boudreau <jboudreau@45drives.com> 0.5.5-1

packaging/el8/main.spec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ rm -rf %{buildroot}
3232
/usr/share/cockpit/navigator/*
3333

3434
%changelog
35+
* Fri Nov 12 2021 Joshua Boudreau <jboudreau@45drives.com> 0.5.6-1
36+
- Fix mangling of large files during upload.
3537
* Mon Oct 04 2021 Joshua Boudreau <jboudreau@45drives.com> 0.5.5-2
3638
- Fix sed command for packaging EL7.
3739
* Mon Oct 04 2021 Joshua Boudreau <jboudreau@45drives.com> 0.5.5-1

packaging/focal/changelog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
cockpit-navigator (0.5.6-1focal) focal; urgency=medium
2+
3+
* Fix mangling of large files during upload.
4+
5+
-- Joshua Boudreau <jboudreau@45drives.com> Fri, 12 Nov 2021 08:06:44 -0400
6+
17
cockpit-navigator (0.5.5-2focal) focal; urgency=medium
28

39
* Fix sed command for packaging EL7.

0 commit comments

Comments
 (0)