gpt4 book ai didi

java - 如何使用 WebScript 上传文件的新版本?

转载 作者:行者123 更新时间:2023-12-02 02:03:13 26 4
gpt4 key购买 nike

我有一个简单的 Spring 应用程序(前端应用程序),它将文件加载到 Alfresco 存储库中。如果该文件已存在,则不会创建其新版本。

Repository web script现介绍如下:

public class CustomFileUploader extends DeclarativeWebScript {

private static final String FIRM_DOC =
"{http://www.firm.com/model/content/1.0}someDoc";
private static final String FIRM_DOC_FOLDER =
"workspace://SpacesStore/8caf07c3-6aa9-4a41-bd63-404cb3e3ef0f";

private FirmFile firmFile;

private FileFolderService fileFolderService;
private ContentService contentService;

protected Map<String, Object> executeImpl(WebScriptRequest req,
Status status) {
retrievePostRequestParams(req);
writeContent();

return null;
}

private void retrievePostRequestParams(WebScriptRequest req) {
FormData formData = (FormData) req.parseContent();
FormData.FormField[] fields = formData.getFields();

for(FormData.FormField field : fields) {
String fieldName = field.getName();
String fieldValue = field.getValue();

if(fieldName.equalsIgnoreCase("firm_file")
&& field.getIsFile()) {
String fileName = field.getFilename();
Content fileContent = field.getContent();
String fileMimetype = field.getMimetype();

firmFile = new FirmFile(fileName, fileContent,
fileMimetype, FIRM_DOC);
}
}
}

private void writeContent() {
try {
NodeRef parentNodeRef = new NodeRef(FIRM_DOC_FOLDER);
NodeRef fileNodeRef = createFileNode(parentNodeRef,
firmFile.getFileName());

ContentWriter contentWriter = contentService.getWriter(fileNodeRef,
ContentModel.PROP_CONTENT, true);
contentWriter.setMimetype(firmFile.getFileMimetype());
contentWriter.putContent(firmFile.getFileContent().getInputStream());

} catch (Exception e) {
e.printStackTrace();
}
}

private NodeRef createFileNode(NodeRef parentNode, String fileName) {
try {
QName contentQName = QName.createQName(FIRM_DOC);
FileInfo fileInfo = fileFolderService.create(parentNode,
fileName, contentQName);

return fileInfo.getNodeRef();

} catch (Exception e) {
e.printStackTrace();
}
}

public FileFolderService getFileFolderService() {
return fileFolderService;
}

public void setFileFolderService(FileFolderService fileFolderService) {
this.fileFolderService = fileFolderService;
}

public ContentService getContentService() {
return contentService;
}

public void setContentService(ContentService contentService) {
this.contentService = contentService;
}
}

如何使用 Java 支持的 WebScript 创建同名文件的新版本?

这个解决方案正确吗?

使用 Lucene 搜索检查文件是否存在:TYPE:"firm:doc"AND @cm\:name:contract.png;(例如)如果存在,则增加属性 cm:versionLabel 并创建一个具有所有属性的新版本的 Node (实际上,需要遍历所有 ResultSet 并找到 NodeRef 的最大值为 cm:versionLabel,然后递增它并创建一个新的 Node)。有更优雅的解决方案吗?

最佳答案

解决方案可以表示如下:

public class CustomFileUploader extends DeclarativeWebScript {
private static final String FIRM_DOC = "{http://www.firm.com/model/content/1.0}doc";
private static final String FIRM_DOC_FOLDER = "workspace://SpacesStore/8caf07c3-6aa9-4a41-bd63-404cb3e3ef0f";

private FileFolderService fileFolderService;
private ContentService contentService;
private NodeService nodeService;
private SearchService searchService;

protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {
processUpload(req);
return null;
}

private void writeContent(NodeRef node, FirmFile firmFile) {
try {
ContentWriter contentWriter = contentService.getWriter(node, ContentModel.PROP_CONTENT, true);
contentWriter.setMimetype(firmFile.getFileMimetype());
contentWriter.putContent(firmFile.getFileContent().getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
}

private NodeRef checkIfNodeExists(String fileName) {
StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE/*LANGUAGE_FTS_ALFRESCO*/,
"TYPE:\"firm:doc\" AND @cm\\:name:" + fileName.replaceAll(" ", "\\ ")+ "");

int len = resultSet.length();
if(len == 0) {
return null;
}

NodeRef node = resultSet.getNodeRef(0);
return node;
}

private NodeRef createNewNode(FirmFile firmFile) {
NodeRef parent = new NodeRef(FIRM_DOC_FOLDER);
NodeRef node = createFileNode(parent, firmFile.getFileName());

return node;
}

private void processUpload(WebScriptRequest req) {
FormData formData = (FormData) req.parseContent();
FormData.FormField[] fields = formData.getFields();

for(FormData.FormField field : fields) {
String fieldName = field.getName();
if(fieldName.equalsIgnoreCase("firm_file") && field.getIsFile()) {
String fileName = field.getFilename();
Content fileContent = field.getContent();
String fileMimetype = field.getMimetype();

NodeRef node = checkIfNodeExists(fileName);

// POJO
FirmFile firm = new FirmFile(fileName, fileContent, fileMimetype, FIRM_DOC);
if(node == null) {
node = createNewNode(firmFile);
}

writeContent(node, firmFile);
}
}
}

private NodeRef createFileNode(NodeRef parentNode, String fileName) {
try {
QName contentQName = QName.createQName(FIRM_DOC);
FileInfo fileInfo = fileFolderService.create(parentNode, fileName, contentQName);
return fileInfo.getNodeRef();
} catch (Exception e) {
e.printStackTrace();
}
}

public FileFolderService getFileFolderService() {
return fileFolderService;
}

public void setFileFolderService(FileFolderService fileFolderService) {
this.fileFolderService = fileFolderService;
}

public ContentService getContentService() {
return contentService;
}

public void setContentService(ContentService contentService) {
this.contentService = contentService;
}

public NodeService getNodeService() {
return nodeService;
}

public void setNodeService(NodeService nodeService) {
this.nodeService = nodeService;
}

public SearchService getSearchService() {
return searchService;
}

public void setSearchService(SearchService searchService) {
this.searchService = searchService;
}
}

内容模型必须具有强制方面cm:versionable:

<mandatory-aspects>
<aspect>cm:versionable</aspect>
</mandatory-aspects>

关于java - 如何使用 WebScript 上传文件的新版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51245559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com