-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from jasonpyau/dev
Add ability to view and upload images to chats
- Loading branch information
Showing
30 changed files
with
778 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
backend/src/main/java/com/jasonpyau/chatapp/controller/AttachmentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.jasonpyau.chatapp.controller; | ||
|
||
import java.time.Duration; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.CacheControl; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
|
||
import com.jasonpyau.chatapp.annotation.GetUser; | ||
import com.jasonpyau.chatapp.annotation.RateLimitAPI; | ||
import com.jasonpyau.chatapp.entity.User; | ||
import com.jasonpyau.chatapp.service.AttachmentService; | ||
import com.jasonpyau.chatapp.service.RateLimitService.Token; | ||
|
||
|
||
@Controller | ||
@RequestMapping("/api/attachment") | ||
public class AttachmentController { | ||
|
||
@Autowired | ||
private AttachmentService attachmentService; | ||
|
||
@GetMapping("/{groupChatId}/{messageId}/{attachmentId}") | ||
@RateLimitAPI(Token.BIG_TOKEN) | ||
public ResponseEntity<byte[]> getAttachment(@GetUser User user, | ||
@PathVariable("groupChatId") Long groupChatId, | ||
@PathVariable("messageId") Long messageId, | ||
@PathVariable("attachmentId") Long attachmentId) { | ||
byte[] bytes = attachmentService.getAttachmentBytes(user, groupChatId, messageId, attachmentId); | ||
return ResponseEntity.ok() | ||
.cacheControl(CacheControl.maxAge(Duration.ofDays(30))) | ||
.contentType(MediaType.parseMediaType(attachmentService.getAttachmentType(attachmentId, groupChatId).getValue())) | ||
.body(bytes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
backend/src/main/java/com/jasonpyau/chatapp/entity/Attachment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package com.jasonpyau.chatapp.entity; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.http.MediaType; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EnumType; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Index; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Entity | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
@Table(name = "attachment", indexes = { | ||
@Index(name = "created_at_ind", columnList = "created_at") | ||
}) | ||
public class Attachment { | ||
|
||
public static final int SIZE_LIMIT_IN_MB = 10; | ||
public static final int FILE_NAME_LENGTH_LIMIT = 100; | ||
public static final String INVALID_ATTACHMENT_TYPE = "'attachmentType' should be one of the following: "+validAttachmentTypes().toString(); | ||
public static final String ATTACHMENT_EXCEEDS_SIZE_LIMIT = "The attachment can be at most "+SIZE_LIMIT_IN_MB+"MB."; | ||
public static final String INVALID_FILE_NAME = "The attachment has too long of a file name. It can be at most "+FILE_NAME_LENGTH_LIMIT+" characters long"; | ||
|
||
public enum AttachmentType { | ||
IMAGE_JPEG_VALUE(MediaType.IMAGE_JPEG_VALUE), IMAGE_PNG_VALUE(MediaType.IMAGE_PNG_VALUE), IMAGE_GIF_VALUE(MediaType.IMAGE_GIF_VALUE); | ||
|
||
@Getter | ||
@JsonValue | ||
private final String value; | ||
|
||
AttachmentType(String value) { | ||
this.value = value; | ||
} | ||
|
||
public static AttachmentType fromValue(String value) { | ||
switch (value) { | ||
case MediaType.IMAGE_JPEG_VALUE: | ||
return IMAGE_JPEG_VALUE; | ||
case MediaType.IMAGE_PNG_VALUE: | ||
return IMAGE_PNG_VALUE; | ||
case MediaType.IMAGE_GIF_VALUE: | ||
return IMAGE_GIF_VALUE; | ||
default: | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
@Column(name = "id") | ||
private Long id; | ||
|
||
@Column(name = "created_at") | ||
private Long createdAt; | ||
|
||
@Column(name = "attachment_type") | ||
@Enumerated(EnumType.STRING) | ||
private AttachmentType attachmentType; | ||
|
||
// "/api/attachment/{groupChat.id}/{message.id}/{id}" | ||
@Column(name = "url") | ||
private String url; | ||
|
||
// If an attachment already exists with the same sender AND file type AND file hash, | ||
// no need to create a new AWS S3 Object. | ||
// "attachment/{sender.id}/{attachmentType}/{fileHash}" | ||
@Column(name = "aws_S3_Key") | ||
@JsonIgnore | ||
private String awsS3Key; | ||
|
||
@Column(name = "file_name") | ||
private String fileName; | ||
|
||
@Column(name = "file_hash") | ||
private String fileHash; | ||
|
||
@Column(name = "file_byte_size") | ||
private Integer fileByteSize; | ||
|
||
@Column(name = "file_compress_byte_size") | ||
private Integer fileCompressByteSize; | ||
|
||
@JoinColumn(name = "sender") | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JsonIgnore | ||
private User sender; | ||
|
||
@JoinColumn(name = "group_chat") | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JsonIgnore | ||
private GroupChat groupChat; | ||
|
||
@JoinColumn(name = "message") | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JsonIgnore | ||
private Message message; | ||
|
||
public static Set<String> validAttachmentTypes() { | ||
return List.of(AttachmentType.values()).stream().map(AttachmentType::getValue).collect(Collectors.toSet()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/com/jasonpyau/chatapp/form/NewMessageForm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,31 @@ | ||
package com.jasonpyau.chatapp.form; | ||
|
||
import org.springframework.util.StringUtils; | ||
|
||
import com.jasonpyau.chatapp.entity.Attachment; | ||
import com.jasonpyau.chatapp.entity.Message; | ||
|
||
import jakarta.validation.constraints.AssertTrue; | ||
import jakarta.validation.constraints.Size; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class NewMessageForm { | ||
|
||
public static final String MISSING_CONTENT_AND_ATTACHMENT = "If 'content' is blank, then 'file' and 'fileName' must not be blank."; | ||
|
||
@Size(max = 1000, message = Message.INVALID_CONTENT) | ||
private String content; | ||
|
||
private String file; | ||
|
||
@Size(max = Attachment.FILE_NAME_LENGTH_LIMIT, message = Attachment.INVALID_FILE_NAME) | ||
private String fileName; | ||
|
||
@AssertTrue(message = MISSING_CONTENT_AND_ATTACHMENT) | ||
public boolean hasContentOrFile() { | ||
return (StringUtils.hasText(content) || (StringUtils.hasText(file) && StringUtils.hasText(fileName))); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
backend/src/main/java/com/jasonpyau/chatapp/repository/AttachmentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.jasonpyau.chatapp.repository; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.jasonpyau.chatapp.entity.Attachment; | ||
|
||
@Repository | ||
public interface AttachmentRepository extends JpaRepository<Attachment, Long> { | ||
|
||
@Query(value = "SELECT * from attachment a WHERE a.id = :id AND a.group_chat = :groupChatId", nativeQuery = true) | ||
public Optional<Attachment> findByIdInGroupChat(@Param("id") Long id, @Param("groupChatId") Long groupChatId); | ||
} |
Oops, something went wrong.