|
|
@@ -18,6 +18,7 @@ import org.apache.commons.io.IOUtils;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
+import java.nio.file.FileAlreadyExistsException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
@@ -97,75 +98,66 @@ public class MdDocsService implements IMdDocsService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Map<String, Object> processAsyncDirect(String coll, String taskId, MultipartFile file, Map<String, String> params) {
|
|
|
+ public Map<String, Object> processAsyncDirect(String coll, String taskId, MultipartFile file, Map<String, String> params) throws Throwable {
|
|
|
|
|
|
Map<String, Object> ret = new HashMap<>();
|
|
|
- try {
|
|
|
- String uri = params.get("objectPath");
|
|
|
- String original = file.getOriginalFilename();
|
|
|
- String p = normalizePath(uri);
|
|
|
- //String objectPath = getObjectPath(uri, original);
|
|
|
- String objectPath = null;
|
|
|
- if (FileUtil.isEndsWithSeparator(p)) {
|
|
|
- objectPath = p + original;
|
|
|
- } else {
|
|
|
- objectPath = p;
|
|
|
- int i1 = p.lastIndexOf("/");
|
|
|
- p = p.substring(0, i1 + 1);
|
|
|
- }
|
|
|
- int collId = mdCollectionsService.getCollectionOrNew(coll);
|
|
|
-
|
|
|
- params.put("path", p);
|
|
|
- params.put("name", objectPath);
|
|
|
- //TaskStatus status = new TaskStatus(coll, taskId, "成功", "处理完成", System.currentTimeMillis(), System.currentTimeMillis());
|
|
|
- //taskStatusManager.markSuccess(taskId, status);
|
|
|
- ret.put("taskId", taskId);
|
|
|
- ret.put("uploadFile", original);
|
|
|
- //List<GBaseKeyValue> kvs = mdCollectionsService.findByName(coll, objectPath);
|
|
|
- //List<GBaseKeyValue> kvs = collection.findByName(objectPath);
|
|
|
|
|
|
- List<GBaseKeyValue> kvs = mdDynamicRepository.findByPath(Integer.toString(collId), objectPath);
|
|
|
- boolean isNewFile = true;
|
|
|
-
|
|
|
- if (kvs != null && kvs.size() > 0) {
|
|
|
- isNewFile = false;
|
|
|
- ret.put("gid", kvs.get(0).getGid());
|
|
|
+ String uri = params.get("objectPath");
|
|
|
+ String original = file.getOriginalFilename();
|
|
|
+ String p = normalizePath(uri);
|
|
|
+ //String objectPath = getObjectPath(uri, original);
|
|
|
+ String objectPath = null;
|
|
|
+ if (FileUtil.isEndsWithSeparator(p)) {
|
|
|
+ objectPath = p + original;
|
|
|
+ } else {
|
|
|
+ objectPath = p;
|
|
|
+ int i1 = p.lastIndexOf("/");
|
|
|
+ p = p.substring(0, i1 + 1);
|
|
|
+ }
|
|
|
+ int collId = mdCollectionsService.getCollectionOrNew(coll);
|
|
|
+
|
|
|
+ params.put("path", p);
|
|
|
+ params.put("name", objectPath);
|
|
|
+ //TaskStatus status = new TaskStatus(coll, taskId, "成功", "处理完成", System.currentTimeMillis(), System.currentTimeMillis());
|
|
|
+ //taskStatusManager.markSuccess(taskId, status);
|
|
|
+ ret.put("taskId", taskId);
|
|
|
+ ret.put("uploadFile", original);
|
|
|
+
|
|
|
+ //List<GBaseKeyValue> kvs = mdDynamicRepository.findByPath(Integer.toString(collId), objectPath);
|
|
|
+ List<GBaseKeyValue> kvs = mdDynamicRepository.findByName(Integer.toString(collId), objectPath);
|
|
|
+ boolean isNewFile = true;
|
|
|
+
|
|
|
+ if (kvs != null && kvs.size() > 0) {
|
|
|
+ isNewFile = false;
|
|
|
+ ret.put("gid", kvs.get(0).getGid());
|
|
|
+ log.info("任务 {} 的objectPath {} 已存在,metadata = {}", taskId, objectPath, kvs.get(0));
|
|
|
+ throw new FileAlreadyExistsException("文件已存在,请更换文件名");
|
|
|
+ } else {
|
|
|
+ ret.put("gid", taskId);
|
|
|
+ }
|
|
|
|
|
|
- log.info("任务 {} 的objectPath {} 已存在,metadata = {}", taskId, objectPath, kvs.get(0));
|
|
|
- } else {
|
|
|
- ret.put("gid", taskId);
|
|
|
- }
|
|
|
+ backupAndStore(coll, collId, taskId, file, params, isNewFile);
|
|
|
|
|
|
- backupAndStore(coll, collId, taskId, file, params, isNewFile);
|
|
|
- } catch (Throwable e) {
|
|
|
- log.error("任务 {} 失败", taskId, e);
|
|
|
- }
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
@Async
|
|
|
- public void backupAndStore(String coll, int collId, String taskId, MultipartFile file, Map<String, String> params, boolean isNewFile) {
|
|
|
- try {
|
|
|
- String uri = params.get("objectPath");
|
|
|
- String original = file.getOriginalFilename();
|
|
|
- //String objectPath = gkbStorer.backupAndStore(file.getInputStream(), coll, original, uri);
|
|
|
- String objectPath = gkbStorer.storeDirect(file.getInputStream(), coll, original, uri);
|
|
|
- if (isNewFile) {
|
|
|
- //params.put("name", objectPath);
|
|
|
- params.put("gid", taskId);
|
|
|
- //String objectPath2 = getObjectPath(uri, original);
|
|
|
- //params.put("name", objectPath2);
|
|
|
-
|
|
|
- //mdCollectionsService.createEntry(coll, toObjectMap(params));
|
|
|
- Map<String, Object> data = toObjectMap(params);
|
|
|
- mdDynamicRepository.save(Integer.toString(collId), GBaseKeyValue.build(data));
|
|
|
- }
|
|
|
- log.info("任务 {} 成功", taskId);
|
|
|
- } catch (Throwable e) {
|
|
|
- //TaskStatus status = new TaskStatus(coll, taskId, "失败", e.getMessage(), System.currentTimeMillis(), System.currentTimeMillis());
|
|
|
- //taskStatusManager.markFailed(taskId, status);
|
|
|
- log.error("任务 {} 失败", taskId, e);
|
|
|
- }
|
|
|
+ public void backupAndStore(String coll, int collId, String taskId, MultipartFile file, Map<String, String> params, boolean isNewFile) throws Throwable {
|
|
|
+
|
|
|
+ String uri = params.get("objectPath");
|
|
|
+ String original = file.getOriginalFilename();
|
|
|
+ //String objectPath = gkbStorer.backupAndStore(file.getInputStream(), coll, original, uri);
|
|
|
+ String objectPath = gkbStorer.storeDirect(file.getInputStream(), coll, original, uri);
|
|
|
+ //if (isNewFile) {
|
|
|
+ params.put("gid", taskId);
|
|
|
+ //String objectPath2 = getObjectPath(uri, original);
|
|
|
+ //params.put("name", objectPath2);
|
|
|
+
|
|
|
+ Map<String, Object> data = toObjectMap(params);
|
|
|
+ mdDynamicRepository.save(Integer.toString(collId), GBaseKeyValue.build(data));
|
|
|
+ //}
|
|
|
+ log.info("任务 {} 成功", taskId);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
private Map<String, Object> toObjectMap(Map<String, String> params) {
|
|
|
@@ -472,6 +464,16 @@ public class MdDocsService implements IMdDocsService {
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public GBaseKeyValue findByName(String coll, String name) throws Throwable {
|
|
|
+ String collId = getStrOfCollId(coll);
|
|
|
+ List<GBaseKeyValue> rs = mdDynamicRepository.findByName(collId, name);
|
|
|
+ if (rs != null && rs.size() > 0) {
|
|
|
+ return rs.get(0);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Object getMetadataByKey(String coll, String mdId, String key) throws Throwable {
|
|
|
GBaseKeyValue kv = findByMdid(coll, mdId);
|
|
|
@@ -615,7 +617,7 @@ public class MdDocsService implements IMdDocsService {
|
|
|
List<String> uids = mdDynamicChunkRepository.findUidsByMdId(collId, mdId);
|
|
|
if (uids != null && !uids.isEmpty()) {
|
|
|
//hybridSearch.delete(coll, uids);
|
|
|
- mdIndexer.delete(coll,uids);
|
|
|
+ mdIndexer.delete(coll, uids);
|
|
|
log.info("删除索引文件,mdid: {}", gid);
|
|
|
long r = mdDynamicChunkRepository.deleteByMdId(collId, mdId);
|
|
|
log.info("删除chunk文件,mdid: {}", gid);
|
|
|
@@ -636,7 +638,7 @@ public class MdDocsService implements IMdDocsService {
|
|
|
List<String> uids = mdDynamicChunkRepository.findUidsByMdId(collId, mdId);
|
|
|
if (uids != null && !uids.isEmpty()) {
|
|
|
//hybridSearch.delete(coll, uids);
|
|
|
- mdIndexer.delete(coll,uids);
|
|
|
+ mdIndexer.delete(coll, uids);
|
|
|
log.info("删除索引文件,mdid: {}", gid);
|
|
|
//long r = mdDynamicChunkRepository.deleteByMdId(collId, mdId);
|
|
|
//log.info("删除chunk文件: {}", gid);
|