Commit 7093cc1d authored by neo.wang's avatar neo.wang

Merge branch 'dev_neo2' into 'dev'

[dev] upload balance sheet impl

See merge request root/atms!24
parents 009c7b6b 9fb3e7e3
package pwc.taxtech.atms.constant;
import java.io.File;
import java.util.UUID;
public final class Constant {
public static final String Comma = ",";
......@@ -19,4 +20,8 @@ public final class Constant {
public static final boolean ACTIVE = true;
public static final String DEFAULT_INDUSTRY_ID = "0";
public static final UUID USER_ID_FOR_UPLOAD = UUID.fromString("64D39CF7-467E-4414-B334-AA4B55A4E2B3");
public static final String UPLOAD_FOLDER_NAME="Upload";
}
\ No newline at end of file
package pwc.taxtech.atms.constant.enums;
public enum EnumModule {
EnterpriseMaterial(0),
TrailBalance(1),
Evidence(2),
Voucher(3),
Invoice(4);
private Integer code;
private EnumModule(Integer code) {
this.code = code;
}
public int getCode() {
return code;
}
}
package pwc.taxtech.atms.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import pwc.taxtech.atms.constant.enums.EnumModule;
import pwc.taxtech.atms.dto.PagingResultDto;
import pwc.taxtech.atms.dto.vatdto.InputVATInvoiceDto;
import pwc.taxtech.atms.vat.service.impl.FileUploadAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;
import static pwc.taxtech.atms.constant.Constant.USER_ID_FOR_UPLOAD;
@RestController
@RequestMapping("/api/v1/FileUpload")
public class FileUploadController {
@Autowired
private FileUploadAdapter fileUploadAdapter;
@RequestMapping(value = "NewFile", method = RequestMethod.POST, produces = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity getInputInvoiceTreeViewData(MultipartHttpServletRequest request) {
return fileUploadAdapter.upload(request,EnumModule.TrailBalance, USER_ID_FOR_UPLOAD);
}
}
package pwc.taxtech.atms.vat.service.impl;
import org.apache.xmlbeans.impl.store.Path;
import org.reflections.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import pwc.taxtech.atms.constant.enums.EnumModule;
import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import static pwc.taxtech.atms.constant.Constant.UPLOAD_FOLDER_NAME;
@Service
public class FileUploadAdapter {
private final static Logger LOGGER = LoggerFactory.getLogger(FileUploadAdapter.class);
@Value("${max_file_length}")
private String maxFileSize;
static final String FILE_NAME = "filename";
public static final String TEMP_FILE_NAME = "tempFileName";
public static final String CHUNK_NUMBER = "_chunkNumber";
public static final String CHUNK_SIZE = "_chunkSize";
public static final String TOTAL_SIZE = "_totalSize";
private boolean containsSubFolder = true;//TODO this should not use val should use parameter(neo)
static class FileParamBean {
String fileName;
String tempFileName;
long chunkNumber;
long chunkSize;
long totalSize;
boolean isCanle;
boolean result;
boolean isLastChunk;
boolean isFirsChunk;
}
public ResponseEntity upload(MultipartHttpServletRequest request, EnumModule trailBalance, UUID userIdForUpload) {
if (request.getFileMap().size() <= 0) return ResponseEntity.badRequest().body("NoFile");
FileParamBean paramBean = getQueryStringParameters(request);
if (!paramBean.result) return ResponseEntity.badRequest().body("ParametersInvalid");
if (paramBean.totalSize > Long.valueOf(maxFileSize)) return ResponseEntity.badRequest().body("InvalidFileSize");
String baseFolder = prepareFolder();
if (!Utils.isEmpty(baseFolder)) return ResponseEntity.badRequest().body("PrepareFolderError");
String tempPath = String.format("%s" + File.separator + "%s", baseFolder, paramBean.tempFileName);
String completePath = String.format("%s" + File.separator + "%s", baseFolder, paramBean.tempFileName);
if (!paramBean.isCanle) {
if (paramBean.isFirsChunk) {
LOGGER.debug("First chunk arrived at webservice");
File tempFile = new File(tempPath);
File completeFile = new File(completePath);
if (tempFile.exists()) tempFile.delete();
if (completeFile.exists()) completeFile.delete();
}
if(paramBean.isLastChunk){
LOGGER.debug("Last chunk arrived");
File completeFile = new File(completePath);
File tempFile = new File(tempPath);
tempFile.renameTo(completeFile);
LOGGER.info("{} upload to temp folder sucess",paramBean.fileName);
}
}else {
File tempFile = new File(tempPath);
File completeFile = new File(completePath);
if (tempFile.exists()) tempFile.delete();
if (completeFile.exists()) completeFile.delete();
}
return null;
}
private FileParamBean getQueryStringParameters(MultipartHttpServletRequest request) {
FileParamBean param = new FileParamBean();
param.fileName = request.getParameter(FILE_NAME);
param.tempFileName = request.getParameter(TEMP_FILE_NAME);
param.chunkNumber = Utils.isEmpty(request.getParameter(CHUNK_NUMBER)) ? -1 : Long.parseLong(
request.getParameter(CHUNK_NUMBER));
param.chunkSize = Utils.isEmpty(request.getParameter(CHUNK_SIZE)) ? -1 : Long.parseLong(
request.getParameter(CHUNK_SIZE));
param.totalSize = Utils.isEmpty(request.getParameter(TOTAL_SIZE)) ? -1 : Long.parseLong(
request.getParameter(TOTAL_SIZE));
param.result = (param.chunkNumber != -1L) && (param.chunkSize != -1L) || (param.totalSize != -1L);
if (!param.result) return param;
param.isLastChunk = (param.chunkNumber + 1) * param.chunkSize >= param.totalSize;
param.isFirsChunk = param.chunkNumber == 0;
param.result = true;
return param;
}
private String prepareFolder() {
String uploadFolder = String.format("~/%s", UPLOAD_FOLDER_NAME);
if (containsSubFolder) {
Calendar now = Calendar.getInstance();
now.setTime(new Date());
String subUploadFolder = String.format("%s" + File.separator + "%s", now.get(Calendar.MONTH), now.get(Calendar.DATE));
uploadFolder = String.format("%s" + File.separator + "%s", uploadFolder, subUploadFolder);
File file = new File(uploadFolder);
if (!file.exists()) file.mkdirs();
}
return uploadFolder;
}
}
......@@ -18,4 +18,7 @@ jwt.powerToken=${jwt.powerToken}
ftp.host=${ftp.host}
ftp.port=${ftp.port}
ftp.user=${ftp.user}
ftp.pwd=${ftp.pwd}
\ No newline at end of file
ftp.pwd=${ftp.pwd}
#upload
max_file_length=${max_file_length}
\ No newline at end of file
......@@ -17,4 +17,7 @@ jwt.powerToken=xxxx
ftp.host=cnshaappulv004.asia.pwcinternal.com
ftp.port=21
ftp.user=ftpuser
ftp.pwd=12345678
\ No newline at end of file
ftp.pwd=12345678
#upload
max_file_length=104857600
\ No newline at end of file
......@@ -17,4 +17,7 @@ jwt.powerToken=
ftp.host=cnshaappulv004.asia.pwcinternal.com
ftp.port=21
ftp.user=ftpuser
ftp.pwd=12345678
\ No newline at end of file
ftp.pwd=12345678
#upload
max_file_length=104857600
\ No newline at end of file
......@@ -17,4 +17,7 @@ jwt.powerToken=xxxx
ftp.host=cnshaappulv004.asia.pwcinternal.com
ftp.port=21
ftp.user=ftpuser
ftp.pwd=12345678
\ No newline at end of file
ftp.pwd=12345678
#upload
max_file_length=104857600
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment