summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/ftp/FtpCredentialsScheduler.java
blob: eba3f55451b885634c7c247f05302bfa8363c3e2 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
package org.openslx.imagemaster.ftp;

import java.io.File;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
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.db.DbImage;

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()
	{
		synchronized ( App.ftpServer.users ) {
			List<DbImage> uploadingImages = DbImage.getUploadingImages();	// get the uploading images
			// List to save the users that need to be deleted after iterating the map
			List<String> usersToDelete = new LinkedList<>();
			// check all folders
			for ( Map.Entry<String, MasterFtpServer.Infos> entry : App.ftpServer.users.entrySet() ) {
				if ( entry == null )
					continue;
				String username = entry.getKey();
				File dir = new File( Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + username );
				if ( !dir.exists() )
					continue;
				File[] list = dir.listFiles();
				if ( list.length == 1 ) {
					// check file size first
					DbImage currentImage = null;
					for (DbImage img : uploadingImages) {									// find the image that was uploaded by this user
						if (img.ftpUser == username) currentImage = img;
					}
					if (currentImage != null && list[0].getTotalSpace() == currentImage.fileSize) {
						ImageProcessor.processImageAfterUpload( username, list[0].getName() );
					} else if ( ( new Date().getTime() - list[0].lastModified() ) >= timeout ) { // check timeout
						log.info( username + "'s files are too old. Deleting him and his folder." );
						usersToDelete.add( username );
					}
				} else if ( list.length > 1 ) {
					log.info( "User '" + username + "' uploaded too many files. Deleting his account and his folder." );
					usersToDelete.add( username );
				} else {
					// check the creation time of the user
					if ( ( System.currentTimeMillis() - App.ftpServer.users.get( username ).getCreateTime() ) >= timeout ) {
						log.info( username + " did nothing for too long. Deleting him and his folder" );
						usersToDelete.add( username );
					}
				}
			}
			// now delete users
			for (String u : usersToDelete) {
				App.ftpServer.removeUser( u );
				ImageProcessor.removeImageFromProcessList( u );
			}
			
		}
	}

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

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

}