Skip to content

Commit

Permalink
Merge pull request #46 from educ-ai-org/feat/bucketStorage
Browse files Browse the repository at this point in the history
Feat/bucket storage
  • Loading branch information
luuh-oliveira authored May 28, 2024
2 parents 1327580 + 594ac7f commit 6d91dc9
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 16 deletions.
1 change: 1 addition & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
sed -i 's#\${MONGO_USERNAME}#'"${{ secrets.MONGO_USERNAME }}"'#g' src/main/resources/application.properties
sed -i 's#\${MONGO_PASSWORD}#'"${{ secrets.MONGO_PASSWORD }}"'#g' src/main/resources/application.properties
sed -i 's#\${AZURE_STORAGE_URL}#'"${{ secrets.AZURE_STORAGE_URL }}"'#g' src/main/resources/application.properties
sed -i 's#\${AZURE_STORAGE_BLOB_TOKEN}#'"${{ secrets.AZURE_STORAGE_BLOB_TOKEN }}"'#g' src/main/resources/application.properties
- name: Display application.properties
run: cat src/main/resources/application.properties
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/api/educai/controllers/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

Expand All @@ -29,8 +30,8 @@ public class PostController {
@Secured("ROLE_TEACHER")
@Operation(summary = "Cria um post")
@PostMapping
public ResponseEntity<Post> createPost(@RequestBody @Valid NewPostDTO post){
return ResponseEntity.status(201).body(postService.createPost(post));
public ResponseEntity<Post> createPost(@RequestBody @Valid NewPostDTO post, MultipartFile file){
return ResponseEntity.status(201).body(postService.createPost(post, file));

}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/api/educai/controllers/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public ResponseEntity<Void> logoff(

@Operation(summary = "Adiciona imagem de perfil do usuário")
@PostMapping("/{userId}/picture")
public ResponseEntity<Void> uploadProfilePicture(@RequestParam MultipartFile file, @PathVariable ObjectId userId) {
public ResponseEntity<Void> uploadProfilePicture(MultipartFile file, @PathVariable ObjectId userId) {
userService.uploadFile(file, userId);
return status(200).build();
}
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/api/educai/dto/ClassroomParticipantsDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Getter;

@Getter
public class ClassroomParticipantsDTO {

private String id;
@NotBlank
@Size(max = 100)
private String name;
Expand All @@ -18,11 +22,4 @@ public ClassroomParticipantsDTO(User user) {
this.email = user.getEmail();
}

public String getName() {
return name;
}

public String getEmail() {
return email;
}
}
2 changes: 1 addition & 1 deletion src/main/java/api/educai/entities/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Post {
@NotBlank
@Size(max = 100)
private String description;
private String url;
private String file;
@NotNull
private LocalDate datePosting;
}
28 changes: 25 additions & 3 deletions src/main/java/api/educai/services/AzureBlobService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatusCode;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.azure.core.http.rest.PagedIterable;
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.models.BlobItem;
import org.springframework.web.server.ResponseStatusException;

@Service
public class AzureBlobService {
Expand All @@ -25,21 +27,41 @@ public class AzureBlobService {
@Value("${azure.storage.url}")
private String storageUrl;

@Value("${azure.storage.blob-token}")
private String blobToken;

public String upload(MultipartFile file)
throws IOException {

String uuid = String.valueOf(UUID.randomUUID());
String[] parts;
String type = "";

try {
if (file.getOriginalFilename() != null) {
parts = file.getOriginalFilename().split("\\.");
type = "." + parts[parts.length - 1];
} else {
if (file.getContentType() != null) {
parts = file.getContentType().split("/");
type = "." + parts[parts.length - 1];
}
}
} catch (NullPointerException ex) {
throw new ResponseStatusException(HttpStatusCode.valueOf(415), "Error while trying to upload file. Check the file type and try again.");
}

BlobClient blob = blobContainerClient
.getBlobClient(uuid);
.getBlobClient(uuid + type);
blob.upload(file.getInputStream(),
file.getSize(), true);

return storageUrl + uuid;
return storageUrl + uuid + type;
}

public String getBlobUrl(String fileName){
BlobClient blob = blobContainerClient.getBlobClient(fileName);
return blob.getBlobUrl();
return blob.getBlobUrl() + "?%s".formatted(blobToken);
}

public byte[] download(String fileName)
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/api/educai/services/ClassroomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class ClassroomService {
@Autowired
private ModelMapper mapper;

@Autowired
private AzureBlobService azureBlobService;

public ClassroomInfoDTO createClassroom(Classroom classroom, ObjectId ownerId) {
User user = userService.getUserById(ownerId);

Expand Down Expand Up @@ -134,7 +137,9 @@ public ReportDTO getUserReport(ObjectId classroomId, ObjectId userId) {

public List<Post> getPostsByClassroom(ObjectId classroomId) {
Classroom classroom = classroomRepository.findById(classroomId);
return classroom.getPosts();
List<Post> posts = classroom.getPosts();
posts.forEach(p -> p.setFile(azureBlobService.getBlobUrl(p.getFile())));
return posts;
}

public void deleteClassroom(ObjectId id, ObjectId userId) {
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/api/educai/services/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatusCode;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ResponseStatusException;

import java.io.IOException;
import java.util.List;

@Service
Expand All @@ -23,10 +25,22 @@ public class PostService {
private ClassroomRepository classroomRepository;
@Autowired
private ModelMapper mapper;
@Autowired
private AzureBlobService azureBlobService;

public Post createPost(NewPostDTO newPost) {
public Post createPost(NewPostDTO newPost, MultipartFile file) {
Classroom classroom = classroomRepository.findById(new ObjectId(newPost.getClassroomId()));
Post post = mapper.map(newPost, Post.class);

if (file != null) {
try {
String path = azureBlobService.upload(file);
post.setFile(path);
} catch (IOException ex) {
throw new ResponseStatusException(HttpStatusCode.valueOf(500), "Error while trying to upload file!");
}
}

postRepository.save(post);

classroom.addPost(post);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ spring.mvc.cors.allowed-headers=*
azure.storage.connection.string=${AZURE_STORAGE_CONNECTION_STRING}
azure.storage.container.name=${AZURE_STORAGE_CONTAINER_NAME}
azure.storage.url=${AZURE_STORAGE_URL}
azure.storage.blob-token=${AZURE_STORAGE_BLOB_TOKEN}

# JWT
jwt.token.secret=07BFq1GxbCm49YnsEQte9lk0z9hte8db7rG5wSERMaPZlqM3dGwvvbAYqUZ6WDNQQLzTSUCx7elIYGiTIbDgbYt6kX4IxQ3F9Ugs
Expand Down

0 comments on commit 6d91dc9

Please sign in to comment.