gpt4 book ai didi

java - 用于上传文件的WebScript : during the first upload one extra version created

转载 作者:行者123 更新时间:2023-12-01 17:49:59 27 4
gpt4 key购买 nike

要将文件上传到存储库,我使用以下 Java 支持的 WebScript:

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;
}
}

这个 WebScript 可以工作。

有一个问题:如果存储库中不存在上传的文件,则会同时创建两个版本:1.01.1。版本 1.0 的大小为 0 字节,并且没有内容。

我认为版本 1.0 是在这里创建的:

FileInfo fileInfo = fileFolderService.create(parentNode, fileName, contentQName); 

版本1.1,也许是在编写内容时在这里创建的:

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

如何防止创建“空”版本1.0

最佳答案

Axel Faust对这个问题给出了很好的答案here

问题出在fileuploader.post.desc.xml描述符中,我没有设置transaction level运行网络脚本所需的内容:

<transaction>none</transaction>

因此,方法 writeContent()createFileNode() 默认情况下是事务性的,并且每个方法都创建自己的内容版本。

我设置了以下内容:

<transaction>requiresnew</transaction>

问题已解决。谢谢@Axel Faust !

关于java - 用于上传文件的WebScript : during the first upload one extra version created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51435938/

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