infoType, infoKey, subType -> FileInfo.Relation

master
mjkhan21 2 years ago
parent e67f4f0984
commit 641d3dedcf

@ -5,6 +5,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
@ -24,64 +25,119 @@ import cokr.xit.foundation.Assert;
* @author mjkhan
*/
public class FileInfo {
/**objs (-) infoKey .
* @param objs
* @return infoKey
/**
* @author mjkhan
*/
public static String InfoKey(Object... objs) {
if (Assert.isEmpty(objs))
return "";
public static class Relation {
private String infoType;
private String infoKey;
private String subType;
/**objs (-) infoKey .
* @param objs
* @return infoKey
*/
public static String InfoKey(Object... objs) {
if (Assert.isEmpty(objs))
return "";
return Stream.of(objs)
.filter(obj -> !Assert.isEmpty(obj))
.map(obj -> obj.toString())
.collect(Collectors.joining("-"));
}
/** .
* @return
*/
public String getInfoType() {
return infoType;
}
/** .
* @param infoType
* @return Relation
*/
public Relation setInfoType(String infoType) {
this.infoType = infoType;
return this;
}
/** .
* @return
*/
public String getInfoKey() {
return infoKey;
}
/** .
* @param infoType
* @return Relation
*/
public Relation setInfoKey(String infoKey) {
this.infoKey = infoKey;
return this;
}
/** .
* @return
*/
public String getSubType() {
return subType;
}
/** .
* @param infoType
* @return Relation
*/
public Relation setSubType(String subType) {
this.subType = subType;
return this;
}
return Stream.of(objs)
.filter(obj -> !Assert.isEmpty(obj))
.map(obj -> obj.toString())
.collect(Collectors.joining("-"));
/** , , fileInfo .
* @param fileInfo FileInfo
*/
public void setInfo(FileInfo fileInfo) {
fileInfo.setInfoType(Assert.notEmpty(infoType, "infoType"));
fileInfo.setInfoKey(Assert.notEmpty(infoKey, "infoKey"));
fileInfo.setSubType(subType);
}
}
/**
* @author mjkhan
*/
public static class DataHolder {
private String infoType;
private String infoKey;
private Relation relation;
private String filename;
private byte[] data;
/** DataHolder .
* @param infoType
* @param infoKey
* @param relation
* @param filename
* @param data
*/
public DataHolder(String infoType, String infoKey, String filename, byte[] data) {
this.infoType = infoType;
this.infoKey = infoKey;
public DataHolder(Relation relation, String filename, byte[] data) {
this.relation = relation;
this.filename = filename;
this.data = data != null ? data : new byte[0];
}
/** DataHolder .
* @param infoType
* @param infoKey
* @param relation
* @param filename
* @param data
*/
public DataHolder(String infoType, String infoKey, String filename, String data) {
this(infoType, infoKey, filename, !Assert.isEmpty(data) ? Base64.getDecoder().decode(data.getBytes()) : null);
public DataHolder(Relation relation, String filename, String data) {
this(relation, filename, !Assert.isEmpty(data) ? Base64.getDecoder().decode(data.getBytes()) : null);
}
/** .
* @return
/** .
* @return
*/
public String getInfoType() {
return infoType;
}
/** .
* @return
*/
public String getInfoKey() {
return infoKey;
public Relation getRelation() {
return relation;
}
/** .
@ -121,14 +177,11 @@ public class FileInfo {
*/
public static class Factory extends AbstractComponent {
/** , FileInfo .
* @param infoType
* @param infoKey
* @param relation
* @param files
* @return FileInfo
*/
public List<FileInfo> createFileInfos(String infoType, String infoKey, Iterable<File> files) {
notEmpty(infoType, "infoType");
notEmpty(infoKey, "infoKey");
public List<FileInfo> createFileInfos(Relation relation, Iterable<File> files) {
if (isEmpty(files))
return Collections.emptyList();
@ -138,8 +191,7 @@ public class FileInfo {
if (file == null) continue;
FileInfo info = new FileInfo();
info.setInfoType(infoType);
info.setInfoKey(infoKey);
relation.setInfo(info);
String filename = file.getName();
info.setName(filename);
InputStream input = new FileInputStream(file);
@ -158,23 +210,21 @@ public class FileInfo {
}
/** , FileInfo .
* @param infoType
* @param infoKey
* @param relation
* @param files
* @return FileInfo
*/
public List<FileInfo> createFileInfos(String infoType, String infoKey, File... files) {
return createFileInfos(infoType, infoKey, Arrays.asList(files));
public List<FileInfo> createFileInfos(Relation relation, File... files) {
return createFileInfos(relation, Arrays.asList(files));
}
/** , FileInfo .
* @param infoType
* @param infoKey
* @param relation
* @param file
* @return FileInfo
*/
public FileInfo create(String infoType, String infoKey, File file) {
List<FileInfo> fileInfos = createFileInfos(infoType, infoKey, file);
public FileInfo create(Relation relation, File file) {
List<FileInfo> fileInfos = createFileInfos(relation, file);
return !fileInfos.isEmpty() ? fileInfos.get(0) : null;
}
@ -191,8 +241,7 @@ public class FileInfo {
if (holder == null) continue;
FileInfo info = new FileInfo();
info.setInfoType(holder.infoType);
info.setInfoKey(holder.infoKey);
holder.getRelation().setInfo(info);
String filename = holder.getFilename();
info.setName(filename);
InputStream input = new ByteArrayInputStream(holder.data);
@ -327,9 +376,11 @@ public class FileInfo {
/** .
* @param subType
* @return FileInfo
*/
public void setSubType(String subType) {
public FileInfo setSubType(String subType) {
this.subType = subType;
return this;
}
/** .
@ -530,18 +581,18 @@ public class FileInfo {
return this;
}
/**FileInfo File .
/**OutputStream File .
* @param out OutputStream
* @return
* <ul><li> true</li>
* <li> false</li>
* </ul>
*/
public boolean write() {
if (Assert.isEmpty(path)) return false;
public boolean write(OutputStream out) {
if (Assert.isEmpty(path) || out == null) return false;
try (
InputStream in = input;
FileOutputStream out = new FileOutputStream(path);
) {
FileCopyUtils.copy(in, out);
return true;
@ -550,6 +601,20 @@ public class FileInfo {
}
}
/**FileInfo File .
* @return
* <ul><li> true</li>
* <li> false</li>
* </ul>
*/
public boolean write() {
try (FileOutputStream out = new FileOutputStream(path)) {
return write(out);
} catch(Exception e) {
throw Assert.runtimeException(e);
}
}
/**FileInfo File .
* @return
* <ul><li> true</li>

@ -51,6 +51,7 @@ public class FileController extends AbstractController {
/** .
* @param infoType
* @param infoKey
* @param infoKey
* @param uploads
* @return ModelAndView
* <pre><code>{
@ -62,9 +63,13 @@ public class FileController extends AbstractController {
public ModelAndView create(
String infoType,
String infoKey,
String subType,
MultipartFile[] uploads
) {
List<FileInfo> files = new FileInfoFactory().makeFileInfos(infoType, infoKey, uploads);
List<FileInfo> files = new FileInfoFactory().makeFileInfos(
new FileInfo.Relation().setInfoType(infoType).setInfoKey(infoKey).setSubType(subType),
uploads
);
int affected = fileService.create(files);
return new ModelAndView("jsonView")
.addObject("affected", affected)

@ -14,12 +14,11 @@ import cokr.xit.base.file.FileInfo;
*/
public class FileInfoFactory extends FileInfo.Factory {
/** , FileInfo .
* @param infoType
* @param infoKey
* @param relation
* @param multipartFiles
* @return FileInfo
*/
public List<FileInfo> makeFileInfos(String infoType, String infoKey, Iterable<MultipartFile> multipartFiles) {
public List<FileInfo> makeFileInfos(FileInfo.Relation relation, Iterable<MultipartFile> multipartFiles) {
if (isEmpty(multipartFiles))
return Collections.emptyList();
@ -35,8 +34,7 @@ public class FileInfoFactory extends FileInfo.Factory {
info.setSize(file.getSize());
info.setInputStream(file.getInputStream());
info.setSortOrder(result.size());
info.setInfoType(infoType);
info.setInfoKey(infoKey);
relation.setInfo(info);
result.add(info);
} catch (Exception e) {
throw runtimeException(e);
@ -45,23 +43,21 @@ public class FileInfoFactory extends FileInfo.Factory {
}
/** , FileInfo .
* @param infoType
* @param infoKey
* @param relation
* @param multipartFiles
* @return FileInfo
*/
public List<FileInfo> makeFileInfos(String infoType, String infoKey, MultipartFile... multipartFiles) {
return makeFileInfos(infoType, infoKey, Arrays.asList(multipartFiles));
public List<FileInfo> makeFileInfos(FileInfo.Relation relation, MultipartFile... multipartFiles) {
return makeFileInfos(relation, Arrays.asList(multipartFiles));
}
/** , FileInfo .
* @param infoType
* @param infoKey
* @param relation
* @param multipartFile
* @return FileInfo
*/
public FileInfo make(String infoType, String infoKey, MultipartFile multipartFile) {
List<FileInfo> files = makeFileInfos(infoType, infoKey, multipartFile);
public FileInfo make(FileInfo.Relation relation, MultipartFile multipartFile) {
List<FileInfo> files = makeFileInfos(relation, multipartFile);
return !files.isEmpty() ? files.get(0) : null;
}
}

@ -27,8 +27,9 @@ public class FileServiceTest extends TestSupport {
String infoType = "00";
String infoKey = "info-key-00";
FileQuery req = new FileQuery().setInfoType(infoType);
FileInfo.Relation relation = new FileInfo.Relation().setInfoType(infoType).setInfoKey(infoKey);
List<FileInfo> fileInfos = new FileInfo.Factory().createFileInfos(infoType, infoKey, files);
List<FileInfo> fileInfos = new FileInfo.Factory().createFileInfos(relation, files);
Assertions.assertTrue(service.create(fileInfos) > 0);
fileInfos.forEach(System.out::println);

@ -20,7 +20,7 @@
</resultMap>
<sql id="fileDirs">SELECT INF_TYPE
, CONCAT('D://workspace/xit/base/files/', DIR, TO_CHAR(CURRENT_DATE, 'YYYY/MM/DD/')) DIR
, CONCAT('C://workspace/xit/base/files/', DIR, TO_CHAR(CURRENT_DATE, 'YYYY/MM/DD/')) DIR
FROM (
SELECT '00' INF_TYPE, 'attachment/' DIR UNION
SELECT '01' INF_TYPE, 'document/' DIR UNION
@ -29,12 +29,12 @@
<sql id="selectFiles">
<if test="fileIDs != null">
SELECT A.*, REPLACE(FILE_PATH, 'D://workspace/xit/base', '') URL
SELECT A.*, REPLACE(FILE_PATH, 'C://workspace/xit/base', '') URL
FROM TBL_FILE A
WHERE FILE_ID IN (<foreach collection="fileIDs" item="fileID" separator=",">#{fileID}</foreach>)
ORDER BY FILE_ID</if>
<if test="fileIDs == null"><include refid="utility.paging-prefix" />
SELECT A.*, REPLACE(FILE_PATH, 'D://workspace/xit/base', '') URL
SELECT A.*, REPLACE(FILE_PATH, 'C://workspace/xit/base', '') URL
FROM TBL_FILE A
<where>
<if test="infoType != null"> AND A.INF_TYPE = #{infoType}</if>

Loading…
Cancel
Save