Skip to content

Commit

Permalink
Merge pull request #16 from IlyaLisov/#15
Browse files Browse the repository at this point in the history
#15 Refactor some classes
  • Loading branch information
IlyaLisov authored Feb 10, 2024
2 parents 0de2c84 + 8d2aaa3 commit e04e875
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
6 changes: 2 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,8 @@
<goal>check</goal>
</goals>
<configuration>
<suppressionsFileExpression>suppressions.xml
</suppressionsFileExpression>
<suppressionsLocation>suppressions.xml
</suppressionsLocation>
<suppressionsFileExpression>suppressions.xml</suppressionsFileExpression>
<suppressionsLocation>suppressions.xml</suppressionsLocation>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Optional<StorageFile> find(
final String fileName,
final Path path
) {
Blob result = bucket.get(path.toString() + "/" + fileName);
Blob result = bucket.get(fileName(path, fileName));
if (result == null) {
return Optional.empty();
}
Expand Down Expand Up @@ -132,21 +132,23 @@ public boolean exists(
final String fileName,
final Path path
) {
return bucket.get(path.toString() + "/" + fileName) != null;
return bucket.get(fileName(path, fileName)) != null;
}

@Override
@SneakyThrows
public Path save(
final StorageFile file
) {
String path = file.getPath() != null ? file.getPath() + "/" : "";
bucket.create(
path + file.getFileName(),
fileName(file.getPath(), file.getFileName()),
file.getInputStream().readAllBytes(),
file.getContentType()
);
return Path.of(path, file.getFileName());
return Path.of(fileName(
file.getPath(),
file.getFileName()
));
}

@Override
Expand All @@ -164,7 +166,7 @@ public void delete(
final String fileName,
final Path path
) {
Blob file = bucket.get(path.toString() + "/" + fileName);
Blob file = bucket.get(fileName(path, fileName));
if (file != null) {
file.delete();
}
Expand All @@ -177,7 +179,8 @@ public void delete(
Page<Blob> blobs = bucket.list(
Storage.BlobListOption.prefix(path + "/")
);
blobs.streamAll().forEach(Blob::delete);
blobs.streamAll()
.forEach(Blob::delete);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ public Optional<StorageFile> find(
try (GetObjectResponse result = client.getObject(
GetObjectArgs.builder()
.bucket(bucket)
.object(path.toString() + "/" + fileName)
.object(fileName(path, fileName))
.build()
)) {
StatObjectResponse stat = client.statObject(
StatObjectArgs.builder()
.bucket(bucket)
.object(path + "/" + fileName)
.object(fileName(path, fileName))
.build()
);
StorageFile file = new StorageFile(
Expand Down Expand Up @@ -220,7 +220,7 @@ public boolean exists(
client.statObject(
StatObjectArgs.builder()
.bucket(bucket)
.object(path.toString() + "/" + fileName)
.object(fileName(path, fileName))
.build()
);
return true;
Expand All @@ -234,7 +234,6 @@ public boolean exists(
public Path save(
final StorageFile file
) {
String path = file.getPath() != null ? file.getPath() + "/" : "";
client.putObject(
PutObjectArgs.builder()
.bucket(bucket)
Expand All @@ -243,11 +242,11 @@ public Path save(
file.getInputStream().available(),
-1
)
.object(path + file.getFileName())
.object(fileName(file.getPath(), file.getFileName()))
.contentType(file.getContentType())
.build()
);
return Path.of(path, file.getFileName());
return Path.of(fileName(file.getPath(), file.getFileName()));
}

@Override
Expand All @@ -272,7 +271,7 @@ public void delete(
client.removeObject(
RemoveObjectArgs.builder()
.bucket(bucket)
.object(path.toString() + "/" + fileName)
.object(fileName(path, fileName))
.build()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,24 @@ void delete(
Path path
);

/**
* Creates file name from path and actual name.
*
* @param path path to folder
* @param fileName name of file
* @return file name with path
*/
default String fileName(
final Path path,
final String fileName
) {
if (path == null) {
return fileName;
}
if (fileName == null) {
return path.toString();
}
return path + "/" + fileName;
}

}

0 comments on commit e04e875

Please sign in to comment.