AnsweredAssumed Answered

How to upload a new version of a file by using WebScript?

Question asked by Aleksey Bykov on Jul 9, 2018
Latest reply on Jul 10, 2018 by Aleksey Bykov

I have a simple Spring application (front-end application) that loads files into the Alfresco repository. If the file already exists, its new version is not created.

 

Repository web script is presented below:

 

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

 

How to create a new version of a file with the same name by using Java-backed WebScript?

 

Does this solution correct?

 

Check if the file exists by using Lucene search: TYPE:"firm:doc" AND @cm\:name:contract.png; (for example) If exists, increment the property cm:versionLabel and create a new version of Node with all the properties (Actually, need to iterate through all the ResultSet and find NodeRef with max value of cm:versionLabel then increment it and create a new Node). Is there more elegant solution?

 

I would be very grateful for the information.

 

Thanks to all.

Outcomes