Skip to content

Commit

Permalink
feature: Integration with OCI Object Storage temp
Browse files Browse the repository at this point in the history
task:  8697bjvqr
  • Loading branch information
KinTrae committed Jan 7, 2025
1 parent 7102085 commit 9c7ecd0
Show file tree
Hide file tree
Showing 36 changed files with 478 additions and 264 deletions.
50 changes: 49 additions & 1 deletion backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<name>backend</name>
<description>backend</description>
<properties>
<java.version>21</java.version>
<java.version>22</java.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -79,8 +79,56 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.2</version>
</dependency>

<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-addons-resteasy-client-configurator</artifactId>
</dependency>

<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-objectstorage</artifactId>
</dependency>

<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-common-httpclient-jersey</artifactId>
</dependency>
<!-- Other dependencies -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.1</version>
</dependency>
<!-- Other dependencies -->


</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-bom</artifactId>
<version>3.55.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-common-httpclient-jersey</artifactId>
<version>3.55.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package meowhub.backend.ext.oci;

import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.objectstorage.ObjectStorage;
import com.oracle.bmc.objectstorage.ObjectStorageClient;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class OCIClientConfiguration {
// Path to OCI configs file
String configurationFilePath = "backend/src/main/resources/config";
String profile = "DEFAULT";

public ObjectStorage getObjectStorage() throws IOException {
//load configs file
final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parse(configurationFilePath, profile);
final ConfigFileAuthenticationDetailsProvider provider = new ConfigFileAuthenticationDetailsProvider(configFile);

//build and return client
return ObjectStorageClient.builder().build(provider);
}

}
42 changes: 42 additions & 0 deletions backend/src/main/java/meowhub/backend/ext/oci/OCIController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package meowhub.backend.ext.oci;

import jakarta.annotation.security.RolesAllowed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("api/ext")
@Deprecated(since = "only to test connection with OCI Object Storage. Otherwise - don't use!!!", forRemoval = true)
@RolesAllowed("ROLE_ADMIN")
public class OCIController {
private static final String urlPrefix = "https://objectstorage.eu-frankfurt-1.oraclecloud.com";

@Autowired
private OCIUploadService ociUploadService;

@PostMapping(path = "upload")
public ResponseEntity<Object> uploadFile(@RequestParam("file") MultipartFile file){
try {
ociUploadService.upload(file, "TEST/"+file.getOriginalFilename());
return ResponseEntity.ok().body("Uploaded File : "+file.getOriginalFilename());
} catch (Exception e) {
return ResponseEntity.internalServerError().body(e.getMessage());
}
}

@GetMapping(path = "file/{fileName}")
public ResponseEntity<Object> getURl(@PathVariable(value = "fileName") String fileName){
try {
return ResponseEntity.ok().body(ociUploadService.getFileObjectUrl(fileName));
} catch (Exception e) {
return ResponseEntity.internalServerError().body(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package meowhub.backend.ext.oci;

import com.oracle.bmc.objectstorage.model.CreatePreauthenticatedRequestDetails;
import com.oracle.bmc.objectstorage.model.PreauthenticatedRequest.BucketListingAction;
import com.oracle.bmc.objectstorage.requests.CreatePreauthenticatedRequestRequest;
import com.oracle.bmc.objectstorage.requests.PutObjectRequest;
import com.oracle.bmc.objectstorage.responses.CreatePreauthenticatedRequestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.Date;
import java.util.UUID;

@Service
public class OCIUploadService {
private static final String urlPrefix = "https://objectstorage.eu-frankfurt-1.oraclecloud.com";

@Value("${oci.objectstorage.bucket-name}")
private String bucketName;

@Value("${oci.objectstorage.namespaceName}")
private String namespaceName;

@Autowired
private OCIClientConfiguration configuration;

public void upload(MultipartFile file, String objectName) throws Exception {
InputStream inputStream = file.getInputStream();

//build upload request
PutObjectRequest putObjectRequest =
PutObjectRequest.builder()
.namespaceName(namespaceName)
.bucketName(bucketName)
.objectName(objectName)
.contentLength(file.getSize())
.putObjectBody(inputStream)
.build();

//upload the file
try {
configuration.getObjectStorage().putObject(putObjectRequest);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
configuration.getObjectStorage().close();
}
}

public String getFileObjectUrl(String objectName) throws Exception{
OffsetDateTime expirationTime = OffsetDateTime.now().plusYears(10);

// Build request details
CreatePreauthenticatedRequestDetails createPreauthenticatedRequestDetails = CreatePreauthenticatedRequestDetails.builder()
.name("OCI_Request")
.bucketListingAction(BucketListingAction.Deny)
.objectName(objectName)
//readonly access
.accessType(CreatePreauthenticatedRequestDetails.AccessType.ObjectRead)
//here we set expiration time for
.timeExpires(Date.from(expirationTime.toInstant())).build();

//Build request
CreatePreauthenticatedRequestRequest createPreauthenticatedRequestRequest = CreatePreauthenticatedRequestRequest.builder()
.namespaceName(namespaceName)
.bucketName(bucketName)
.createPreauthenticatedRequestDetails(createPreauthenticatedRequestDetails)
.opcClientRequestId(UUID.randomUUID().toString()).build();

// send request to oci
CreatePreauthenticatedRequestResponse response = configuration.getObjectStorage().createPreauthenticatedRequest(createPreauthenticatedRequestRequest);
configuration.getObjectStorage().close();

String accessUri = response.getPreauthenticatedRequest().getAccessUri();

return urlPrefix + accessUri;
}
}
12 changes: 0 additions & 12 deletions backend/src/main/java/meowhub/backend/jpa_buddy/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import meowhub.backend.users.models.Picture;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import java.time.LocalDateTime;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -46,12 +40,6 @@ public class Group {
@Column(name = "DESCRIPTION", nullable = false, length = 200)
private String description;

@NotNull
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@OnDelete(action = OnDeleteAction.RESTRICT)
@JoinColumn(name = "PICTURE_ID", nullable = false)
private Picture picture;

@Column(name = "CREATED_AT")
private LocalDateTime createdAt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import meowhub.backend.users.models.Picture;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

Expand All @@ -23,9 +21,7 @@
@Getter
@Setter
@Entity
@Table(name = "MATCHING_PROFILE_PICTURES", schema = "mh_matching", uniqueConstraints = {
@UniqueConstraint(name = "MATCHING_PROFILE_PICTURES_UQ", columnNames = {"MATCHING_PROFILE_ID", "PICTURE_ID"})
})
@Table(name = "MATCHING_PROFILE_PICTURES", schema = "mh_matching")
public class MatchingProfilePicture {
@Id
@Size(max = 36)
Expand All @@ -40,10 +36,12 @@ public class MatchingProfilePicture {
private MatchingProfile matchingProfile;

@NotNull
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@OnDelete(action = OnDeleteAction.RESTRICT)
@JoinColumn(name = "PICTURE_ID", nullable = false)
private Picture picture;
@Column(name = "OCI_NAME", nullable = false, length = 100)
private String ociName;

@NotNull
@Column(name = "OCI_URL", nullable = false, length = 2000)
private String ociUrl;

@NotNull
@Column(name = "PICTURE_INDEX", nullable = false)
Expand All @@ -62,5 +60,4 @@ public class MatchingProfilePicture {
@Size(max = 36)
@Column(name = "MODIFIED_BY", length = 36)
private String modifiedBy;

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import meowhub.backend.users.models.Picture;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

Expand All @@ -37,10 +36,12 @@ public class ProfilePicture {
private Profile profile;

@NotNull
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@OnDelete(action = OnDeleteAction.RESTRICT)
@JoinColumn(name = "PICTURE_ID", nullable = false)
private Picture picture;
@Column(name = "OCI_NAME", nullable = false, length = 100)
private String ociName;

@NotNull
@Column(name = "OCI_URL", nullable = false, length = 2000)
private String ociUrl;

@NotNull
@Column(name = "PICTURE_INDEX", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import meowhub.backend.posts.dtos.PostDto;
import meowhub.backend.posts.services.PostService;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
Expand All @@ -14,7 +15,11 @@
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("api/posts")
Expand All @@ -36,9 +41,9 @@ public ResponseEntity<Page<PostDto>> getPostsForUser(@PathVariable("login") Stri

}

@PostMapping("")
public ResponseEntity<PostDto> createPost(@RequestParam String content, @AuthenticationPrincipal UserDetails userDetails) {
PostDto postDto = postService.createPost(userDetails.getUsername(), content);
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<PostDto> createPost(@RequestPart("content") String content, @RequestPart("pictures") List<MultipartFile> pictures, @AuthenticationPrincipal UserDetails userDetails) {
PostDto postDto = postService.createPost(userDetails.getUsername(), content, pictures);
return ResponseEntity.ok(postDto);
}

Expand Down
12 changes: 12 additions & 0 deletions backend/src/main/java/meowhub/backend/posts/dtos/PostDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import meowhub.backend.shared.dtos.PictureDto;
import meowhub.backend.users.dtos.BasicUserInfoDto;

import java.time.LocalDateTime;
import java.util.List;

@Builder
@Data
Expand All @@ -18,4 +20,14 @@ public class PostDto {
private BasicUserInfoDto author;
private Long numberOfComments;
private LocalDateTime createdAt;
private List<PictureDto> pictures;

//used for postRepository "find" queries
public PostDto(String id, String content, BasicUserInfoDto author, Long numberOfComments, LocalDateTime createdAt) {
this.id = id;
this.content = content;
this.author = author;
this.numberOfComments = numberOfComments;
this.createdAt = createdAt;
}
}
Loading

0 comments on commit 9c7ecd0

Please sign in to comment.