Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioG70 committed Jul 25, 2024
1 parent 0317ab5 commit fb139d9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import com.databasepreservation.common.client.models.structure.ViewerCell;
import com.databasepreservation.model.data.Cell;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.roda.core.data.exceptions.GenericException;
Expand Down Expand Up @@ -515,7 +518,7 @@ public ResponseEntity<StreamingResponseBody> exportLOB(
return handleExternalLobDownload(configTable, row, columnIndex);
} else {
// TODO use databaseUUID to get siard version
String version = ViewerConstants.SIARD2;
String version = ViewerFactory.getSolrManager().retrieve(ViewerDatabase.class, databaseUUID).getVersion();
return handleInternalLobDownload(database.getPath(), configTable, row, columnIndex, version);
}
}
Expand Down Expand Up @@ -593,30 +596,36 @@ private ResponseEntity<StreamingResponseBody> handleInternalLobDownload(String d
handlebarsFilename = ViewerConstants.SIARD_RECORD_PREFIX + row.getUuid()
+ ViewerConstants.SIARD_LOB_FILE_EXTENSION;
}
String handlebarsMimeType = HandlebarsUtils.applyMimeTypeTemplate(row, tableConfiguration, columnIndex);

String handlebarsMimeType = HandlebarsUtils.applyMimeTypeTemplate(row, tableConfiguration, columnIndex);
if (ViewerStringUtils.isBlank(handlebarsMimeType)) {
handlebarsMimeType = tableConfiguration.getColumnByIndex(columnIndex).getApplicationType();
}

if (LobManagerUtils.isLobEmbedded(tableConfiguration, row, columnIndex)) {
// handle lob as embedded
String lobCellValue = LobManagerUtils.getLobCellValue(tableConfiguration, row, columnIndex);
lobCellValue = lobCellValue.replace(ViewerConstants.SIARD_EMBEDDED_LOB_PREFIX, "");
String decodedString = new String(Base64.decodeBase64(lobCellValue.getBytes()));

if (version.equals(ViewerConstants.SIARD_DK)) {
String filePath = row.getCells().get(row.getCells().keySet().toArray()[row.getCells().size() - 1]).getValue();
return ApiUtils.okResponse(new StreamResponse(handlebarsFilename, handlebarsMimeType,
DownloadUtils.stream(new BufferedInputStream(new ByteArrayInputStream(decodedString.getBytes())))));
DownloadUtils.stream(new BufferedInputStream(new FileInputStream(filePath)))));
} else {
// handle lob as internal on separated folder
ZipFile zipFile = new ZipFile(databasePath);
final ZipEntry entry = zipFile.getEntry(LobManagerUtils.getZipFilePath(tableConfiguration, columnIndex, row));
if (entry == null) {
throw new GenericException("Zip archive entry is missing");
}
if (LobManagerUtils.isLobEmbedded(tableConfiguration, row, columnIndex)) {
// handle lob as embedded
String lobCellValue = LobManagerUtils.getLobCellValue(tableConfiguration, row, columnIndex);
lobCellValue = lobCellValue.replace(ViewerConstants.SIARD_EMBEDDED_LOB_PREFIX, "");
String decodedString = new String(Base64.decodeBase64(lobCellValue.getBytes()));

return ApiUtils.okResponse(new StreamResponse(handlebarsFilename, handlebarsMimeType,
DownloadUtils.stream(new BufferedInputStream(new ByteArrayInputStream(decodedString.getBytes())))));
} else {
// handle lob as internal on separated folder
ZipFile zipFile = new ZipFile(databasePath);
final ZipEntry entry = zipFile.getEntry(LobManagerUtils.getZipFilePath(tableConfiguration, columnIndex, row));
if (entry == null) {
throw new GenericException("Zip archive entry is missing");
}

return ApiUtils.okResponse(new StreamResponse(handlebarsFilename, handlebarsMimeType,
DownloadUtils.stream(new BufferedInputStream(zipFile.getInputStream(entry)))));
return ApiUtils.okResponse(new StreamResponse(handlebarsFilename, handlebarsMimeType,
DownloadUtils.stream(new BufferedInputStream(zipFile.getInputStream(entry)))));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -846,7 +847,13 @@ private static ViewerCell getCell(CollectionStatus collectionConfiguration, View
collectionConfiguration.getTableStatusByTableId(table.getId()).getColumnByIndex(colIndex).getId(),
ViewerLobStoreType.INTERNALLY);
//TODO binaryCell.getFile() pass it to mimetype
detectMimeType(actualViewerRow, result, databasePath, collectionConfiguration, table, colIndex, lobName, true);
String dbContainerName = databasePath.split("/")[databasePath.split("/").length - 1];
if (binaryCell.getFile() != null && dbContainerName.matches("AVID\\.[A-ZÆØÅ]{2,4}\\.[1-9][0-9].*")) {
result.setValue(binaryCell.getFile());
detectMimeType(actualViewerRow, result, databasePath, collectionConfiguration, table, colIndex, binaryCell.getFile(), true);
} else {
detectMimeType(actualViewerRow, result, databasePath, collectionConfiguration, table, colIndex, lobName, true);
}
}
} else if (cell instanceof ComposedCell) {
ComposedCell composedCell = (ComposedCell) cell;
Expand Down Expand Up @@ -890,15 +897,16 @@ private static void detectMimeType(ViewerRow row, ViewerCell cell, String databa
ZipFile zipFile = null;
ZipEntry entry = null;
String dbContainerName = databasePath.split("/")[databasePath.split("/").length - 1];
String siardLobPath;
boolean isSiardDK = dbContainerName.matches("AVID\\.[A-ZÆØÅ]{2,4}\\.[1-9][0-9].*");

if (!dbContainerName.matches("AVID\\.[A-ZÆØÅ]{2,4}\\.[1-9][0-9].*")) {
String siardLobPath = LobManagerUtils.getZipFilePath(tableStatus, colIndex, lobName, ViewerConstants.SIARD_V21);
if (!isSiardDK) {
siardLobPath = LobManagerUtils.getZipFilePath(tableStatus, colIndex, lobName);
zipFile = new ZipFile(databasePath);
entry = zipFile.getEntry(siardLobPath);
} else {
blobIsInsideSiard = false;
//TODO CHANGE WAY TO GET SIARDDK PATH
String siardLobPath = LobManagerUtils.getZipFilePath(tableStatus, colIndex, lobName, ViewerConstants.SIARD_DK);
siardLobPath = lobName;
}

String lobCellValue = cell.getValue();
Expand All @@ -908,6 +916,8 @@ private static void detectMimeType(ViewerRow row, ViewerCell cell, String databa
} else if (blobIsInsideSiard) {
lobCellValue = lobCellValue.replace(ViewerConstants.SIARD_EMBEDDED_LOB_PREFIX, "");
inputStream = new ByteArrayInputStream(Base64.decodeBase64(lobCellValue.getBytes()));
} else if (isSiardDK) {
inputStream = Files.newInputStream(Paths.get(siardLobPath));
} else {
lobCellValue = cell.getValue();
final Path lobPath = Paths.get(lobCellValue);
Expand Down Expand Up @@ -952,7 +962,10 @@ private static void detectMimeType(ViewerRow row, ViewerCell cell, String databa
}

inputStream.close();
zipFile.close();

if (zipFile != null) {
zipFile.close();
}

cell.setMimeType(mimeType);
cell.setFileExtension(fileExtension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
*/
package com.databasepreservation.common.utils;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.databasepreservation.common.client.ViewerConstants;
import com.databasepreservation.common.client.models.status.collection.TableStatus;
Expand Down Expand Up @@ -42,16 +47,30 @@ public static String getZipFilePath(TableStatus configTable, int columnIndex, Vi
return "content" + "/" + siardSchemaFolder + "/" + siardTableFolder + "/" + siardLobFolder + "/" + lobCellValue;
}

public static String getZipFilePath(TableStatus configTable, int columnIndex, String recordValue, String siardVersion) {
public static String getZipFilePath(TableStatus configTable, int columnIndex, String recordValue) {
String siardSchemaFolder = configTable.getSchemaFolder();
String siardTableFolder = configTable.getTableFolder();
String siardLobFolder = ViewerConstants.SIARD_LOB_FOLDER_PREFIX + (columnIndex + 1);

if (siardVersion.equals(ViewerConstants.SIARD_V21)) {
return "content" + "/" + siardSchemaFolder + "/" + siardTableFolder + "/" + siardLobFolder + "/" + recordValue;
} else {
return "content" + "/" + siardSchemaFolder + "/" + siardTableFolder + "/" + siardLobFolder + "/" + recordValue;
return "content" + "/" + siardSchemaFolder + "/" + siardTableFolder + "/" + siardLobFolder + "/" + recordValue;

}

public static Path createZipFromDirectory(Path directoryPath) throws IOException {
Path zipFilePath = directoryPath.resolveSibling(directoryPath.getFileName().toString() + ".zip");
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFilePath))) {
Files.walk(directoryPath).filter(path -> !Files.isDirectory(path)).forEach(path -> {
ZipEntry zipEntry = new ZipEntry(directoryPath.relativize(path).toString());
try {
zos.putNextEntry(zipEntry);
Files.copy(path, zos);
zos.closeEntry();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
return zipFilePath;
}

public static Path getConsolidatedPath(ViewerAbstractConfiguration configuration, String databaseUUID,
Expand Down

0 comments on commit fb139d9

Please sign in to comment.