summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/DownloadFile.java
blob: 352aac5622440c59a6d8ce6972c78aabaf1551ea (plain) (blame)
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
package org.openslx.taskmanager.tasks;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.openslx.satserver.util.Exec;
import org.openslx.satserver.util.ProxyHandler;
import org.openslx.satserver.util.Util;
import org.openslx.taskmanager.api.AbstractTask;

import com.google.gson.annotations.Expose;

public class DownloadFile extends AbstractTask
{

	@Expose
	private String url = null;
	@Expose
	private String destination = null;
	@Expose
	private String gpg = null;

	private Output status = new Output();

	private static final String[] ALLOWED_DIRS =
	{ "/srv/openslx/www/boot/", "/tmp/" };

	@Override
	protected boolean initTask()
	{
		this.setStatusObject( status );
		if ( this.url == null ) {
			status.error = "No URL given.";
			return false;
		}
		this.destination = FilenameUtils.normalize( this.destination );
		if ( this.destination == null || !Util.startsWith( this.destination, ALLOWED_DIRS ) || this.destination.endsWith( "/" ) ) {
			status.error = "File not in allowed directory";
			return false;
		}
		return true;
	}

	@Override
	protected boolean execute()
	{
		URLConnection connection = null;
		BufferedInputStream in = null;
		FileOutputStream fout = null;

		// Handle proxy settings before opening connection for downloading.
		ProxyHandler.configProxy();
		try {
			File tmpFile = File.createTempFile( "bwlp-", ".tmp", null );
			connection = new URL( this.url ).openConnection();
			in = new BufferedInputStream( connection.getInputStream() );
			fout = new FileOutputStream( tmpFile );
			status.size = connection.getContentLengthLong();
			if ( status.size <= 0 ) // If size is unknown, fake progress...
				status.progress = 10;

			final byte data[] = new byte[ 90000 ];
			int count;
			while ( ( count = in.read( data, 0, data.length ) ) != -1 ) {
				fout.write( data, 0, count );
				status.complete += count;
				if ( status.size > 0 )
					status.progress = (int) ( 100l * status.complete / status.size );
				else if ( status.progress < 99 && System.currentTimeMillis() % 20 == 0 )
					status.progress++;
			}
			fout.close();
			in.close();
			// If we have a gpg sig, validate
			if ( this.gpg != null && !this.gpg.isEmpty() ) {
				File gpgTempFile = null;
				try {
					gpgTempFile = File.createTempFile( "bwlp-", ".gpg", null );
					Util.writeStringToFile( gpgTempFile, this.gpg );
				} catch ( Exception e ) {
					status.error = "Could not create temporary file for gpg signature";
					return false;
				}
				if ( 0 != Exec.sync( 10, "gpg", "--verify", gpgTempFile.getAbsolutePath(), tmpFile.getAbsolutePath() ) ) {
					status.error = "GPG signature of downloaded file not valid!\n\n" + this.gpg;
					return false;
				}
				gpgTempFile.delete();
			}
			// Move file to destination
			File dest = new File( this.destination );
			FileUtils.forceMkdir( new File( dest.getParent() ) );
			if ( dest.exists() )
				dest.delete();
			if ( !tmpFile.renameTo( dest ) ) {
				status.error = "Could not move downloaded file to destination directory!";
				return false;
			}
			return true;
		} catch ( IOException e ) {
			status.error = "Download error: " + e.toString();
			return false;
		} finally {
			Util.multiClose( in, fout );
		}
	}

	/**
	 * Output - contains additional status data of this task
	 */
	@SuppressWarnings( "unused" )
	private static class Output
	{
		protected String error = null;
		protected long size = -1;
		protected long complete = 0;
		protected int progress = 0;
	}

}