summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/ftp/FtpCredentialsScheduler.java
blob: b0b23aaf4c1f280d7f7a8b979913aedc04cfb99f (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
package org.openslx.imagemaster.ftp;

import java.io.File;
import java.util.Date;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;
import org.openslx.imagemaster.App;
import org.openslx.imagemaster.Globals;
import org.openslx.imagemaster.thrift.iface.FtpCredentials;
import org.openslx.imagemaster.util.Util;

public class FtpCredentialsScheduler extends TimerTask
{
	private static Logger log = Logger.getLogger( FtpCredentialsScheduler.class );

	public static final long timeout = Long.valueOf( Globals.getPropertyInt( Globals.PropInt.FTPTIMEOUT ) ) * 60L * 1000L; // timeout in ms

	@Override
	public void run()
	{
		// check all folders
		for ( Map.Entry<String, FtpCredentials> entry : App.ftpServer.users.entrySet() ) {
			String sessionId = entry.getKey();
			String username = entry.getValue().username;
			File dir = new File( Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + username );
			if ( !dir.exists() )
				continue;
			File[] list = dir.listFiles();
			if ( list.length == 1 ) {
				if ( ( new Date().getTime() - list[0].lastModified() ) >= timeout ) {
					log.info( username + "'s files are too old. Deleting him and his folder." );
					Util.deleteFolder( dir );
					App.ftpServer.removeUser( sessionId );
				}
			} else if ( list.length > 1 ) {
				log.info( username + " uploaded too many files. Deleting his account and his folder." );
				Util.deleteFolder( dir );
				App.ftpServer.removeUser( sessionId );
			} else {
				// check the creation time of the user
				if ( ( new Date().getTime() - App.ftpServer.timeouts.get( username ).getTime() ) >= timeout ) {
					// remove user and his folder
					Util.deleteFolder( dir );
					App.ftpServer.removeUser( sessionId );
					log.info( username + " did nothing for too long. Deleting him and his folder" );
				}
			}
		}
		//TODO: remove image from process list
	}

	public static void startScheduling()
	{
		Timer timer = new Timer();

		// start timer now and fire every 60 seconds
		timer.schedule( new FtpCredentialsScheduler(), 0, 60000 );
	}

}