Skip to content
Open
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@
<artifactId>firebase-admin</artifactId>
<version>9.5.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>2.55.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void updateDayDetails(

@PreAuthorize("hasAuthority(T(com.markguiang.backend.role.domain.Role.Authority).WRITE.name())")
@PutMapping("/image-url/{eventId}")
public void updateImageUrl(@PathVariable UUID eventId, URI imageUrl) throws IOException {
public void updateImageUrl(@PathVariable UUID eventId, @RequestParam("path") URI imageUrl) throws IOException {
eventService.updateEventImage(eventId, imageUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@ public class EventReducer implements LinkedHashMapRowReducer<UUID, Event> {
@Override
public void accumulate(Map<UUID, Event> map, RowView rowView) {
UUID eventId = rowView.getColumn("event_id", UUID.class);
String statusStr = rowView.getColumn("status", String.class);
EventStatus status;

Event event =
if (statusStr != null) {
status = EventStatus.valueOf(statusStr);
} else {
status = null;
}

Event event =
map.computeIfAbsent(
eventId,
id ->
Expand All @@ -30,7 +38,7 @@ public void accumulate(Map<UUID, Event> map, RowView rowView) {
rowView.getColumn("description", String.class),
rowView.getColumn("location", String.class),
rowView.getColumn("img_url", URI.class),
EventStatus.valueOf(rowView.getColumn("status", String.class)),
status,
new ArrayList<>()));

UUID dayId = rowView.getColumn("day_id", UUID.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.markguiang.backend.base.exceptions.InvalidDateRangeException;
import com.markguiang.backend.base.model.ValueObject;
import com.markguiang.backend.event.utils.DateUtils;
import com.markguiang.backend.event.utils.Utils;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Comparator;
Expand Down Expand Up @@ -32,8 +32,8 @@ public static boolean hasOverlappingTimes(List<Agenda> agendaList) {
public static boolean allOnDate(List<Agenda> agendaList, OffsetDateTime date) {
Objects.requireNonNull(date);
for (Agenda agenda : agendaList) {
if (!DateUtils.onSameDate(agenda.getStartDate(), date)
|| !DateUtils.onSameDate(agenda.getEndDate(), date)) {
if (!Utils.onSameDate(agenda.getStartDate(), date)
|| !Utils.onSameDate(agenda.getEndDate(), date)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.markguiang.backend.base.model.IdentifiableDomainObject;
import com.markguiang.backend.event.exceptions.DayNotFoundException;
import com.markguiang.backend.event.exceptions.DaysOnSameDateException;
import com.markguiang.backend.event.utils.DateUtils;
import com.markguiang.backend.event.utils.Utils;
import java.net.URI;
import java.time.OffsetDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -155,7 +155,7 @@ public List<Day> getDays() {

private Day getDayWithDate(OffsetDateTime date) {
for (Day day : days) {
if (DateUtils.onSameDate(day.getDate(), date)) {
if (Utils.onSameDate(day.getDate(), date)) {
return day;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public EventService(EventRepository er) {
}

public Event getEventOrThrow(UUID eventID) {
return er.findByID(eventID).orElseThrow(() -> new EventDoesNotExistException(eventID));
return er.findByID(eventID).orElseThrow(() ->
new EventDoesNotExistException(eventID));
}

public Event getEvent(UUID eventID) {
Expand Down

This file was deleted.

18 changes: 18 additions & 0 deletions src/main/java/com/markguiang/backend/event/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.markguiang.backend.event.utils;

import java.time.OffsetDateTime;

public class Utils {
public static boolean onSameDate(OffsetDateTime a, OffsetDateTime b) {
return a.toLocalDate().equals(b.toLocalDate());
}

public static String concatenateStr(String... s) {
StringBuilder sb = new StringBuilder();
for (String string : s) {
sb.append(string);
}

return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.markguiang.backend.infrastructure.storage;

import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.storage.*;
import com.markguiang.backend.infrastructure.storage.base.ObjectStore;
import com.markguiang.backend.infrastructure.storage.base.StorageDetails;
import com.markguiang.backend.infrastructure.storage.base.StoreProperties;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.markguiang.backend.event.utils.Utils.concatenateStr;

@Component
@Primary
public class GCPObjectStore implements ObjectStore {
@Value("${GCP_PROJECTID}")
private String projectId;

@Value("${GCP_BUCKET_PUBLIC}")
private String publicBucket;

@Value("${GCP_BUCKET_PRIVATE}")
private String privateBucket;

@Value("${GOOGLE_CLOUD_STORAGE_JSON}")
private String sa;

private static final String SUFFIX = "/";
private static final String BUCKET_PATH_EVENT = "event/";

private static final String BUCKET_PATH_USER = "users/";

private StoreProperties initProperties(Boolean isPublic) {
return StoreProperties
.builder()
.projectId(projectId)
.bucket(isPublic ? publicBucket : privateBucket)
.sa(sa)
.build();
}

public URL generatePresignedUrlForDownload(String key) throws IOException {
return generateSignedUrl(key);
}

public StorageDetails generatePresignedUrlForUpload(UUID id, String fileType, String fileExtension, boolean isPublic) throws IOException {
String filename = RandomStringUtils.randomAlphabetic(20) + "." + fileExtension;

String filePath = concatenateStr(BUCKET_PATH_EVENT, id.toString(), SUFFIX, filename);

URL signedUrl = generateV4PutObjectSignedUrl(initProperties(isPublic), filePath, fileType);

return new StorageDetails(filePath, signedUrl);
}


private URL generateV4PutObjectSignedUrl(StoreProperties properties, String path, String contentType) throws IOException {

Storage storage = StorageOptions.newBuilder().setProjectId(properties.getProjectId()).build().getService();

BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(properties.getBucket(), path)).build();

Map<String, String> extensionHeaders = new HashMap<>();
extensionHeaders.put("Content-Type", contentType);

return storage.signUrl(
blobInfo,
15,
TimeUnit.MINUTES,
Storage.SignUrlOption.httpMethod(HttpMethod.PUT),
Storage.SignUrlOption.withExtHeaders(extensionHeaders),
Storage.SignUrlOption.withV4Signature(),
Storage.SignUrlOption.signWith(
ServiceAccountCredentials
.fromStream(new ByteArrayInputStream(properties.getSa().getBytes(StandardCharsets.UTF_8)))
)
);
}

public URL generateSignedUrl(String objectPath) throws IOException {
Storage storage = StorageOptions.newBuilder()
.setProjectId(projectId)
.setCredentials(
ServiceAccountCredentials.fromStream(
new ByteArrayInputStream(sa.getBytes(StandardCharsets.UTF_8)) // since sa is a JSON string
)
)
.build()
.getService();

BlobInfo blobInfo = BlobInfo.newBuilder(privateBucket, objectPath).build();

return storage.signUrl(
blobInfo,
15,
TimeUnit.MINUTES,
Storage.SignUrlOption.withV4Signature()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.markguiang.backend.infrastructure.storage;

import com.markguiang.backend.infrastructure.storage.base.DirectObjectStore;
import com.markguiang.backend.infrastructure.storage.base.StorageDetails;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.UUID;

@Component
public class LocalObjectStore implements DirectObjectStore {
Expand All @@ -32,11 +36,14 @@ public InputStream fetch(URI presignedUrl) throws IOException {
return Files.newInputStream(source);
}

public URI generatePresignedUrlForDownload(String key) {
return URI.create(key);
public URL generatePresignedUrlForDownload(String key) throws MalformedURLException {
return URI.create(key).toURL();
}

public URI generatePresignedUrlForUpload(String key) {
return URI.create(key);
public StorageDetails generatePresignedUrlForUpload(UUID id, String fileType, String fileExtension, boolean isPublic) throws IOException {
String key = "/event/" + id + "/upload." + fileExtension;
URL presignedUrl = URI.create(key).toURL();

return new StorageDetails(key, presignedUrl);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StorageService is a generic component that should not be exposed to implementation details. The existing ObjectStore is already the parent class of the new GCPObjectStore.

What you need to do is create another StorageService bean and use it in non dev controllers.

So you would have devStorageService and storageService beans. One would use LocalObjectStore and the other would use GCPObjectStore, both of these are implementations of the ObjectStore class. So you would need no changes in the dependencies of the StorageService class.

And you would use each in the different storageControllers.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 controllers???
devcontroller, storagecontroller, gcpcontroller (will change it to ProdStorageController)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just create multiple ObjectStore beans for the different profiles and make spring insert them into the single storageService bean

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class StorageConfig {

@Bean
public StorageService storageService(ObjectStore objectStore) {
return new StorageService(objectStore);
public StorageService storageService(ObjectStore objectStore, GCPObjectStore gcpObjectStore) {
return new StorageService(objectStore, gcpObjectStore);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

import com.markguiang.backend.infrastructure.storage.base.DirectObjectStore;
import com.markguiang.backend.infrastructure.storage.base.ObjectStore;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.UUID;

import com.markguiang.backend.infrastructure.storage.base.StorageDetails;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class StorageService {
private final ObjectStore os;
private final GCPObjectStore gcpObjectStore;

public StorageService(ObjectStore os) {
public StorageService(ObjectStore os, GCPObjectStore gcpObjectStore) {
this.os = os;
this.gcpObjectStore = gcpObjectStore;
}

public URI store(InputStream is, URI presignedUrl) throws IOException {
Expand All @@ -34,11 +41,12 @@ public InputStream fetch(URI presignedUrl) throws IOException {
throw new UnsupportedOperationException("direct-storage-not-supported-by-this-implementation");
}

public URI generatePresignedUrlForUpload(String key) {
return os.generatePresignedUrlForUpload(key);
public StorageDetails generatePresignedUrlForUpload(UUID id, String fileType, String fileExtension, Boolean isPublic) throws IOException {
return gcpObjectStore.generatePresignedUrlForUpload(id, fileType, fileExtension, isPublic);
}

public URI generatePresignedUrlForDownload(String key) {
return os.generatePresignedUrlForDownload(key);
public URL generatePresignedUrlForDownload(String key) throws IOException {
return gcpObjectStore.generatePresignedUrlForDownload(key);
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.markguiang.backend.infrastructure.storage.base;

import java.net.URI;
import java.io.IOException;
import java.net.URL;
import java.util.UUID;

public interface ObjectStore {
public URI generatePresignedUrlForDownload(String key);
public URL generatePresignedUrlForDownload(String key) throws IOException;

public URI generatePresignedUrlForUpload(String key);
public StorageDetails generatePresignedUrlForUpload(UUID id, String fileType, String fileExtension, boolean isPublic) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.markguiang.backend.infrastructure.storage.base;

import java.net.URL;

public record StorageDetails(String filePath, URL url) {
}
Loading