Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/exastudio/opcgui/db/AccountDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.exastudio.opcgui.types.AccountConfig;
import org.exastudio.opcgui.types.StorageProvider;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.statement.StatementContext;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
130 changes: 130 additions & 0 deletions src/main/java/org/exastudio/opcgui/db/SingleBucketDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package org.exastudio.opcgui.db;

import java.util.List;

import org.exastudio.opcgui.types.SingleBucketConfig;
import org.exastudio.opcgui.types.StorageProvider;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import software.amazon.awssdk.regions.Region;

public final class SingleBucketDao {
private static final Logger logger = LoggerFactory.getLogger(SingleBucketDao.class);

private static final String UPSERT_SQL = """
INSERT INTO single_buckets (id, name, bucket_name, provider, access_key, secret_key, region, r2_endpoint)
VALUES (:id, :name, :bucketName, :provider, :accessKey, :secretKey, :region, :r2Endpoint)
ON CONFLICT(id) DO UPDATE SET
name = excluded.name,
bucket_name = excluded.bucket_name,
provider = excluded.provider,
access_key = excluded.access_key,
secret_key = excluded.secret_key,
region = excluded.region,
r2_endpoint = excluded.r2_endpoint
""";

private static final String SELECT_ALL_SQL = """
SELECT id, name, bucket_name, provider, access_key, secret_key, region, r2_endpoint
FROM single_buckets
ORDER BY name COLLATE NOCASE
""";

private static final String DELETE_SQL = """
DELETE FROM single_buckets
WHERE id = :id
""";

private static final RowMapper<SingleBucketConfig> MAPPER = (rs, ctx) -> mapSingleBucket(rs, ctx);

private final Jdbi jdbi;

public SingleBucketDao(Jdbi jdbi) {
this.jdbi = jdbi;
}

public void save(SingleBucketConfig config) {
if (config == null) {
return;
}

jdbi.useHandle(handle -> handle.createUpdate(UPSERT_SQL)
.bind("id", config.id())
.bind("name", config.name())
.bind("bucketName", config.bucketName())
.bind("provider", config.provider().name())
.bind("accessKey", config.accessKey())
.bind("secretKey", config.secretKey())
.bind("region", regionValue(config))
.bind("r2Endpoint", r2EndpointValue(config))
.execute());
}

public List<SingleBucketConfig> findAll() {
return jdbi.withHandle(handle -> handle.createQuery(SELECT_ALL_SQL)
.map(MAPPER)
.list());
}

public void deleteById(String id) {
if (id == null || id.isBlank()) {
return;
}
jdbi.useHandle(handle -> handle.createUpdate(DELETE_SQL)
.bind("id", id)
.execute());
}

private static SingleBucketConfig mapSingleBucket(java.sql.ResultSet rs, StatementContext ctx)
throws java.sql.SQLException {
String id = rs.getString("id");
String name = rs.getString("name");
String bucketName = rs.getString("bucket_name");
String providerValue = rs.getString("provider");

StorageProvider provider = providerValue == null
? StorageProvider.AWS_S3
: StorageProvider.valueOf(providerValue);

String accessKey = rs.getString("access_key");
String secretKey = rs.getString("secret_key");
String regionValue = rs.getString("region");
String r2Endpoint = rs.getString("r2_endpoint");

Region region = null;
if (provider == StorageProvider.AWS_S3 && regionValue != null && !regionValue.isBlank()) {
try {
region = Region.of(regionValue);
} catch (Exception e) {
logger.warn("Invalid region stored for single bucket {}: {}", id, regionValue);
}
}
if (provider == StorageProvider.AWS_S3) {
r2Endpoint = null;
} else if (provider == StorageProvider.CLOUDFLARE_R2) {
region = null;
}

return new SingleBucketConfig(id, name, bucketName, provider, accessKey, secretKey, region, r2Endpoint);
}

private static String regionValue(SingleBucketConfig config) {
if (config.provider() != StorageProvider.AWS_S3) {
return null;
}
Region region = config.region();
return region == null ? null : region.id();
}

private static String r2EndpointValue(SingleBucketConfig config) {
if (config.provider() != StorageProvider.CLOUDFLARE_R2) {
return null;
}
String value = config.r2Endpoint();
return value == null ? null : value.trim();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.exastudio.opcgui.types;

import software.amazon.awssdk.regions.Region;

public record SingleBucketConfig(String id, String name, String bucketName,
StorageProvider provider,
String accessKey, String secretKey,
Region region, String r2Endpoint) {
}
Loading