AnsweredAssumed Answered

WebScript for uploading files: during the first upload one extra version created

Question asked by Aleksey Bykov on Jul 20, 2018
Latest reply on Jul 20, 2018 by Aleksey Bykov

To upload files to the repository, I use the following Java-backed 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;
    }
}

 

This WebScript works.

 

There is one problem: If the uploaded file does not exist in the repository, then two versions are created at once: 1.0 and 1.1. Version 1.0 has a size of 0 bytes and has no content.

 

I think version 1.0 is created here:

 

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

 

Version 1.1, perhaps, is created here, when writing the content:

 

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

 

How to prevent the creation of "empty" version 1.0?

 

I would be very grateful for the information.

 

Thanks to all.

Outcomes