Updating Files

Updating a file is a bit more complicated than creating one. This is due to the way the update operation handles a file’s metadata and content. To update only a file’s content, you must also supply the file’s existing metadata. Otherwise, the update operation could lose the metadata. The opposite, however, isn’t true. You can modify a file’s metadata without re-supplying the content. In such an update, the file’s content is automatically copied to the new version of the file. To make this easier to remember, follow these rules when updating files:

  • Always provide all metadata.
  • Only provide the file’s content when you want to change it.

DLAppService has three updateFileEntry methods that you can use to update a file. These methods differ only in the file content’s type. Click each method to see its Javadoc, which contains a full description of its parameters:

Keep these things in mind when using these methods:

  • To retain the original file’s title and description, you must provide those parameters to updateFileEntry. Omitting them deletes any existing title and description.

  • If you supply null in place of the file’s content (e.g., bytes, file, or is), the update automatically uses the file’s existing content. Do this only if you want to update the file’s metadata.

  • If you use false for the majorVersion parameter, the update increments the file version by 0.1 (e.g., from 1.0 to 1.1). If you use true for this parameter, the update increments the file version to the next .0 value (e.g., from 1.0 to 2.0, 1.1 to 2.0, etc.).

Follow these steps to update a file. Note that the example in these steps uses the updateFileEntry method that contains InputStream, but you can adapt the example to the other methods if you wish:

  1. Get a reference to DLAppService:

    @Reference
    private DLAppService _dlAppService;
    

    For more information on this, see the section on getting a service reference in the getting started tutorial.

  2. Get the data needed to populate the updateFileEntry method’s arguments. Since it’s common to update a file with data submitted by the end user, you can extract the data from the request. This example does so via UploadPortletRequest and ParamUtil, but you can get the data any way you wish:

    long repositoryId = ParamUtil.getLong(uploadPortletRequest, "repositoryId");
    long folderId = ParamUtil.getLong(uploadPortletRequest, "folderId");
    String sourceFileName = uploadPortletRequest.getFileName("file");
    String title = ParamUtil.getString(uploadPortletRequest, "title");
    String description = ParamUtil.getString(uploadPortletRequest, "description");
    String changeLog = ParamUtil.getString(uploadPortletRequest, "changeLog");
    boolean majorVersion = ParamUtil.getBoolean(uploadPortletRequest, "majorVersion");
    
    try (InputStream inputStream = uploadPortletRequest.getFileAsStream("file")) {
    
        String contentType = uploadPortletRequest.getContentType("file");
        long size = uploadPortletRequest.getSize("file");
    
        ServiceContext serviceContext = ServiceContextFactory.getInstance(
                DLFileEntry.class.getName(), uploadPortletRequest);
    }
    

    For more information on getting repository and folder IDs, see the getting started tutorial’s sections on specifying repositories and folders. For more information on ServiceContext, see the tutorial Understanding ServiceContext.

  3. Call the service reference’s updateFileEntry method with the data from the previous step. Note that this example does so inside the previous step’s try-with-resources statement:

    try (InputStream inputStream = uploadPortletRequest.getFileAsStream("file")) {
    
        ...
    
        FileEntry fileEntry = _dlAppService.updateFileEntry(
                                fileEntryId, sourceFileName, contentType, title,
                                description, changeLog, majorVersion, inputStream, size,
                                serviceContext);
    }
    

    The method returns a FileEntry object, which this example sets to a variable for later use. Note, however, that you don’t have to do this.

You can find the full code for this example in the updateFileEntry method of Liferay DXP’s EditFileEntryMVCActionCommand class. This class uses the Documents and Media API to implement almost all the FileEntry actions that the Documents and Media app supports. Also note that this updateFileEntry method, as well as the rest of EditFileEntryMVCActionCommand, contains additional logic to suit the specific needs of the Documents and Media app.

Creating Files

Deleting Files

Moving Folders and Files

Este artigo foi útil?
Utilizadores que acharam útil: 0 de 0