-
Notifications
You must be signed in to change notification settings - Fork 187
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 #17 from getmoneynote/dev
feat: file upload
- Loading branch information
Showing
18 changed files
with
302 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,8 +31,9 @@ | |
|
||
## 主要功能 | ||
|
||
- 监控个人资产负债。 | ||
- 记录个人支出和收入。 | ||
- 监控个人资产负债 | ||
- 记录个人支出和收入 | ||
- 支持账单添加多个附件 | ||
- 支持多个账本记账 | ||
- 支持多币种 | ||
- 支持多种账本模板 | ||
|
30 changes: 30 additions & 0 deletions
30
moneynote-api-base/src/main/java/cn/biq/mn/base/validation/FileValidator.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,30 @@ | ||
package cn.biq.mn.base.validation; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
public class FileValidator implements ConstraintValidator<ValidFile, MultipartFile> { | ||
|
||
@Override | ||
public void initialize(ValidFile constraintAnnotation) { | ||
|
||
} | ||
|
||
@Override | ||
public boolean isValid(MultipartFile multipartFile, ConstraintValidatorContext context) { | ||
boolean result = true; | ||
String contentType = multipartFile.getContentType(); | ||
if (!isSupportedContentType(contentType)) { | ||
result = false; | ||
} | ||
return result; | ||
} | ||
|
||
private boolean isSupportedContentType(String contentType) { | ||
return contentType.equals("application/pdf") | ||
|| contentType.equals("image/png") | ||
|| contentType.equals("image/jpg") | ||
|| contentType.equals("image/jpeg"); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
moneynote-api-base/src/main/java/cn/biq/mn/base/validation/ValidFile.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,18 @@ | ||
package cn.biq.mn.base.validation; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Documented | ||
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = {FileValidator.class}) | ||
public @interface ValidFile { | ||
String message() default "Only PDF,XML,PNG or JPG images are allowed"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
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
41 changes: 41 additions & 0 deletions
41
moneynote-api-user/src/main/java/cn/biq/mn/user/flowfile/FlowFile.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,41 @@ | ||
package cn.biq.mn.user.flowfile; | ||
|
||
import cn.biq.mn.base.base.BaseEntity; | ||
import cn.biq.mn.user.balanceflow.BalanceFlow; | ||
import cn.biq.mn.user.user.User; | ||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Entity | ||
@Table(name = "t_flow_file") | ||
@Getter | ||
@Setter | ||
public class FlowFile extends BaseEntity { | ||
|
||
@Lob | ||
@Basic(fetch = FetchType.LAZY) | ||
@Column(columnDefinition = "LONGBLOB", nullable = false) | ||
private byte[] data; | ||
|
||
@ManyToOne(optional = false, fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id") | ||
private User creator; //上传人 | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "flow_id") | ||
private BalanceFlow flow; | ||
|
||
@Column(nullable = false) | ||
private Long createTime; | ||
|
||
@Column(length = 32, nullable = false) | ||
private String contentType; | ||
|
||
@Column(nullable = false) | ||
private Long size; | ||
|
||
@Column(length = 512, nullable = false) | ||
private String originalName; | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
moneynote-api-user/src/main/java/cn/biq/mn/user/flowfile/FlowFileController.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,31 @@ | ||
package cn.biq.mn.user.flowfile; | ||
|
||
import cn.biq.mn.base.base.BaseController; | ||
import cn.biq.mn.base.response.BaseResponse; | ||
import cn.biq.mn.user.utils.SessionUtil; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
|
||
@RestController | ||
@RequestMapping("/flow-files") | ||
@RequiredArgsConstructor | ||
public class FlowFileController extends BaseController { | ||
|
||
private final FlowFileService flowFileService; | ||
|
||
@RequestMapping(method = RequestMethod.GET, value = "/view") | ||
public ResponseEntity<byte[]> handleView(@Valid FlowFileViewForm form) { | ||
FlowFile flowFile = flowFileService.getFile(form); | ||
return ResponseEntity.ok().contentType(MediaType.parseMediaType(flowFile.getContentType())).body(flowFile.getData()); | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.DELETE, value = "/{id}") | ||
public BaseResponse handleDelete(@PathVariable("id") Integer id) { | ||
return new BaseResponse(flowFileService.remove(id)); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
moneynote-api-user/src/main/java/cn/biq/mn/user/flowfile/FlowFileDetails.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,20 @@ | ||
package cn.biq.mn.user.flowfile; | ||
|
||
import cn.biq.mn.base.base.BaseDetails; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class FlowFileDetails extends BaseDetails { | ||
|
||
private Long createTime; | ||
private String contentType; | ||
private Long size; | ||
private String originalName; | ||
|
||
public boolean isImage() { | ||
return contentType.equals("image/jpeg") || contentType.equals("image/png"); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
moneynote-api-user/src/main/java/cn/biq/mn/user/flowfile/FlowFileMapper.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 cn.biq.mn.user.flowfile; | ||
|
||
|
||
public class FlowFileMapper { | ||
|
||
public static FlowFileDetails toDetails(FlowFile entity) { | ||
if (entity == null) return null; | ||
var details = new FlowFileDetails(); | ||
details.setId(entity.getId()); | ||
details.setCreateTime(entity.getCreateTime()); | ||
details.setContentType(entity.getContentType()); | ||
details.setSize(entity.getSize()); | ||
details.setOriginalName(entity.getOriginalName()); | ||
return details; | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
moneynote-api-user/src/main/java/cn/biq/mn/user/flowfile/FlowFileRepository.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,15 @@ | ||
package cn.biq.mn.user.flowfile; | ||
|
||
import cn.biq.mn.base.base.BaseRepository; | ||
import cn.biq.mn.user.balanceflow.BalanceFlow; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
|
||
@Repository | ||
public interface FlowFileRepository extends BaseRepository<FlowFile> { | ||
|
||
List<FlowFile> findByFlow(BalanceFlow flow); | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
moneynote-api-user/src/main/java/cn/biq/mn/user/flowfile/FlowFileService.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,42 @@ | ||
package cn.biq.mn.user.flowfile; | ||
|
||
import cn.biq.mn.base.exception.FailureMessageException; | ||
import cn.biq.mn.user.balanceflow.BalanceFlow; | ||
import cn.biq.mn.user.base.BaseService; | ||
import cn.biq.mn.user.book.Book; | ||
import cn.biq.mn.user.group.Group; | ||
import cn.biq.mn.user.user.User; | ||
import cn.biq.mn.user.utils.SessionUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class FlowFileService { | ||
|
||
private final FlowFileRepository flowFileRepository; | ||
private final SessionUtil sessionUtil; | ||
private final BaseService baseService; | ||
|
||
@Transactional(readOnly = true) | ||
public FlowFile getFile(FlowFileViewForm form) { | ||
FlowFile flowFile = flowFileRepository.getReferenceById(form.getId()); | ||
if (!flowFile.getCreateTime().equals(form.getCreateTime())) { | ||
throw new FailureMessageException(); | ||
} | ||
return flowFile; | ||
} | ||
|
||
public boolean remove(Integer id) { | ||
FlowFile flowFile = flowFileRepository.getReferenceById(id); | ||
BalanceFlow flow = baseService.findFlowById(flowFile.getFlow().getId()); | ||
if (flow == null) { | ||
throw new FailureMessageException("auth.error"); | ||
} | ||
flowFileRepository.delete(flowFile); | ||
return true; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
moneynote-api-user/src/main/java/cn/biq/mn/user/flowfile/FlowFileViewForm.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,18 @@ | ||
package cn.biq.mn.user.flowfile; | ||
|
||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.Setter; | ||
|
||
|
||
@Getter @Setter | ||
public class FlowFileViewForm { | ||
|
||
@NonNull | ||
private Integer id; | ||
|
||
// 时间是为了增加地址的安全性 | ||
@NonNull | ||
private Long createTime; | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,4 +43,13 @@ sentry.dsn=https://[email protected]/450 | |
sentry.traces-sample-rate=1.0 | ||
#sentry.debug=true | ||
|
||
user_api_base_url=https://api.moneywhere.com/api/v1/user-api/ | ||
user_api_base_url=https://api.moneywhere.com/api/v1/user-api/ | ||
|
||
|
||
# 文件上传 | ||
# 设置内置Tomcat请求大小 | ||
server.tomcat.max-http-form-post-size=100MB | ||
# 设置请求最大大小 | ||
spring.servlet.multipart.max-request-size=100MB | ||
# 设置文件上传最大大小 | ||
spring.servlet.multipart.max-file-size=100MB |
Oops, something went wrong.