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

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
alekseybykov
Established Member II

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

Jump to solution

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.

1 Solution

Accepted Solutions
afaust
Master

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

Jump to solution

What transaction mode has been set for your web script in its *.desc.xml file? I assume you forgot to set any, and as a result, your web script runs without any transaction, so that the calls to create() and putContent() each create their own versions since they are automatically transactional if no called as part of an already established transaction.

2 Replies
afaust
Master

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

Jump to solution

What transaction mode has been set for your web script in its *.desc.xml file? I assume you forgot to set any, and as a result, your web script runs without any transaction, so that the calls to create() and putContent() each create their own versions since they are automatically transactional if no called as part of an already established transaction.

alekseybykov
Established Member II

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

Jump to solution

Thank you very much for your answer, Axel Faust!

Yes, you are right, in my descriptor I specify the following:

<transaction>none</transaction>

I'll try to set "requiresnew" and let you know.

Kind regards,
Aleksey Bykov