forked from microsoft/dicom-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
151 lines (131 loc) · 5.18 KB
/
main.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
// -------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------
const {
app,
BrowserWindow,
dialog,
ipcMain
} = require("electron");
const path = require("path");
const fs = require("fs");
const FormData = require('form-data')
const https = require('https')
const fetch = require('node-fetch')
// based on answer from https://stackoverflow.com/questions/57807459/how-to-use-preload-js-properly-in-electron
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;
async function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: false, // is default value after Electron v5
contextIsolation: true, // protect against prototype pollution
enableRemoteModule: true, // turn off remote
preload: path.join(__dirname, "preload.js") // use a preload script
}
});
// Load app
win.loadFile(path.join(__dirname, "index.html"));
// Used to allow self signed certificates
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
// Set the maximum content length size in bytes and megabytes
const maxSizeMegabytes = 2048;
const maxSizeBytes = maxSizeMegabytes * 1024 * 1024;
ipcMain.on("postFile", (event, args) => {
let form = new FormData();
let fullContentLength = 0;
for (let file of args.files) {
const { size } = fs.statSync(file);
// Check to see if this particular file is too large
if (size > maxSizeBytes) {
win.webContents.send("errorEncountered", "The file '" + file + "' exceeds the maximum content size of " + maxSizeMegabytes + " MB.");
return;
}
fullContentLength += size;
form.append('file', fs.createReadStream(file), {
contentType: "application/dicom"
});
}
// See if the sum of all the files is too large
if (fullContentLength > maxSizeBytes) {
win.webContents.send("errorEncountered", "The total size of the request exceeds the maximum content size of " + maxSizeMegabytes + " MB.");
return;
}
let authorizationHeader = ''
if (args.bearerToken !== '') {
authorizationHeader = 'Bearer ' + args.bearerToken
}
fetch(
args.url,
{
method: `POST`,
headers: {
'Content-Type': 'multipart/related; ' + 'boundary=' + form._boundary,
'Accept': 'application/dicom+json',
'Authorization': authorizationHeader
},
maxContentLength: maxSizeBytes,
maxBodyLength: maxSizeBytes,
body: form,
agent: httpsAgent
})
.then(function(response) {
if (response.ok) {
win.webContents.send("success", response.data);
} else {
win.webContents.send("httpErrorEncountered", response.status);
}
})
.catch(function(error) {
if (error.response === undefined) {
win.webContents.send("httpErrorEncountered", error.code);
} else {
win.webContents.send("httpErrorEncountered", error.response.status);
}
})
});
ipcMain.on("getChangeFeed", (event, args) => {
let form = new FormData();
let authorizationHeader = ''
if (args.bearerToken !== '') {
authorizationHeader = 'Bearer ' + args.bearerToken
}
fetch(
args.url,
{
headers: {
'Accept': 'application/json',
'Authorization': authorizationHeader
},
agent: httpsAgent
})
.then(function(res) {
return res.json();
}).then(function(json) {
win.webContents.send("changeFeedRetrieved", json);
})
.catch(function(error) {
if (error.response === undefined) {
win.webContents.send("httpErrorEncountered", error.code);
} else {
win.webContents.send("httpErrorEncountered", error.response.status);
}
})
});
ipcMain.on("selectFile", (event, args) => {
dialog.showOpenDialog({
filters: [
{ name: 'DICOM Files', extensions: ['dcm'] },
],
properties: ['openFile', 'multiSelections']
}).then(result => {
win.webContents.send("fileSelected", result.filePaths);
});
})
}
app.on("ready", createWindow);