diff options
author | Simon Rettberg | 2014-11-18 18:40:49 +0100 |
---|---|---|
committer | Simon Rettberg | 2014-11-18 18:40:49 +0100 |
commit | ecb072b02e1a70555db0fdf4ed47375d3080a074 (patch) | |
tree | 75db05621458eee14a96ff2d825a30072eb06e40 /src/main/java/org/openslx/taskmanager/tasks/DownloadFile.java | |
parent | Added class ProxyHandler for for configuring proxy settings system wide once ... (diff) | |
download | tmlite-bwlp-ecb072b02e1a70555db0fdf4ed47375d3080a074.tar.gz tmlite-bwlp-ecb072b02e1a70555db0fdf4ed47375d3080a074.tar.xz tmlite-bwlp-ecb072b02e1a70555db0fdf4ed47375d3080a074.zip |
Many improvements and additions:
- Added task+script for lighttpd https config
- Added task for reloading proxy config
- ldapsearch now supports searching for specific user
- DownloadFile now supports checking file integrity through optional gpg signature
Diffstat (limited to 'src/main/java/org/openslx/taskmanager/tasks/DownloadFile.java')
-rw-r--r-- | src/main/java/org/openslx/taskmanager/tasks/DownloadFile.java | 47 |
1 files changed, 39 insertions, 8 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/DownloadFile.java b/src/main/java/org/openslx/taskmanager/tasks/DownloadFile.java index ac9ea0e..1129200 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/DownloadFile.java +++ b/src/main/java/org/openslx/taskmanager/tasks/DownloadFile.java @@ -6,9 +6,11 @@ import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.net.URLConnection; +import java.nio.charset.StandardCharsets; 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; @@ -22,11 +24,13 @@ public class DownloadFile extends AbstractTask 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/" }; + { "/srv/openslx/www/boot/", "/tmp/" }; @Override protected boolean initTask() @@ -50,30 +54,57 @@ public class DownloadFile extends AbstractTask URLConnection connection = null; BufferedInputStream in = null; FileOutputStream fout = null; - + // Handle proxy settings before opening connection for downloading. ProxyHandler.configProxy(); try { - File dest = new File( this.destination ); - FileUtils.forceMkdir( new File( dest.getParent() ) ); - FileUtils.deleteQuietly( dest ); + File tmpFile = File.createTempFile( "bwlp-", ".tmp", null ); connection = new URL( this.url ).openConnection(); in = new BufferedInputStream( connection.getInputStream() ); - fout = new FileOutputStream( dest ); + 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[ 9000 ]; + 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(); + // If we have a gpg sig, validate + if ( this.gpg != null ) { + 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( "gpg", "--homedir", "/opt/openslx/gpg", "--verify", gpgTempFile.getAbsolutePath(), tmpFile.getAbsolutePath() ) ) { + status.error = "GPG signature of downloaded file not valid!"; + 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(); - FileUtils.deleteQuietly( new File( this.destination ) ); return false; } finally { Util.multiClose( in, fout ); |