add export task
This commit is contained in:
parent
f580d3b962
commit
7d322ffe56
@ -1,9 +1,9 @@
|
||||
package com.pudonghot.ambition.crm.service.support;
|
||||
|
||||
import lombok.val;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chyxion.tigon.model.M2;
|
||||
@ -11,14 +11,13 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import me.chyxion.tigon.mybatis.Search;
|
||||
import me.chyxion.tigon.sequence.IdSequence;
|
||||
import org.springframework.util.Assert;
|
||||
import me.chyxion.tigon.mybatis.BaseMapper;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import me.chyxion.tigon.sequence.IdSequence;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.pudonghot.ambition.file.DiskFileApi;
|
||||
import com.pudonghot.ambition.crm.model.FileInfo;
|
||||
import com.pudonghot.ambition.crm.model.Attachment;
|
||||
import com.pudonghot.ambition.crm.model.AttachedImage;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@ -27,6 +26,7 @@ import com.pudonghot.ambition.crm.mapper.AttachedFileMapper;
|
||||
import com.pudonghot.ambition.crm.mapper.AttachedImageMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import com.pudonghot.ambition.file.request.AmInputStreamUploadReq;
|
||||
import com.pudonghot.ambition.crm.form.update.AttachmentFormForUpdate;
|
||||
|
||||
/**
|
||||
@ -64,19 +64,24 @@ public class AttachmentServiceSupport
|
||||
final Date now = new Date();
|
||||
final String fileFolder = fileFolder(ownerId);
|
||||
int i = 0;
|
||||
for (final MultipartFile file : files) {
|
||||
|
||||
for (val file : files) {
|
||||
if (!file.isEmpty()) {
|
||||
final String originalFilename = file.getOriginalFilename();
|
||||
try (final InputStream ins = file.getInputStream()) {
|
||||
final String fileId = idSeq.get();
|
||||
final T appFile = constructor.get();
|
||||
final FileInfo fileInfo = fileApi.upload(ins,
|
||||
file.getSize(),
|
||||
fileFolder,
|
||||
fileId,
|
||||
file.getContentType(),
|
||||
FilenameUtils.getExtension(originalFilename),
|
||||
originalFilename);
|
||||
val originalFilename = file.getOriginalFilename();
|
||||
try (val ins = file.getInputStream()) {
|
||||
val fileId = idSeq.get();
|
||||
val appFile = constructor.get();
|
||||
val req = new AmInputStreamUploadReq();
|
||||
req.setInputStream(ins);
|
||||
req.setContentLength(file.getSize());
|
||||
req.setFolder(fileFolder);
|
||||
req.setName(fileId);
|
||||
req.setContentType(file.getContentType());
|
||||
req.setFormat(FilenameUtils.getExtension(originalFilename));
|
||||
req.setDownloadName(originalFilename);
|
||||
|
||||
val fileInfo = fileApi.upload(req);
|
||||
|
||||
appFile.setId(fileId);
|
||||
appFile.setOwnerId(ownerId);
|
||||
appFile.setFileId(fileInfo.getId());
|
||||
@ -112,15 +117,15 @@ public class AttachmentServiceSupport
|
||||
final Function<String, List<T>> listSort,
|
||||
final Consumer<T> updater) {
|
||||
|
||||
final String id = form.getId();
|
||||
final T appImage = finder.apply(id);
|
||||
val id = form.getId();
|
||||
val appImage = finder.apply(id);
|
||||
Assert.state(appImage != null, "No local product file [" + id + "] found");
|
||||
final String ownerId = appImage.getOwnerId();
|
||||
final String updatedBy = form.getUpdatedBy();
|
||||
val ownerId = appImage.getOwnerId();
|
||||
val updatedBy = form.getUpdatedBy();
|
||||
|
||||
boolean sortUpdated = false;
|
||||
final float sort = form.getSort();
|
||||
final float sortOld = appImage.getSort();
|
||||
val sort = form.getSort();
|
||||
val sortOld = appImage.getSort();
|
||||
if (sort < sortOld) {
|
||||
sortUpdated = true;
|
||||
appImage.setSort(sortOld - 1.5f);
|
||||
@ -150,12 +155,13 @@ public class AttachmentServiceSupport
|
||||
final Function<String, Integer> deleter,
|
||||
final Function<String, Integer> sortUpdater,
|
||||
final Consumer<T> validator) {
|
||||
final T attachment = finder.apply(id);
|
||||
|
||||
val attachment = finder.apply(id);
|
||||
Assert.state(attachment != null, "No file [" + id + "] found");
|
||||
if (validator != null) {
|
||||
validator.accept(attachment);
|
||||
}
|
||||
final String ownerId = attachment.getOwnerId();
|
||||
val ownerId = attachment.getOwnerId();
|
||||
fileApi.delete(fileFolder(ownerId) + "/" + id);
|
||||
deleter.apply(id);
|
||||
sortUpdater.apply(ownerId);
|
||||
@ -170,7 +176,7 @@ public class AttachmentServiceSupport
|
||||
final Consumer<M> validator) {
|
||||
|
||||
log.info("Delete attachment owner [{}].", ownerId);
|
||||
final M owner = mapper.find(ownerId);
|
||||
val owner = mapper.find(ownerId);
|
||||
Assert.state(owner != null, "No attachment owner [" + ownerId + "] found");
|
||||
Assert.state(!owner.isEnabled(), "Attachment owner [" + ownerId + "] is enabled");
|
||||
|
||||
@ -178,7 +184,7 @@ public class AttachmentServiceSupport
|
||||
validator.accept(owner);
|
||||
}
|
||||
|
||||
final Search search = new Search(AttachedImage.OWNER_ID, ownerId);
|
||||
val search = new Search(AttachedImage.OWNER_ID, ownerId);
|
||||
imageMapper.list(search).forEach(
|
||||
image -> fileApi.deleteById(image.getFileId()));
|
||||
imageMapper.delete(search);
|
||||
|
@ -3,10 +3,12 @@ package com.pudonghot.ambition.file;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.io.InputStream;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import com.pudonghot.ambition.file.request.AmFileUploadReq;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import com.pudonghot.ambition.file.request.AmInputStreamUploadReq;
|
||||
|
||||
/**
|
||||
* @version 0.0.4
|
||||
@ -42,67 +44,18 @@ public interface AmbitionFileApi<UploadReturn> {
|
||||
@NotBlank String getUrl(@NotBlank String name);
|
||||
|
||||
/**
|
||||
* Upload File To OSS
|
||||
* @param file File
|
||||
* @param name File Name [optional], Such As "foobar.doc",
|
||||
* A UUID Will Be The Name, If Name Not Specified,
|
||||
* @return File Link [http://umsapp.oss-cn-hangzhou.aliyuncs.com/echat-dev/foobar.png]
|
||||
* upload file
|
||||
* @param req req
|
||||
* @return result
|
||||
*/
|
||||
UploadReturn upload(@NotNull File file, String name);
|
||||
|
||||
/**
|
||||
* Upload File
|
||||
* @param file File
|
||||
* @param folder Custom Folder
|
||||
* @param name File Name
|
||||
* @param contentType Content Type [Optional]
|
||||
* @param format File Format [Optional]
|
||||
* @return
|
||||
*/
|
||||
UploadReturn upload(@NotNull File file, String folder, String name, String contentType, String format);
|
||||
UploadReturn upload(@NotNull @Valid AmFileUploadReq req);
|
||||
|
||||
/**
|
||||
* Upload Stream
|
||||
* @param ins Input Stream
|
||||
* @param name File Name
|
||||
* @return File Link [http://umsapp.oss-cn-hangzhou.aliyuncs.com/echat-dev/foobar.png]
|
||||
* upload file
|
||||
* @param req req
|
||||
* @return result
|
||||
*/
|
||||
UploadReturn upload(@NotNull InputStream ins, @Min(1) long contentLength, String name);
|
||||
|
||||
/**
|
||||
* Upload Stream
|
||||
* @param ins Input Stream
|
||||
* @param folder Custom Folder
|
||||
* @param name name
|
||||
* @param contentType Content Type [Optional]
|
||||
* @param format File Format [Optional]
|
||||
*/
|
||||
UploadReturn upload(@NotNull InputStream ins,
|
||||
@Min(1) long contentLength,
|
||||
String folder,
|
||||
String name,
|
||||
String contentType,
|
||||
String format);
|
||||
|
||||
/**
|
||||
* Upload Stream To OSS
|
||||
* @param ins Input Stream
|
||||
* @param contentLength
|
||||
* @param folder Custom Folder
|
||||
* @param name File Name [optional], Such As "foobar.doc",
|
||||
* A UUID Will Be The Name, If Name Not Specified,
|
||||
* @param contentType Content Type [Optional]
|
||||
* @param format File Format [Optional]
|
||||
* @param downloadName File Download Name [Optional]
|
||||
* @return
|
||||
*/
|
||||
UploadReturn upload(@NotNull InputStream ins,
|
||||
@Min(1) long contentLength,
|
||||
String folder,
|
||||
String name,
|
||||
String contentType,
|
||||
String format,
|
||||
String downloadName);
|
||||
UploadReturn upload(@NotNull @Valid AmInputStreamUploadReq req);
|
||||
|
||||
/**
|
||||
* Get File From OSS
|
||||
|
@ -0,0 +1,51 @@
|
||||
package com.pudonghot.ambition.file.request;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import java.io.File;
|
||||
import java.io.Serializable;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* @author Donghuang
|
||||
* @date May 29, 2022 12:14:35
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public class AmFileUploadReq implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Upload file
|
||||
*/
|
||||
@NotNull
|
||||
private File file;
|
||||
|
||||
/**
|
||||
* custom folder
|
||||
*/
|
||||
private String folder;
|
||||
|
||||
/**
|
||||
* optional file name, such as "some-file.doc"
|
||||
* A UUID will be the name, if name not specified
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* file content type [optional]
|
||||
*/
|
||||
private String contentType;
|
||||
|
||||
/**
|
||||
* file format
|
||||
*/
|
||||
private String format;
|
||||
|
||||
/**
|
||||
* file download name
|
||||
*/
|
||||
private String downloadName;
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package com.pudonghot.ambition.file.request;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import java.io.InputStream;
|
||||
import java.io.Serializable;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* @author Donghuang
|
||||
* @date May 29, 2022 12:14:35
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public class AmInputStreamUploadReq implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Input stream
|
||||
*/
|
||||
@NotNull
|
||||
private InputStream inputStream;
|
||||
|
||||
/**
|
||||
* file content length
|
||||
*/
|
||||
@Min(1)
|
||||
@NotNull
|
||||
private Number contentLength;
|
||||
|
||||
/**
|
||||
* custom folder
|
||||
*/
|
||||
private String folder;
|
||||
|
||||
/**
|
||||
* optional file name, such as "some-file.doc"
|
||||
* A UUID will be the name, if name not specified
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* file content type [optional]
|
||||
*/
|
||||
private String contentType;
|
||||
|
||||
/**
|
||||
* file format
|
||||
*/
|
||||
private String format;
|
||||
|
||||
/**
|
||||
* file download name
|
||||
*/
|
||||
private String downloadName;
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package com.pudonghot.ambition.file.support;
|
||||
|
||||
import lombok.val;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
@ -20,7 +21,9 @@ import me.chyxion.tigon.sequence.IdSequence;
|
||||
import com.pudonghot.ambition.file.ImageTool;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import com.pudonghot.ambition.file.AmbitionFileApi;
|
||||
import com.pudonghot.ambition.file.request.AmFileUploadReq;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import com.pudonghot.ambition.file.request.AmInputStreamUploadReq;
|
||||
|
||||
/**
|
||||
* @version 0.0.1
|
||||
@ -40,55 +43,41 @@ public abstract class AbstractAmbitionFileApi<UploadReturn> implements AmbitionF
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public UploadReturn upload(final File file, final String folder, String name, final String contentType, String format) {
|
||||
public UploadReturn upload(final AmFileUploadReq fileReq) {
|
||||
val file = fileReq.getFile();
|
||||
val folder = fileReq.getFolder();
|
||||
String name = fileReq.getName();
|
||||
val contentType = fileReq.getContentType();
|
||||
String format = fileReq.getFormat();
|
||||
|
||||
log.info("Upload input stream file [{}].", name);
|
||||
if (StringUtils.isBlank(name)) {
|
||||
name = idSeq.get();
|
||||
String fileExt = FilenameUtils.getExtension(file.getName());
|
||||
if (StringUtils.isNotBlank(fileExt)
|
||||
val fileExt = FilenameUtils.getExtension(file.getName());
|
||||
if (StringUtils.isNotBlank(fileExt)
|
||||
&& StringUtils.isBlank(format)) {
|
||||
format = fileExt;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
val req = new AmInputStreamUploadReq();
|
||||
try {
|
||||
return upload(new FileInputStream(file),
|
||||
file.length(),
|
||||
folder,
|
||||
name,
|
||||
contentType,
|
||||
format);
|
||||
req.setInputStream(new FileInputStream(file));
|
||||
}
|
||||
catch (FileNotFoundException e) {
|
||||
throw new IllegalStateException(
|
||||
"Upload File Error Caused, File Not Found", e);
|
||||
"Upload file error caused, file not found", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public UploadReturn upload(File file, String name) {
|
||||
return upload(file, null, name, null, null);
|
||||
}
|
||||
req.setContentLength(file.length());
|
||||
req.setFolder(folder);
|
||||
req.setName(name);
|
||||
req.setContentType(contentType);
|
||||
req.setFormat(format);
|
||||
req.setDownloadName(fileReq.getDownloadName());
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public UploadReturn upload(InputStream in, long contentLength, String name) {
|
||||
return upload(in, contentLength, null, name, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public UploadReturn upload(InputStream in,
|
||||
long contentLength,
|
||||
String folder, String name,
|
||||
String contentType, String fileFormat) {
|
||||
return upload(in, contentLength, folder, name, contentType, fileFormat, null);
|
||||
return upload(req);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -96,25 +85,19 @@ public abstract class AbstractAmbitionFileApi<UploadReturn> implements AmbitionF
|
||||
*/
|
||||
@Override
|
||||
public byte[] combineAvatars(List<String> members) {
|
||||
List<BufferedImage> images = new LinkedList<BufferedImage>();
|
||||
for (String memberId : members) {
|
||||
InputStream imageInput = null;
|
||||
try {
|
||||
imageInput = getInputStream("avatar/" + memberId);
|
||||
val images = new LinkedList<BufferedImage>();
|
||||
|
||||
for (val memberId : members) {
|
||||
try (val imageInput = getInputStream("avatar/" + memberId)) {
|
||||
images.add(ImageIO.read(imageInput));
|
||||
}
|
||||
catch (IOException e) {
|
||||
uploadAvatar(memberId, "U", "");
|
||||
log.error("Read Member [{}] Avatar Error Caused", memberId, e);
|
||||
}
|
||||
finally {
|
||||
IOUtils.closeQuietly(imageInput);
|
||||
}
|
||||
}
|
||||
if (!images.isEmpty()) {
|
||||
ByteArrayOutputStream baos = null;
|
||||
try {
|
||||
baos = new ByteArrayOutputStream();
|
||||
try (val baos = new ByteArrayOutputStream()) {
|
||||
ImageIO.write(imageTool.combine(images), "png", baos);
|
||||
return baos.toByteArray();
|
||||
}
|
||||
@ -122,12 +105,9 @@ public abstract class AbstractAmbitionFileApi<UploadReturn> implements AmbitionF
|
||||
throw new IllegalStateException(
|
||||
"Write Image File Error Caused", e);
|
||||
}
|
||||
finally {
|
||||
IOUtils.closeQuietly(baos);
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException(
|
||||
"No Image Found In Members " + members);
|
||||
"No image found in members " + members);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -152,11 +132,14 @@ public abstract class AbstractAmbitionFileApi<UploadReturn> implements AmbitionF
|
||||
@Override
|
||||
public UploadReturn uploadAvatar(String folder, String memberId, String name, String gender) {
|
||||
byte[] bytesAvatar = imageTool.genAvatar(name, gender);
|
||||
return upload(new ByteArrayInputStream(bytesAvatar),
|
||||
bytesAvatar.length,
|
||||
folder,
|
||||
memberId,
|
||||
MimeTypeUtils.IMAGE_PNG_VALUE, "png");
|
||||
val req = new AmInputStreamUploadReq();
|
||||
req.setInputStream(new ByteArrayInputStream(bytesAvatar));
|
||||
req.setContentLength(bytesAvatar.length);
|
||||
req.setFolder(folder);
|
||||
req.setName(memberId);
|
||||
req.setContentType(MimeTypeUtils.IMAGE_PNG_VALUE);
|
||||
req.setFormat("png");
|
||||
return upload(req);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -202,15 +185,21 @@ public abstract class AbstractAmbitionFileApi<UploadReturn> implements AmbitionF
|
||||
String format = imageTool.imageType(new ByteArrayInputStream(bytesFile));
|
||||
if (StringUtils.isNotBlank(format)) {
|
||||
if (!ArrayUtils.contains(new String[] {"png", "jpg", "jpeg"}, format)) {
|
||||
log.info("Image File Type Is Not JPG Or PNG, Convert To JPG.");
|
||||
log.info("Image file type is not `JPG` Or `PNG`, convert to JPG.");
|
||||
bytesFile = imageTool.toJpegBytes(bytesFile);
|
||||
format = "jpg";
|
||||
}
|
||||
log.info("Upload Image With Folder [{}], Name [{}], Format [{}].", folder, name, format);
|
||||
return upload(
|
||||
new ByteArrayInputStream(bytesFile),
|
||||
bytesFile.length,
|
||||
folder, name, "image/" + format, format);
|
||||
|
||||
val req = new AmInputStreamUploadReq();
|
||||
req.setInputStream(new ByteArrayInputStream(bytesFile));
|
||||
req.setContentLength(bytesFile.length);
|
||||
req.setFolder(folder);
|
||||
req.setName(name);
|
||||
req.setContentType("image/" + format);
|
||||
req.setFormat(format);
|
||||
|
||||
return upload(req);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException(
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.pudonghot.ambition.file.disk;
|
||||
|
||||
import java.io.*;
|
||||
import lombok.val;
|
||||
import java.util.Date;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
@ -15,6 +16,7 @@ import com.pudonghot.ambition.crm.mapper.FileInfoMapper;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import com.pudonghot.ambition.file.request.AmInputStreamUploadReq;
|
||||
import com.pudonghot.ambition.file.support.AbstractAmbitionFileApi;
|
||||
|
||||
/**
|
||||
@ -41,11 +43,11 @@ public class DiskFileSupport
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public InputStream getInputStream(String name) {
|
||||
public InputStream getInputStream(final String name) {
|
||||
try {
|
||||
return new FileInputStream(getFile(name));
|
||||
}
|
||||
catch (FileNotFoundException e) {
|
||||
catch (final FileNotFoundException e) {
|
||||
throw new IllegalStateException(
|
||||
"Get file [" + name + "] input stream error caused");
|
||||
}
|
||||
@ -65,7 +67,7 @@ public class DiskFileSupport
|
||||
@Override
|
||||
@Transactional(rollbackFor = Throwable.class)
|
||||
public void delete(final String name) {
|
||||
final FileInfo fileInfo = findFileInfo(name);
|
||||
val fileInfo = findFileInfo(name);
|
||||
if (fileInfo != null) {
|
||||
fileInfoMapper.delete(fileInfo.getId());
|
||||
FileUtils.deleteQuietly(getFile(fileInfo));
|
||||
@ -85,23 +87,24 @@ public class DiskFileSupport
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public FileInfo upload(final InputStream in,
|
||||
final long contentLength,
|
||||
String folder,
|
||||
final String name,
|
||||
String contentType,
|
||||
String fileFormat,
|
||||
String downloadName) {
|
||||
final String fileId = idSeq.get();
|
||||
final String fullName = getFileFullName(fileId, folder, name);
|
||||
public FileInfo upload(final AmInputStreamUploadReq req) {
|
||||
|
||||
val folder = req.getFolder();
|
||||
val name = req.getName();
|
||||
|
||||
val fileId = idSeq.get();
|
||||
val fullName = getFileFullName(fileId, folder, name);
|
||||
|
||||
boolean update = false;
|
||||
Date now = new Date();
|
||||
|
||||
val now = new Date();
|
||||
FileInfo fileInfo = findFileInfo(fullName);
|
||||
|
||||
if (fileInfo != null) {
|
||||
fileInfo.setDateUpdated(now);
|
||||
update = true;
|
||||
// delete previous file
|
||||
final File prevFile = getFile(fileInfo);
|
||||
val prevFile = getFile(fileInfo);
|
||||
log.info("Will delete previous file [{}].", prevFile);
|
||||
// delete previous files
|
||||
if (prevFile != null) {
|
||||
@ -116,12 +119,15 @@ public class DiskFileSupport
|
||||
fileInfo.setDateCreated(now);
|
||||
}
|
||||
|
||||
final String filePath = getFilePath(fileId, folder, name, fileFormat);
|
||||
val fileFormat = req.getFormat();
|
||||
val filePath = getFilePath(fileId, folder, name, fileFormat);
|
||||
fileInfo.setFilePath(filePath);
|
||||
fileInfo.setEnabled(true);
|
||||
fileInfo.setContentLength(contentLength);
|
||||
fileInfo.setDownloadName(downloadName);
|
||||
fileInfo.setContentType(StringUtils.defaultIfBlank(contentType,
|
||||
|
||||
fileInfo.setContentLength(req.getContentLength().longValue());
|
||||
fileInfo.setDownloadName(req.getDownloadName());
|
||||
|
||||
fileInfo.setContentType(StringUtils.defaultIfBlank(req.getContentType(),
|
||||
MimeTypeUtils.APPLICATION_OCTET_STREAM_VALUE));
|
||||
fileInfo.setFormat(fileFormat);
|
||||
|
||||
@ -134,29 +140,24 @@ public class DiskFileSupport
|
||||
|
||||
// File
|
||||
log.info("Upload input stream with name [{}].", fullName);
|
||||
OutputStream fout = null;
|
||||
try {
|
||||
|
||||
final File storeFile = new File(getFileDir(), filePath);
|
||||
final File fileParent = storeFile.getParentFile();
|
||||
if (!fileParent.isDirectory()) {
|
||||
fileParent.mkdirs();
|
||||
}
|
||||
val storeFile = new File(getFileDir(), filePath);
|
||||
val fileParent = storeFile.getParentFile();
|
||||
if (!fileParent.isDirectory()) {
|
||||
fileParent.mkdirs();
|
||||
}
|
||||
|
||||
fout = new FileOutputStream(storeFile);
|
||||
try (val in = req.getInputStream();
|
||||
val fout = new FileOutputStream(storeFile)) {
|
||||
IOUtils.copy(in, fout);
|
||||
// return basePath + fullName;
|
||||
fileInfo.setUrl(basePath + filePath);
|
||||
return fileInfo;
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException(
|
||||
"Upload file [" + fullName + "] error caused", e);
|
||||
}
|
||||
finally {
|
||||
IOUtils.closeQuietly(fout);
|
||||
IOUtils.closeQuietly(in);
|
||||
"Upload file [" + fullName + "] error caused", e);
|
||||
}
|
||||
// return basePath + fullName;
|
||||
fileInfo.setUrl(basePath + filePath);
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -164,7 +165,7 @@ public class DiskFileSupport
|
||||
*/
|
||||
@Override
|
||||
public File getFile(final String name) {
|
||||
final FileInfo fileInfo = findFileInfo(name);
|
||||
val fileInfo = findFileInfo(name);
|
||||
return fileInfo != null ? getFile(fileInfo): null;
|
||||
}
|
||||
|
||||
@ -172,7 +173,7 @@ public class DiskFileSupport
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public long getContentLength(String name) {
|
||||
public long getContentLength(final String name) {
|
||||
return getFile(name).length();
|
||||
}
|
||||
|
||||
@ -180,23 +181,16 @@ public class DiskFileSupport
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String getUrl(String folder, String name) {
|
||||
String fileFullName = null;
|
||||
if (StringUtils.isNotBlank(folder)) {
|
||||
if (!folder.endsWith("/")) {
|
||||
folder += "/";
|
||||
}
|
||||
fileFullName = folder + name;
|
||||
public String getUrl(final String folder, String name) {
|
||||
val fileFullName = prependFolder(folder, name);
|
||||
|
||||
val fileInfo = findFileInfo(fileFullName);
|
||||
if (fileInfo != null) {
|
||||
return basePath + fileInfo.getFilePath();
|
||||
}
|
||||
else {
|
||||
fileFullName = name;
|
||||
}
|
||||
final FileInfo fileInfo = findFileInfo(fileFullName);
|
||||
if (fileInfo == null) {
|
||||
log.error("No File [{}] Found.", fileFullName);
|
||||
return basePath + fileFullName;
|
||||
}
|
||||
return basePath + fileInfo.getFilePath();
|
||||
|
||||
log.error("No File [{}] Found.", fileFullName);
|
||||
return basePath + fileFullName;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -227,8 +221,8 @@ public class DiskFileSupport
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void deleteById(String id) {
|
||||
final FileInfo fileInfo = fileInfoMapper.find(id);
|
||||
public void deleteById(final String id) {
|
||||
val fileInfo = fileInfoMapper.find(id);
|
||||
if (fileInfo != null) {
|
||||
log.info("Delete file [{}].", fileInfo);
|
||||
fileInfoMapper.delete(id);
|
||||
@ -240,54 +234,35 @@ public class DiskFileSupport
|
||||
// private methods
|
||||
|
||||
File getFileDir() {
|
||||
File baseFileDir = new File(baseDir);
|
||||
val baseFileDir = new File(baseDir);
|
||||
if (!baseFileDir.exists()) {
|
||||
baseFileDir.mkdirs();
|
||||
}
|
||||
return baseFileDir;
|
||||
}
|
||||
|
||||
String getFileFullName(String fileId, String folder, String name) {
|
||||
if (StringUtils.isNotBlank(folder)) {
|
||||
if (!folder.endsWith("/")) {
|
||||
folder += "/";
|
||||
}
|
||||
}
|
||||
String fullName = null;
|
||||
String getFileFullName(final String fileId,
|
||||
final String folder,
|
||||
final String name) {
|
||||
|
||||
if (StringUtils.isNotBlank(name)) {
|
||||
log.debug("Name [{}] is not blank.", name);
|
||||
if (name.startsWith(basePath)) {
|
||||
log.debug("Name starts with base path [{}], trim.", basePath);
|
||||
fullName = name.replaceFirst(basePath, "");
|
||||
}
|
||||
else if (StringUtils.isNotBlank(folder)) {
|
||||
fullName = folder + name;
|
||||
}
|
||||
else {
|
||||
fullName = name;
|
||||
return name.replaceFirst(basePath, "");
|
||||
}
|
||||
}
|
||||
// name is blank, folder is not blank
|
||||
else if (StringUtils.isNotBlank(folder)) {
|
||||
fullName = folder + fileId;
|
||||
}
|
||||
// name and folder are blank
|
||||
else {
|
||||
fullName = fileId;
|
||||
}
|
||||
return fullName;
|
||||
|
||||
return prependFolder(folder, fileId);
|
||||
}
|
||||
|
||||
String getFilePath(final String fileId, final String folder, final String name, String fileFormat) {
|
||||
String distFolder = DateFormatUtils.format(new Date(), "yyyyMMdd") + "/";
|
||||
if (StringUtils.isNotBlank(folder)) {
|
||||
if (folder.endsWith("/")) {
|
||||
distFolder = folder + distFolder;
|
||||
}
|
||||
else {
|
||||
distFolder = folder + "/" + distFolder;
|
||||
}
|
||||
}
|
||||
String getFilePath(final String fileId,
|
||||
final String folder,
|
||||
final String name,
|
||||
final String fileFormat) {
|
||||
|
||||
val distFolder = prependFolder(folder,
|
||||
DateFormatUtils.format(new Date(), "yyyyMMdd") + "/");
|
||||
|
||||
String filePath = null;
|
||||
if (StringUtils.isNotBlank(name)) {
|
||||
@ -307,4 +282,14 @@ public class DiskFileSupport
|
||||
return StringUtils.isNotBlank(fileFormat) ?
|
||||
filePath + "." + fileFormat : filePath;
|
||||
}
|
||||
|
||||
String prependFolder(final String folder, final String file) {
|
||||
if (StringUtils.isNotBlank(folder)) {
|
||||
if (folder.endsWith("/")) {
|
||||
return folder + file;
|
||||
}
|
||||
return folder + "/" + file;
|
||||
}
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
package com.pudonghot.ambition.crm.enumeration;
|
||||
|
||||
/**
|
||||
* @author Donghuang
|
||||
* @date May 29, 2022 11:58:01
|
||||
*/
|
||||
public enum EnumExportTaskStatus {
|
||||
RUNNING,
|
||||
DONE,
|
||||
FAILED
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.pudonghot.ambition.crm.enumeration;
|
||||
|
||||
/**
|
||||
* @author Donghuang
|
||||
* @date May 29, 2022 11:58:01
|
||||
*/
|
||||
public enum EnumGender {
|
||||
M,
|
||||
F
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.pudonghot.ambition.crm.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import java.util.Date;
|
||||
import java.io.Serializable;
|
||||
import me.chyxion.tigon.mybatis.NotUpdate;
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
|
||||
/**
|
||||
* @author Shaun Chyxion <br>
|
||||
* chyxion@163.com <br>
|
||||
* Aug 09, 2017 22:24:40
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@FieldNameConstants(prefix = "")
|
||||
public class BaseDbModel implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Long id;
|
||||
@NotUpdate
|
||||
private String createdBy;
|
||||
@NotUpdate
|
||||
private Date dateCreated;
|
||||
private String updatedBy;
|
||||
private Date dateUpdated;
|
||||
private boolean enabled;
|
||||
private String note;
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.pudonghot.ambition.crm.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import me.chyxion.tigon.mybatis.Table;
|
||||
import me.chyxion.tigon.mybatis.NotUpdate;
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
import com.pudonghot.ambition.crm.enumeration.EnumExportTaskStatus;
|
||||
|
||||
/**
|
||||
* @author Donghuang
|
||||
* @date May 29, 2022 11:55:30
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Table("crm_export_task")
|
||||
@FieldNameConstants(prefix = "")
|
||||
public class ExportTask extends BaseDbModel {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// Properties
|
||||
private EnumExportTaskStatus status;
|
||||
@NotUpdate
|
||||
private String location;
|
||||
}
|
@ -26,7 +26,7 @@ public class FileInfo extends M3<String, String> {
|
||||
private String filePath;
|
||||
private String format;
|
||||
private String downloadName;
|
||||
private long contentLength;
|
||||
private Long contentLength;
|
||||
private String contentType;
|
||||
@Transient
|
||||
private String url;
|
||||
|
Loading…
x
Reference in New Issue
Block a user