-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.js
219 lines (178 loc) · 5.16 KB
/
file.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const Gtk = imports.gi.Gtk;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const secret = imports.lib.secret;
const base64 = imports.lib.base64;
const Data = imports.object.Data;
const appData = new Data.Data();
/* eslint-disable no-unused-vars */
function decompress(data) {
const td = new TextDecoder('utf-8');
let decompressed = Gio.MemoryOutputStream.new_resizable();
let output = new Gio.ConverterOutputStream({
base_stream: decompressed,
converter: new Gio.ZlibDecompressor({
format: Gio.ZlibCompressorFormat.GZIP,
}),
});
output.splice(Gio.MemoryInputStream.new_from_bytes(data),
Gio.OutputStreamSpliceFlags.CLOSE_SOURCE,
null);
output.flush(null);
output.close(null);
return td.decode(decompressed.steal_as_bytes().get_data());
}
function compress(data) {
const te = new TextEncoder('utf-8');
let compressed = Gio.MemoryOutputStream.new_resizable();
let output = new Gio.ConverterOutputStream({
base_stream: compressed,
converter: new Gio.ZlibCompressor({
format: Gio.ZlibCompressorFormat.GZIP,
}),
});
output.splice(Gio.MemoryInputStream.new_from_bytes(te.encode(data)),
Gio.OutputStreamSpliceFlags.CLOSE_SOURCE,
null);
output.flush(null);
output.close(null);
return compressed.steal_as_bytes().get_data();
}
function fileSave(props, ret) {
// const parent = props.parent ? props.parent : null;
const title = props.title ? props.title : 'Save a file';
const data = props.data;
const filename = props.filename;
const foldername = props.foldername;
const saver = new Gtk.FileDialog({ title });
if (filename) {
saver.set_initial_name(filename);
}
if (foldername) {
saver.set_initial_folder(Gio.File.new_for_path(foldername));
}
saver.save(null, null, async (o, r) => {
try {
const dest = await o.save_finish(r);
dest.replace_contents(data, null, false,
Gio.FileCreateFlags.REPLACE_DESTINATION, null);
ret(dest.get_basename());
} catch (e) {
log(e)
throw e;
}
});
}
function fileOpen(props, ret) {
// const parent = props.parent ? props.parent : null;
const title = props.title ? props.title : 'Select a file';
const foldername = props.foldername ? props.foldername : '/home';
const decoder = new TextDecoder('utf-8');
const opener = new Gtk.FileDialog({ title });
opener.set_initial_folder(Gio.File.new_for_path(foldername));
try {
opener.open(null, null, (o, r) => {
ret(o.open_finish(r));
});
} catch (error) {
log(error);
throw(error);
}
}
function fopen (path) {
return new Promise((resolve, reject) => {
const file = Gio.File.new_for_path(path);
// asynchronous file loading...
file.load_contents_async(null, (file, res) => {
try {
// read the file into a variable...
const contents = file.load_contents_finish(res)[1];
const decoder = new TextDecoder('utf-8');
const dataString = decoder.decode(contents);
resolve(dataString);
} catch (e) {
logError(e, 'File error');
reject(e);
}
});
});
}
function save (path, data) {
const dataStr = data;
GLib.file_set_contents(path, dataStr);
}
function rollConn (conn, savePW) {
if (savePW) {
conn.PASS = secret.connPasswordGet(conn.ID);
}
return conn;
}
function rollUp () {
// secret.passwordSet(appData.get('PASS'));
const roll = {
// FROM: appData.get('FROM'),
// USER: appData.get('USER'),
// HOST: appData.get('HOST'),
CONN: appData.get('CONN'),
SUBJECT: appData.get('SUBJECT'),
HTML: appData.get('HTML'),
TEXT: appData.get('TEXT'),
TO: appData.get('TO'),
CSVA: appData.get('CSVA'),
VARS: appData.get('VARS'),
ATTS: appData.get('ATTACHMENTS'),
// DELAY: appData.get('DELAY'),
FILEID: appData.get('FILEID'),
SENT: appData.get('SENT'),
};
return JSON.stringify(roll, null, '\t');
}
function verify (data) {
const required = [
'CONN', 'SUBJECT',
'FILEID', 'HTML', 'TEXT', 'TO', 'CSVA', 'VARS',
];
let valid = true;
required.forEach((key) => {
// eslint-disable-next-line no-prototype-builtins
if (!data.hasOwnProperty(key)) {
valid = false;
}
});
return valid;
}
function unRoll (str) {
const data = JSON.parse(str);
// console.log(JSON.parse(data));
if (!verify(data)) {
const bfe = new Error('Bad file');
throw bfe;
}
appData.set('CONN', data.CONN);
appData.set('SUBJECT', data.SUBJECT);
appData.set('HTML', data.HTML);
appData.set('TEXT', data.TEXT);
appData.set('TO', data.TO);
appData.set('CSVA', data.CSVA);
appData.set('VARS', data.VARS);
appData.set('FILEID', data.FILEID);
appData.set('SENT', data.SENT);
if (data.ATTS) {
appData.set('ATTACHMENTS', data.ATTS);
}
}
async function open (path) {
const contents = await fopen(path);
return JSON.parse(contents);
}
function nameFromPath (path) {
return path.split('/')[-1];
}
function csvFromArray (rows = []) {
let work = [];
rows.forEach((row) => {
work.push(row.map((cell) => `"${cell.replace('"', '\"')}"`).join(','));
});
const res = work.join('\n');
return res;
}