summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util/FsUtil.java
blob: 76571ef4dfb3c6801d9ee66d15b763d8fca618a7 (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
package org.openslx.util;

import java.io.File;

import org.apache.log4j.Logger;

public class FsUtil
{
	
	private static final Logger LOGGER = Logger.getLogger( FsUtil.class );

	public static String getRelativePath( File absolutePath, File parentDir )
	{
		String file;
		String dir;
		try {
			file = absolutePath.getCanonicalPath();
			dir = parentDir.getCanonicalPath() + File.separator;
		} catch ( Exception e ) {
			LOGGER.error( "Could not get relative path for " + absolutePath.toString(), e );
			return null;
		}
		if ( !file.startsWith( dir ) )
			return null;
		return file.substring( dir.length() );
	}

	public static String sanitizeFileName( String fileName )
	{
		fileName = fileName.replaceAll( "[^a-zA-Z0-9_\\-]+", "_" );
		if ( fileName.length() > 40 )
			fileName = fileName.substring( 0, 40 );
		return fileName;
	}

}