Bug fixes file upload and download
This commit is contained in:
@@ -80,7 +80,10 @@ public class TransactionService {
|
|||||||
buildTransaction(fromAccount, toAccount, amount, description, date, recurringTransaction, taxRelevant);
|
buildTransaction(fromAccount, toAccount, amount, description, date, recurringTransaction, taxRelevant);
|
||||||
|
|
||||||
transaction.setPeriods(getRelevantPeriods(transaction));
|
transaction.setPeriods(getRelevantPeriods(transaction));
|
||||||
|
|
||||||
|
if (StringUtils.isNotEmpty(fileName) && StringUtils.isNotEmpty(fileContent)) {
|
||||||
transaction.setFiles(Collections.singleton(buildFile(fileName, fileContent)));
|
transaction.setFiles(Collections.singleton(buildFile(fileName, fileContent)));
|
||||||
|
}
|
||||||
|
|
||||||
fromAccount.setCurrentBalance(fromAccount.getCurrentBalance() + (this.ruleService
|
fromAccount.setCurrentBalance(fromAccount.getCurrentBalance() + (this.ruleService
|
||||||
.getMultiplierFromAccount(fromAccount) * amount));
|
.getMultiplierFromAccount(fromAccount) * amount));
|
||||||
|
|||||||
@@ -54,6 +54,11 @@
|
|||||||
<artifactId>jfreechart</artifactId>
|
<artifactId>jfreechart</artifactId>
|
||||||
<version>1.5.0</version>
|
<version>1.5.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.overviewproject</groupId>
|
||||||
|
<artifactId>mime-types</artifactId>
|
||||||
|
<version>0.1.3</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- Financer dependencies -->
|
<!-- Financer dependencies -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import de.financer.model.File;
|
|||||||
import de.financer.template.FinancerRestTemplate;
|
import de.financer.template.FinancerRestTemplate;
|
||||||
import de.financer.template.exception.FinancerRestException;
|
import de.financer.template.exception.FinancerRestException;
|
||||||
import de.financer.util.ControllerUtils;
|
import de.financer.util.ControllerUtils;
|
||||||
|
import org.overviewproject.mime_types.GetBytesException;
|
||||||
|
import org.overviewproject.mime_types.MimeTypeDetector;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.core.io.ByteArrayResource;
|
import org.springframework.core.io.ByteArrayResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
@@ -36,8 +38,20 @@ public class FileController {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final byte[] decodedFileContent = Base64.getDecoder().decode(file.getContent());
|
||||||
|
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"")
|
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"")
|
||||||
.body(new ByteArrayResource(Base64.getDecoder().decode(file.getContent())));
|
.header(HttpHeaders.CONTENT_TYPE, getMimeTypeForFile(file.getName(), decodedFileContent))
|
||||||
|
.header(HttpHeaders.CONTENT_LENGTH, String.valueOf(decodedFileContent.length))
|
||||||
|
.body(new ByteArrayResource(decodedFileContent));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getMimeTypeForFile(String fileName, byte[] content) {
|
||||||
|
try {
|
||||||
|
return new MimeTypeDetector().detectMimeType(fileName, () -> content);
|
||||||
|
} catch(GetBytesException e) {
|
||||||
|
return "text/html";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import de.financer.form.NewTransactionForm;
|
|||||||
import de.financer.model.Account;
|
import de.financer.model.Account;
|
||||||
import de.financer.template.exception.FinancerRestException;
|
import de.financer.template.exception.FinancerRestException;
|
||||||
import de.financer.util.ControllerUtils;
|
import de.financer.util.ControllerUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.core.ParameterizedTypeReference;
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
@@ -119,7 +120,7 @@ public class TransactionController {
|
|||||||
requestDto.setDescription(form.getDescription());
|
requestDto.setDescription(form.getDescription());
|
||||||
requestDto.setTaxRelevant(form.getTaxRelevant());
|
requestDto.setTaxRelevant(form.getTaxRelevant());
|
||||||
|
|
||||||
if (form.getFile() != null) {
|
if (form.getFile() != null && StringUtils.isNotEmpty(form.getFile().getOriginalFilename())) {
|
||||||
try {
|
try {
|
||||||
requestDto.setFileContent(Base64.getEncoder().encodeToString(form.getFile().getBytes()));
|
requestDto.setFileContent(Base64.getEncoder().encodeToString(form.getFile().getBytes()));
|
||||||
requestDto.setFileName(form.getFile().getOriginalFilename());
|
requestDto.setFileName(form.getFile().getOriginalFilename());
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
v29 -> v30:
|
||||||
|
- Fix a bug in the file upload feature that caused every transaction to have a file, even though no file was uploaded
|
||||||
|
- Set proper HTTP headers for file download (mime type, length)
|
||||||
|
|
||||||
v28 -> v29:
|
v28 -> v29:
|
||||||
- Internal optimizations
|
- Internal optimizations
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user