Skip to content

Commit

Permalink
fix: fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
YarikRevich committed Nov 29, 2024
1 parent 4e12265 commit a54a5de
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 49 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.objectstorage.exception;

import java.io.IOException;
import java.util.Arrays;
import java.util.Formatter;

/**
* Represents exception used when file units retrieval operation fails.
*/
public class FileUnitsRetrievalFailureException extends IOException {
public FileUnitsRetrievalFailureException() {
this("");
}

public FileUnitsRetrievalFailureException(Object... message) {
super(
new Formatter()
.format("File units retrieval operation failed: %s", Arrays.stream(message).toArray())
.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public ContentRetrievalResult retrieveContent(ValidationSecretsApplication valid
throws ProcessorContentRetrievalFailureException {
List<ContentRetrievalCompound> compounds = new ArrayList<>();

String workspaceUnitKey = workspaceFacade.createWorkspaceUnitKey(validationSecretsApplication);

for (ValidationSecretsUnit validationSecretsUnit : validationSecretsApplication.getSecrets()) {
RepositoryContentUnitDto repositoryContentLocationUnitDto;

Expand Down Expand Up @@ -84,11 +86,19 @@ public ContentRetrievalResult retrieveContent(ValidationSecretsApplication valid
VendorOperationFailureException ignored) {
}

List<ContentRetrievalBackupUnit> backups;

try {
backups = workspaceFacade.getBackupUnits(workspaceUnitKey);
} catch (FileUnitsRetrievalFailureException e) {
throw new ProcessorContentRetrievalFailureException(e.getMessage());
}

compounds.add(
ContentRetrievalCompound.of(
repositoryContentLocationUnitDto.getRoot(),
validationSecretsUnit.getProvider().toString(),
List.of(ContentRetrievalUnits.of(pending, uploaded))));
List.of(ContentRetrievalUnits.of(pending, uploaded, backups))));
}

return ContentRetrievalResult.of(compounds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.stream.Stream;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import lombok.SneakyThrows;
Expand Down Expand Up @@ -95,7 +94,7 @@ public void createContentDirectory(String workspaceUnitDirectory, String type) t
* @param key given workspace unit directory.
* @return result of the check.
*/
public Boolean isUnitDirectoryExist(String key) {
private Boolean isUnitDirectoryExist(String key) {
return Files.exists(Paths.get(properties.getWorkspaceDirectory(), key));
}

Expand All @@ -106,7 +105,7 @@ public Boolean isUnitDirectoryExist(String key) {
* @param type given content directory type.
* @return result of the check.
*/
public Boolean isContentDirectoryExist(String workspaceUnitDirectory, String type) {
private Boolean isContentDirectoryExist(String workspaceUnitDirectory, String type) {
return Files.exists(Paths.get(workspaceUnitDirectory, type));
}

Expand Down Expand Up @@ -339,7 +338,11 @@ public byte[] compressFolder(List<FolderContentUnitDto> folderContentUnits, Stri
writer.putNextEntry(new ZipEntry(WorkspaceConfigurationHelper.getZipFolderDefinition(type)));

for (FolderContentUnitDto folderContentUnit : folderContentUnits) {
writer.putNextEntry(new ZipEntry(folderContentUnit.getLocation()));
writer.putNextEntry(new ZipEntry(
Path.of(
properties.getWorkspaceContentBackupDirectory(),
WorkspaceConfigurationHelper.getZipFile(
folderContentUnit.getLocation())).toString()));

writer.write(folderContentUnit.getContent());
}
Expand All @@ -361,10 +364,10 @@ public byte[] compressFolder(List<FolderContentUnitDto> folderContentUnits, Stri
* @param workspaceUnitKey given user workspace unit key.
* @param type given content type.
* @param name given content name.
* @param inputStream given input.
* @param content given content.
* @throws FileCreationFailureException if file creation operation failed.
*/
public void addContentFile(String workspaceUnitKey, String type, String name, InputStream inputStream)
public void addContentFile(String workspaceUnitKey, String type, String name, byte[] content)
throws FileCreationFailureException {
if (!isUnitDirectoryExist(workspaceUnitKey)) {
try {
Expand Down Expand Up @@ -394,14 +397,6 @@ public void addContentFile(String workspaceUnitKey, String type, String name, In
throw new FileCreationFailureException();
}

byte[] content;

try {
content = compressFile(inputStream);
} catch (InputCompressionFailureException e) {
throw new FileCreationFailureException(e.getMessage());
}

try {
createFile(workspaceUnitDirectory, type, name, content);
} catch (FileWriteFailureException e) {
Expand Down Expand Up @@ -435,6 +430,39 @@ public Boolean isContentFilePresent(String workspaceUnitKey, String type, String
return false;
}

/**
* Retrieves content units in the workspace with the given workspace unit key and of the given type.
*
* @param workspaceUnitKey given user workspace unit key.
* @param type given file type.
* @return retrieves additional content units.
* @throws FileUnitsRetrievalFailureException if content units retrieval failed.
*/
public List<String> getContentUnits(String workspaceUnitKey, String type) throws
FileUnitsRetrievalFailureException {
List<String> result = new ArrayList<>();

if (isUnitDirectoryExist(workspaceUnitKey)) {
String workspaceUnitDirectory;

try {
workspaceUnitDirectory = getUnitDirectory(workspaceUnitKey);
} catch (WorkspaceUnitDirectoryNotFoundException e) {
throw new FileUnitsRetrievalFailureException(e.getMessage());
}

if (isContentDirectoryExist(workspaceUnitDirectory, type)) {
try {
result = getFilesLocations(workspaceUnitDirectory, type);
} catch (FilesLocationsRetrievalFailureException e) {
throw new FileUnitsRetrievalFailureException(e.getMessage());
}
}
}

return result;
}

/**
* Retrieves file from the workspace with the given workspace unit key as compressed byte array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ public class WorkspaceConfigurationHelper {
public static String getZipFolderDefinition(String name) {
return String.format("%s/", name);
}

/**
* Creates zip file definition with the help of the given file name.
*
* @param name given zip file name.
* @return wrapped zip file.
*/
public static String getZipFile(String name) {
return String.format("%s.zip", name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.objectstorage.dto.FolderContentUnitDto;
import com.objectstorage.entity.common.PropertiesEntity;
import com.objectstorage.exception.*;
import com.objectstorage.model.ContentRetrievalBackupUnit;
import com.objectstorage.model.ValidationSecretsApplication;
import com.objectstorage.service.config.ConfigService;
import com.objectstorage.service.workspace.WorkspaceService;
Expand Down Expand Up @@ -75,7 +76,19 @@ public String createFileUnitKey(String name) {
*/
public void addObjectFile(String workspaceUnitKey, String name, InputStream inputStream)
throws FileCreationFailureException {
workspaceService.addContentFile(workspaceUnitKey, properties.getWorkspaceContentObjectDirectory(), name, inputStream);
byte[] content;

try {
content = workspaceService.compressFile(inputStream);
} catch (InputCompressionFailureException e) {
throw new FileCreationFailureException(e.getMessage());
}

workspaceService.addContentFile(
workspaceUnitKey,
properties.getWorkspaceContentObjectDirectory(),
name,
content);
}

/**
Expand All @@ -101,7 +114,7 @@ public void addBackupFile(String workspaceUnitKey, String name, List<FolderConte
workspaceUnitKey,
properties.getWorkspaceContentBackupDirectory(),
name,
new ByteArrayInputStream(content));
content);

Integer amount;

Expand Down Expand Up @@ -150,6 +163,21 @@ public Boolean isBackupFilePresent(String workspaceUnitKey, String name) throws
workspaceUnitKey, properties.getWorkspaceContentBackupDirectory(), name);
}

/**
* Retrieves backup units from the workspace with the given workspace unit key.
*
* @param workspaceUnitKey given user workspace unit key.
* @return retrieved backup units.
* @throws FileUnitsRetrievalFailureException if file units retrieval fails.
*/
public List<ContentRetrievalBackupUnit> getBackupUnits(String workspaceUnitKey) throws FileUnitsRetrievalFailureException {
return workspaceService
.getContentUnits(workspaceUnitKey, properties.getWorkspaceContentBackupDirectory())
.stream()
.map(ContentRetrievalBackupUnit::of)
.toList();
}

/**
* Retrieves object file with the given name and of the given type from the workspace with the given workspace
* unit key as compressed byte array.
Expand Down
10 changes: 10 additions & 0 deletions api-server/src/main/openapi/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ components:
type: array
items:
$ref: "#/components/schemas/ContentRetrievalProviderUnit"
backups:
type: array
items:
$ref: "#/components/schemas/ContentRetrievalBackupUnit"
ContentRetrievalProviderUnit:
required:
- location
Expand All @@ -284,6 +288,12 @@ components:
created_at:
type: integer
format: int64
ContentRetrievalBackupUnit:
required:
- location
properties:
location:
type: string
ContentApplication:
required:
- root
Expand Down
Loading

0 comments on commit a54a5de

Please sign in to comment.