package org.openslx.taskmanager.tasks; import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.openslx.satserver.util.Util; import org.openslx.taskmanager.api.AbstractTask; import com.google.gson.annotations.Expose; public class MoveFile extends AbstractTask { protected static final String[] ALLOWED_DIRS = { "/tmp/", "/opt/openslx/configs/" }; @Expose private String source = null; @Expose private String destination = null; private Output status = new Output(); @Override protected boolean initTask() { this.setStatusObject( status ); this.source = FilenameUtils.normalize( this.source ); this.destination = FilenameUtils.normalize( this.destination ); if ( this.source == null || !Util.startsWith( this.source, ALLOWED_DIRS ) ) { status.error = "Source file not in allowed directory"; return false; } if ( this.destination == null || !Util.startsWith( this.destination, ALLOWED_DIRS ) ) { status.error = "Destination file not in allowed directory"; return false; } return true; } @Override protected boolean execute() { try { FileUtils.deleteQuietly( new File( this.destination ) ); Files.move( Paths.get( this.source ), Paths.get( this.destination ) ); } catch ( Exception e1 ) { status.error = e1.toString(); return false; } return true; } /** * Output - contains additional status data of this task */ @SuppressWarnings( "unused" ) private static class Output { protected String error = null; } }