NIHVIVO-160 Remove the maximum file size - no need for it at this level.

This commit is contained in:
jeb228 2010-05-24 19:18:41 +00:00
parent fe89b1f7e9
commit b86ec9e7b2
5 changed files with 6 additions and 136 deletions

View file

@ -18,12 +18,6 @@ public interface FileStorage {
*/ */
String PROPERTY_FILE_STORAGE_BASE_DIR = "upload.directory"; String PROPERTY_FILE_STORAGE_BASE_DIR = "upload.directory";
/**
* The default implementation will use this key to ask
* {@link ConfigurationProperties} for the maximum permissible file size.
*/
String PROPERTY_FILE_MAXIMUM_SIZE = "file.maximum.size";
/** /**
* The default implementation will use this key to ask * The default implementation will use this key to ask
* {@link ConfigurationProperties} for the default URI namespace. * {@link ConfigurationProperties} for the default URI namespace.

View file

@ -21,7 +21,6 @@ import org.apache.log4j.Logger;
* <li>convert an ID (with namespaces) to an absolute path</li> * <li>convert an ID (with namespaces) to an absolute path</li>
* <li>convert an ID (with namespaces) and a filename to a full path for storing * <li>convert an ID (with namespaces) and a filename to a full path for storing
* the file</li> * the file</li>
* <li>parse the string that specifies the maximum size of an uploaded file</li>
* </ul> * </ul>
*/ */
public class FileStorageHelper { public class FileStorageHelper {
@ -259,48 +258,4 @@ public class FileStorageHelper {
encodeName(filename)); encodeName(filename));
} }
/**
* Translate the configuration property for maximum file size from a
* <code>String</code> to a <code>long</code>.
*
* The string must be represent a positive integer, optionally followed by
* "K", "M", or "G" (to indicate kilobytes, megabytes, or gigabytes).
*/
public static long parseMaximumFileSize(String fileSizeString) {
long factor = 1L;
String integerString;
int shorter = fileSizeString.length() - 1;
if (fileSizeString.endsWith("K")) {
factor = 1024L;
integerString = fileSizeString.substring(0, shorter);
} else if (fileSizeString.endsWith("M")) {
factor = 1024L * 1024L;
integerString = fileSizeString.substring(0, shorter);
} else if (fileSizeString.endsWith("G")) {
factor = 1024L * 1024L * 1024L;
integerString = fileSizeString.substring(0, shorter);
} else {
integerString = fileSizeString;
}
long value = 0;
try {
value = Long.parseLong(integerString);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"Maximum file size is invalid: '" + fileSizeString
+ "'. Must be a positive integer, "
+ "optionally followed by 'K', 'M', or 'G'");
}
if (value <= 0L) {
throw new IllegalArgumentException(
"Maximum file size must be more than 0: '" + fileSizeString
+ "'");
}
return value * factor;
}
} }

View file

@ -34,7 +34,6 @@ public class FileStorageImpl implements FileStorage {
private final File baseDir; private final File baseDir;
private final File rootDir; private final File rootDir;
private final File namespaceFile; private final File namespaceFile;
private final long maximumFileSize;
private final Map<Character, String> namespacesMap; private final Map<Character, String> namespacesMap;
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
@ -53,7 +52,7 @@ public class FileStorageImpl implements FileStorage {
* missing, or if it isn't in the expected form. * missing, or if it isn't in the expected form.
*/ */
FileStorageImpl() throws IOException { FileStorageImpl() throws IOException {
this(figureBaseDir(), figureFileNamespace(), figureMaximumFileSize()); this(figureBaseDir(), figureFileNamespace());
} }
/** /**
@ -65,11 +64,9 @@ public class FileStorageImpl implements FileStorage {
* if the configuration property doesn't point to an existing, * if the configuration property doesn't point to an existing,
* writeable directory. * writeable directory.
*/ */
FileStorageImpl(File baseDir, Collection<String> namespaces, FileStorageImpl(File baseDir, Collection<String> namespaces) throws IOException {
long maximumFileSize) throws IOException {
checkBaseDirValid(baseDir); checkBaseDirValid(baseDir);
checkNamespacesValid(namespaces); checkNamespacesValid(namespaces);
checkMaximumFileSizeValid(maximumFileSize);
this.baseDir = baseDir; this.baseDir = baseDir;
this.rootDir = new File(this.baseDir, "file_storage_root"); this.rootDir = new File(this.baseDir, "file_storage_root");
@ -77,8 +74,6 @@ public class FileStorageImpl implements FileStorage {
this.namespaceFile = new File(baseDir, this.namespaceFile = new File(baseDir,
"file_storage_namespaces.properties"); "file_storage_namespaces.properties");
this.maximumFileSize = maximumFileSize;
if (rootDir.exists() && namespaceFile.exists()) { if (rootDir.exists() && namespaceFile.exists()) {
this.namespacesMap = confirmNamespaces(namespaces); this.namespacesMap = confirmNamespaces(namespaces);
} else if (!rootDir.exists() && !namespaceFile.exists()) { } else if (!rootDir.exists() && !namespaceFile.exists()) {
@ -97,13 +92,6 @@ public class FileStorageImpl implements FileStorage {
} }
} }
private void checkMaximumFileSizeValid(long maximumFileSize) {
if (maximumFileSize < 0) {
throw new IllegalArgumentException(
"Maximum file size may not be negative.");
}
}
private void checkNamespacesValid(Collection<String> namespaces) { private void checkNamespacesValid(Collection<String> namespaces) {
if (namespaces == null) { if (namespaces == null) {
throw new NullPointerException("namespaces may not be null."); throw new NullPointerException("namespaces may not be null.");
@ -185,23 +173,6 @@ public class FileStorageImpl implements FileStorage {
return Collections.singleton(fileNamespace); return Collections.singleton(fileNamespace);
} }
/**
* Get the configuration property for the maximum file size and translate it
* into a long integer. It must be a positive integer, optionally followed
* by "K", "M", or "G" (to indicate kilobytes, megabytes, or gigabytes).
*/
private static long figureMaximumFileSize() {
String fileSizeString = ConfigurationProperties
.getProperty(PROPERTY_FILE_MAXIMUM_SIZE);
if (fileSizeString == null) {
throw new IllegalArgumentException(
"Configuration properties must contain a value for '"
+ PROPERTY_FILE_MAXIMUM_SIZE + "'");
}
return FileStorageHelper.parseMaximumFileSize(fileSizeString);
}
/** /**
* Assign arbitrary prefixes to these namespaces. * Assign arbitrary prefixes to these namespaces.
*/ */
@ -398,10 +369,6 @@ public class FileStorageImpl implements FileStorage {
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
int howMany; int howMany;
while (-1 != (howMany = in.read(buffer))) { while (-1 != (howMany = in.read(buffer))) {
if (bytes.size() > this.maximumFileSize) {
throw new IOException("File is too large at this ID: '"
+ id + "', file location '" + file + "'");
}
bytes.write(buffer, 0, howMany); bytes.write(buffer, 0, howMany);
} }
bytes.close(); bytes.close();

View file

@ -3,7 +3,6 @@
package edu.cornell.mannlib.vitro.webapp.utils.filestorage; package edu.cornell.mannlib.vitro.webapp.utils.filestorage;
import static edu.cornell.mannlib.vitro.webapp.utils.filestorage.FileStorageFactory.PROPERTY_IMPLEMETATION_CLASSNAME; import static edu.cornell.mannlib.vitro.webapp.utils.filestorage.FileStorageFactory.PROPERTY_IMPLEMETATION_CLASSNAME;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -64,7 +63,7 @@ public class FileStorageFactoryTest extends AbstractTestClass {
@Test @Test
public void createDefaultImplementation() throws IOException { public void createDefaultImplementation() throws IOException {
setConfigurationProperties(tempDir.getPath(), setConfigurationProperties(tempDir.getPath(),
"http://vivo.myDomain.edu/individual/", "50M"); "http://vivo.myDomain.edu/individual/");
FileStorage fs = FileStorageFactory.getFileStorage(); FileStorage fs = FileStorageFactory.getFileStorage();
assertEquals("implementation class", FileStorageImpl.class, fs assertEquals("implementation class", FileStorageImpl.class, fs
.getClass()); .getClass());
@ -82,20 +81,13 @@ public class FileStorageFactoryTest extends AbstractTestClass {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void baseDirectoryDoesntExist() throws IOException { public void baseDirectoryDoesntExist() throws IOException {
setConfigurationProperties("/bogus/Directory", setConfigurationProperties("/bogus/Directory",
"http://vivo.myDomain.edu/individual/", "50M"); "http://vivo.myDomain.edu/individual/");
FileStorageFactory.getFileStorage(); FileStorageFactory.getFileStorage();
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void defaultNamespaceIsBogus() throws IOException { public void defaultNamespaceIsBogus() throws IOException {
setConfigurationProperties(tempDir.getPath(), "namespace", "50M"); setConfigurationProperties(tempDir.getPath(), "namespace");
FileStorageFactory.getFileStorage();
}
@Test(expected = IllegalArgumentException.class)
public void invalidMaximumFileSize() throws IOException {
setConfigurationProperties(tempDir.getPath(),
"http://vivo.myDomain.edu/individual/", "50X");
FileStorageFactory.getFileStorage(); FileStorageFactory.getFileStorage();
} }
@ -124,11 +116,10 @@ public class FileStorageFactoryTest extends AbstractTestClass {
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
private void setConfigurationProperties(String baseDir, private void setConfigurationProperties(String baseDir,
String defaultNamespace, String maxFileSize) { String defaultNamespace) {
Map<String, String> map = new HashMap<String, String>(); Map<String, String> map = new HashMap<String, String>();
map.put(FileStorage.PROPERTY_FILE_STORAGE_BASE_DIR, baseDir); map.put(FileStorage.PROPERTY_FILE_STORAGE_BASE_DIR, baseDir);
map.put(FileStorage.PROPERTY_DEFAULT_NAMESPACE, defaultNamespace); map.put(FileStorage.PROPERTY_DEFAULT_NAMESPACE, defaultNamespace);
map.put(FileStorage.PROPERTY_FILE_MAXIMUM_SIZE, maxFileSize);
try { try {
Field f = ConfigurationProperties.class.getDeclaredField("theMap"); Field f = ConfigurationProperties.class.getDeclaredField("theMap");

View file

@ -199,41 +199,4 @@ public class FileStorageHelperTest {
assertEquals("fullPath", FULL_RESULT_PATH, actual); assertEquals("fullPath", FULL_RESULT_PATH, actual);
} }
// ----------------------------------------------------------------------
// parseMaximumFileSize
// ----------------------------------------------------------------------
@Test
public void parseMaximumFileSizeBare() {
long size = FileStorageHelper.parseMaximumFileSize("1467898");
assertEquals("", 1467898, size);
}
@Test
public void parseMaximumFileSizeWithSuffixes() {
long size = FileStorageHelper.parseMaximumFileSize("152K");
assertEquals("", 152L * 1024L, size);
size = FileStorageHelper.parseMaximumFileSize("47M");
assertEquals("", 47L * 1024L * 1024L, size);
size = FileStorageHelper.parseMaximumFileSize("3G");
assertEquals("", 3L * 1024L * 1024L * 1024L, size);
}
@Test(expected = IllegalArgumentException.class)
public void parseMaximumFileSizeInvalidSuffix() {
FileStorageHelper.parseMaximumFileSize("152X");
}
@Test(expected = IllegalArgumentException.class)
public void parseMaximumFileSizeNegativeNumber() {
FileStorageHelper.parseMaximumFileSize("-3K");
}
@Test(expected = IllegalArgumentException.class)
public void parseMaximumFileSizeEmbeddedBadCharacter() {
FileStorageHelper.parseMaximumFileSize("1G52K");
}
} }