summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/MoveFile.java
blob: e01cf877e2a99031eb49c0da387a63662e4903b9 (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
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;
	}

}