/
Download.java
296 lines (261 loc) · 9.17 KB
/
Download.java
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package mediaPlayer;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
public class Download {
private JFrame parent;
private String outputFile;
private JDialog downloadMain = new JDialog(parent, "Download");
private JPanel main = new JPanel();
private FlowLayout flow = new FlowLayout();
private JPanel urlPanel = new JPanel(new FlowLayout());
private JLabel urlLabel = new JLabel("URL: ");
private JTextField urlField = new JTextField();
private JPanel outputPanel = new JPanel(new FlowLayout());
private JLabel outputLabel = new JLabel("Save As: ");
private JTextField outputField = new JTextField();
private JButton outputSelectButton = new JButton("Browse");
private JProgressBar progressBar = new JProgressBar();
private JPanel bottomPanel = new JPanel(new FlowLayout());
private JButton downloadButton = new JButton("Download");
private JPanel openSourcePanel = new JPanel(flow);
private JLabel openSourceLabel = new JLabel("Open Source?");
private JCheckBox openSourceCheck = new JCheckBox();
public Download(JFrame parent){
this.parent = parent;
//Sets the position of the new window
if (parent != null) {
Dimension parentSize = parent.getSize();
Point p = parent.getLocation();
downloadMain.setLocation(p.x + parentSize.width / 4, p.y
+ parentSize.height / 4);
}
flow.setVgap(15);
main.setLayout(new BoxLayout(main,BoxLayout.PAGE_AXIS));
main.add(urlPanel);
urlField.setColumns(30);
urlPanel.add(urlLabel);
urlPanel.add(urlField);
urlPanel.add(openSourcePanel);
urlPanel.add(openSourceLabel);
urlPanel.add(openSourceCheck);
main.add(outputPanel);
outputPanel.add(outputLabel);
outputField.setColumns(30);
outputPanel.add(outputField);
outputPanel.add(outputSelectButton);
main.add(bottomPanel);
bottomPanel.add(progressBar);
bottomPanel.add(downloadButton);
downloadMain.getContentPane().add(main);
downloadMain.pack();
downloadMain.setVisible(true);
// Allows user to specify location and name of the output file
outputSelectButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
try {
JFileChooser fileSaver = new JFileChooser();
URL downloadURL;
if(urlField.getText()!=""){
downloadURL = new URL(urlField.getText());
fileSaver.setSelectedFile(new File(downloadURL.getFile()));
}
fileSaver.showDialog(null,"Save");
outputFile = fileSaver.getSelectedFile().toString();
outputField.setText(outputFile);
} catch (MalformedURLException e) {
JFileChooser fileSaver = new JFileChooser();
fileSaver.showDialog(null,"Save");
outputFile = fileSaver.getSelectedFile().toString();
outputField.setText(outputFile);
}
}
});
// download button, activates the DownloadBackground process
downloadButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
if(urlField!=null && outputFile!=null && openSourceCheck.isSelected()){
DownloadBackground download = new DownloadBackground(urlField.getText(),outputFile);
download.execute();
} else if(!openSourceCheck.isSelected()){
JOptionPane.showMessageDialog(null,"File is not open source, I refuse to download!");
} else{
JOptionPane.showMessageDialog(null,"Please ensure both URL and output file are specified.");
}
}
});
}
public class DownloadBackground extends SwingWorker<Integer, Integer> {
private String url;
private String outputFile;
private int status;
public DownloadBackground(String url,String outputFile) {
this.url = url;
this.outputFile = outputFile;
}
@Override
protected Integer doInBackground() throws Exception {
String chkFileExistsCmd = "test -e " + outputFile;
ProcessBuilder checkFileBuilder = new ProcessBuilder("bash", "-c",
chkFileExistsCmd);
checkFileBuilder.redirectErrorStream(true);
Process checkFileProcess = checkFileBuilder.start();
if (!isCancelled()) {
status = checkFileProcess.waitFor();
}
if (checkFileProcess.exitValue() == 0) {
// if file exists -alert user,ask for options
Object[] options = { "Override File", "Resume Download",
"Cancel" };
int a = JOptionPane
.showOptionDialog(
null,
"File exists. Would you like to override, resume, or cancel your download?",
"File Exists!",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options,
options[2]);
if (a == JOptionPane.YES_OPTION) { // Override option
String ovrCmd = "wget " + " --progress=dot " + url + " -O "
+ outputFile;
ProcessBuilder ovrBuilder = new ProcessBuilder("bash",
"-c", ovrCmd);
ovrBuilder.redirectErrorStream(true);
Process ovrProcess = ovrBuilder.start();
progressBar.setIndeterminate(true);
BufferedReader stdoutOverride = new BufferedReader(
new InputStreamReader(ovrProcess.getInputStream()));
String line;
final AtomicInteger percent = new AtomicInteger();
while ((line = stdoutOverride.readLine()) != null
&& !isCancelled()) {
if (line.contains("%")) {
percent.incrementAndGet();
publish(percent.get());
}
}
if (!isCancelled()) { //while not cancelled, keep the process going
status = ovrProcess.waitFor();
}
//if exit value isn't 0, show error.
if (ovrProcess.exitValue() != 0) {
JOptionPane
.showMessageDialog(null,
"Error! Check your internet connection and that your URL is correct!");
this.cancel(true);
}
ovrProcess.getInputStream().close();
ovrProcess.getOutputStream().close();
ovrProcess.getErrorStream().close();
ovrProcess.destroy();
} else if (a == JOptionPane.NO_OPTION) {
// resume option
String resCmd = "wget " + "--progress=dot -c " + url
+ " -O " + outputFile;
ProcessBuilder resBuilder = new ProcessBuilder("bash",
"-c", resCmd);
resBuilder.redirectErrorStream(true);
Process resProcess = resBuilder.start();
progressBar.setIndeterminate(true);
BufferedReader stdoutDownload = new BufferedReader(
new InputStreamReader(resProcess.getInputStream()));
String line;
final AtomicInteger percent = new AtomicInteger();
while ((line = stdoutDownload.readLine()) != null
&& !isCancelled()) {
if (line.contains("%")) {
percent.incrementAndGet();
publish(percent.get());
}
}
if (!isCancelled()) {
status = resProcess.waitFor();
}
if (resProcess.exitValue() != 0) {
JOptionPane
.showMessageDialog(null,
"Error! Check your internet connection and that your URL is correct!");
this.cancel(true);
}
resProcess.getInputStream().close();
resProcess.getOutputStream().close();
resProcess.getErrorStream().close();
resProcess.destroy();
} else {
this.cancel(true);
}
} else {
// file doesn't exist, download
String dwnCmd = "wget " + " --progress=dot " + url + " -O "
+ outputFile;
//System.out.println(dwnCmd);
ProcessBuilder downloadBuilder = new ProcessBuilder("bash",
"-c", dwnCmd);
downloadBuilder.redirectErrorStream(true);
Process downloadProcess = downloadBuilder.start();
progressBar.setIndeterminate(true);
BufferedReader stdoutDownload = new BufferedReader(
new InputStreamReader(downloadProcess.getInputStream()));
String line;
final AtomicInteger percent = new AtomicInteger();
while ((line = stdoutDownload.readLine()) != null
&& !isCancelled()) {
if (line.contains("%")) {
percent.incrementAndGet();
publish(percent.get());
}
}
if (!isCancelled()) {
status = downloadProcess.waitFor();
}
if (downloadProcess.exitValue() != 0) {
JOptionPane
.showMessageDialog(null,
"Error! Check your internet connection and that your URL is correct!");
this.cancel(true);
}
downloadProcess.getInputStream().close();
downloadProcess.getOutputStream().close();
downloadProcess.getErrorStream().close();
downloadProcess.destroy();
}
return status;
}
@Override
protected void process(List<Integer> chunks) {
}
@Override
protected void done() {
if (!this.isCancelled()) {
JOptionPane.showMessageDialog(null, "Download completed!");
progressBar.setIndeterminate(false);
} else if (this.isCancelled()) {
JOptionPane.showMessageDialog(null, "Download not completed.");
progressBar.setIndeterminate(false);
}
}
}
}