Skip to content

Commit

Permalink
restored shortName-regexp and second check on fileNumberLimit
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-pol committed Sep 30, 2024
1 parent 255159e commit 571cfa7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,20 @@ public CreateDataFileResult execute(CommandContext ctxt) throws CommandException

try (var zipFile = openZipFile(tempFile, charset)) {
for (var entry : filteredZipEntries(zipFile)) {
if (datafiles.size() > fileNumberLimit) {
logger.warning("Zip upload - too many files.");
warningMessage = "The number of files in the zip archive is over the limit (" + fileNumberLimit
+ "); please upload a zip archive with fewer files, if you want them to be ingested "
+ "as individual DataFiles.";
throw new IOException();
}
String storageIdentifier = FileUtil.generateStorageIdentifier();
File unzippedFile = new File(getFilesTempDirectory() + "/" + storageIdentifier);
Files.copy(zipFile.getInputStream(entry), unzippedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
// No need to check the size of this unpacked file against the size limit,
// since we've already checked for that in the first pass.
var fileEntryName = entry.getName();
var shortName = new File(fileEntryName).getName();
var shortName = getShortName(fileEntryName);
DataFile datafile = FileUtil.createSingleDataFile(version, null, storageIdentifier, shortName,
MIME_TYPE_UNDETERMINED_DEFAULT,
ctxt.systemConfig().getFileFixityChecksumAlgorithm(), null, false);
Expand Down Expand Up @@ -679,10 +686,14 @@ private static boolean isNotFakeFile(String fileName) {
// check if it's a "fake" file - a zip archive entry
// created for a MacOS X filesystem element: (these
// start with "._")
var shortName = new File(fileName).getName();
var shortName = getShortName(fileName);
return !shortName.startsWith("._") && !shortName.startsWith(".DS_Store") && !"".equals(shortName);
}

private static String getShortName(String fileName) {
return fileName.replaceFirst("^.*[\\/]", "");
}

@Override
public Map<String, Set<Permission>> getRequiredPermissions() {
Map<String, Set<Permission>> ret = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void cleanTmpDir() throws IOException {
public void execute_fails_to_upload_when_tmp_does_not_exist() throws FileNotFoundException {

mockTmpLookup();
var cmd = createCmd("scripts/search/data/shape/shapefile.zip", mockDatasetVersion());
var cmd = createCmd("scripts/search/data/shape/shapefile.zip", mockDatasetVersion(), 1000L, 500L);
var ctxt = mockCommandContext(mockSysConfig(true, 0L, MD5, 10));

assertThatThrownBy(() -> cmd.execute(ctxt))
Expand All @@ -73,7 +73,7 @@ public void execute_fails_on_size_limit() throws Exception {
createDirectories(Path.of("target/test/CreateNewDataFilesTest/tmp/temp"));

mockTmpLookup();
var cmd = createCmd("scripts/search/data/binary/3files.zip", mockDatasetVersion());
var cmd = createCmd("scripts/search/data/binary/3files.zip", mockDatasetVersion(), 1000L, 500L);
var ctxt = mockCommandContext(mockSysConfig(true, 50L, MD5, 0));
try (var mockedStatic = Mockito.mockStatic(JhoveFileType.class)) {
mockedStatic.when(JhoveFileType::getJhoveConfigFile).thenReturn("conf/jhove/jhove.conf");
Expand All @@ -91,7 +91,7 @@ public void execute_loads_individual_files_from_uploaded_zip() throws Exception
createDirectories(tempDir);

mockTmpLookup();
var cmd = createCmd("src/test/resources/own-cloud-downloads/greetings.zip", mockDatasetVersion());
var cmd = createCmd("src/test/resources/own-cloud-downloads/greetings.zip", mockDatasetVersion(), 1000L, 500L);
var ctxt = mockCommandContext(mockSysConfig(false, 1000000L, MD5, 10));
try (MockedStatic<JhoveFileType> mockedStatic = Mockito.mockStatic(JhoveFileType.class)) {
mockedStatic.when(JhoveFileType::getJhoveConfigFile).thenReturn("conf/jhove/jhove.conf");
Expand All @@ -118,7 +118,7 @@ public void execute_rezips_sets_of_shape_files_from_uploaded_zip() throws Except
createDirectories(tempDir);

mockTmpLookup();
var cmd = createCmd("src/test/resources/own-cloud-downloads/shapes.zip", mockDatasetVersion());
var cmd = createCmd("src/test/resources/own-cloud-downloads/shapes.zip", mockDatasetVersion(), 1000L, 500L);
var ctxt = mockCommandContext(mockSysConfig(false, 100000000L, MD5, 10));
try (var mockedJHoveFileType = Mockito.mockStatic(JhoveFileType.class)) {
mockedJHoveFileType.when(JhoveFileType::getJhoveConfigFile).thenReturn("conf/jhove/jhove.conf");
Expand Down Expand Up @@ -146,15 +146,15 @@ public void execute_rezips_sets_of_shape_files_from_uploaded_zip() throws Except
}
}

private static @NotNull CreateNewDataFilesCommand createCmd(String name, DatasetVersion dsVersion) throws FileNotFoundException {
private static @NotNull CreateNewDataFilesCommand createCmd(String name, DatasetVersion dsVersion, long allocatedQuotaLimit, long usedQuotaLimit) throws FileNotFoundException {
return new CreateNewDataFilesCommand(
Mockito.mock(DataverseRequest.class),
dsVersion,
new FileInputStream(name),
"example.zip",
"application/zip",
null,
new UploadSessionQuotaLimit(1000L, 500L),
new UploadSessionQuotaLimit(allocatedQuotaLimit, usedQuotaLimit),
"sha");
}

Expand Down

0 comments on commit 571cfa7

Please sign in to comment.