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

import java.nio.file.Files;
import java.nio.file.Paths;

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 DeleteFile extends AbstractTask
{

	protected static final String[] ALLOWED_DIRS =
	{ "/tmp/", "/opt/openslx/configs/" };

	@Expose
	private String file = null;

	private Output status = new Output();

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

	@Override
	protected boolean execute()
	{
		try {
			Files.deleteIfExists( Paths.get( this.file ) );
		} 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;
	}

}