/
rom_upload.js
124 lines (103 loc) · 3.58 KB
/
rom_upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"use strict";
let db;
let dbVersion = 1;
let dbReady = false;
function initDb(func) {
let request = indexedDB.open('FileStorage', dbVersion);
request.onerror = function(e) {
console.error('Unable to open database.');
}
request.onsuccess = function(e) {
db = e.target.result;
console.log('db opened');
func();
}
request.onupgradeneeded = function(e) {
let db = e.target.result;
db.createObjectStore('files', {keyPath:'id', autoIncrement: false});
dbReady = true;
}
}
const bfs = new basic_fs();
async function uploadFile(fileId) {
let rk = romkind_sub(global_player.system_kind)
let path = '/' + rk + '/roms/';
let el = document.getElementById('fileUpload');
await do_upload_file('test', el, path)
//await reload_roms(rk);
}
async function do_upload_file(fileId, el, path) {
let fileIn = el;
if (fileIn.files && fileIn.files[0]) {
let file_name = fileIn.files[0].name;
let reader = new FileReader();
reader.onload = async function (e) {
let bits = e.target.result;
let ob = {
id: fileId,
type: fileIn.files[0].type,
name: fileIn.files[0].name,
data: bits
};
path = path + file_name;
if (await bfs.file_exists(path)) {
alert('File already exists!');
return;
}
await bfs.write_file(path, e.target.result);
};
await reader.readAsBinaryString(fileIn.files[0])
}
}
function dumb_convert(instr) {
var abuf = new ArrayBuffer(instr.length);
var buf = new Uint8Array(abuf);
for (var i = 0, strLen = instr.length; i < strLen; i++) {
buf[i] = instr.charCodeAt(i);
}
return buf;
}
function getFromDb(fileId, func) {
var trans = db.transaction(['files'], 'readonly');
var dlReq = trans.objectStore('files').get(fileId);
let ready = false;
let errored = false;
dlReq.onerror = function(e) {
console.log('error reading data');
console.error(e);
};
dlReq.onsuccess = function(e) {
console.log('data read');
console.log("loaded bytes", dlReq.result.data.length);
//console.log('hi', dlReq.result.data)
/*var myvar = new TextEncoder("utf-8").encode(dlReq.result.data);
console.log(myvar.byteLength, typeof(myvar))
var yourvar = new Uint8Array(myvar);
console.log(typeof(yourvar), yourvar.byteLength, yourvar);
myvar = dumb_convert(dlReq.result.data);
console.log("done dumb:", myvar.byteLength);
var my = myvar[0xFFD5 + 512];*/
//console.log(my.toString(16));
func(dumb_convert(dlReq.result.data));
};
}
function downloadFile(fileId) {
console.log('downloading');
var trans = db.transaction(['files'], 'readonly');
var dlReq = trans.objectStore('files').get(fileId);
dlReq.onerror = function(e) {
console.log('error reading data');
console.error(e);
};
dlReq.onsuccess = function(e) {
console.log('data read');
var element = document.createElement('a');
//element.setAttribute('href', 'data:' + dlReq.result.type + ';charset=utf-8,' + encodeURIComponent(dlReq.result.data));
/*element.setAttribute('href', 'data:' + dlReq.result.type + ';base64,' + dlReq.result.data);
element.setAttribute('download', dlReq.result.name);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);*/
};
}