View Javadoc
1   /**
2    * This file Copyright (c) 2020 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.resources.app.action;
35  
36  import info.magnolia.jcr.util.NodeNameHelper;
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.jcr.util.PropertyUtil;
40  import info.magnolia.resourceloader.Resource;
41  import info.magnolia.resourceloader.ResourceOrigin;
42  import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
43  import info.magnolia.resources.app.data.ResourcePropertySetFactory;
44  import info.magnolia.ui.CloseHandler;
45  import info.magnolia.ui.ValueContext;
46  import info.magnolia.ui.contentapp.Datasource;
47  import info.magnolia.ui.editor.FormView;
48  import info.magnolia.ui.observation.DatasourceObservation;
49  
50  import java.io.File;
51  import java.io.IOException;
52  import java.io.InputStream;
53  import java.nio.charset.Charset;
54  
55  import javax.inject.Inject;
56  import javax.jcr.Node;
57  import javax.jcr.RepositoryException;
58  
59  import org.apache.commons.io.FileUtils;
60  import org.apache.commons.io.FilenameUtils;
61  import org.apache.commons.io.IOUtils;
62  
63  import com.machinezoo.noexception.Exceptions;
64  
65  /**
66   * Saves the uploaded file inside the selected folder.
67   */
68  public class UploadResourceAction extends AbstractAddResourceAction {
69  
70      private final FormView<Resource> form;
71      private final NodeNameHelper nodeNameHelper;
72  
73      @Inject
74      public UploadResourceAction(AddResourceActionDefinition definition, CloseHandler closeHandler,
75                                  ValueContext<Resource> valueContext, FormView<Resource> form,
76                                  Datasource<Resource> datasource,
77                                  DatasourceObservation.Manual datasourceObservation,
78                                  NodeNameHelper nodeNameHelper,
79                                  ResourceOrigin resourceOrigin) {
80          super(definition, closeHandler, valueContext, form, datasource, datasourceObservation, resourceOrigin);
81          this.form = form;
82          this.nodeNameHelper = nodeNameHelper;
83      }
84  
85      @Override
86      protected void addResource(Resource parent,  Node rootNode) {
87          try {
88              File uploadedResource = form.<File>getPropertyValue(ResourcePropertySetFactory.CONTENT).orElseThrow(RuntimeException::new);
89              Node parentNode = NodeUtil.createPath(rootNode, parent.getPath(), NodeTypes.Folder.NAME);
90              String uploadedResourceName = getUniqueResourceName(uploadedResource, parentNode);
91  
92              Node childNode = NodeUtil.createPath(parentNode, uploadedResourceName, NodeTypes.Content.NAME);
93  
94              try (InputStream stream = FileUtils.openInputStream(uploadedResource)) {
95                  if (isTextMediaType(uploadedResourceName)) {
96                      PropertyUtil.setProperty(childNode, JcrResourceOrigin.TEXT_PROPERTY, IOUtils.toString(stream, Charset.defaultCharset()));
97                  } else {
98                      Node childNodeBinary = NodeUtil.createPath(childNode, JcrResourceOrigin.BINARY_NODE_NAME, NodeTypes.Resource.NAME);
99                      createBinary(childNodeBinary, uploadedResource);
100                 }
101             }
102             NodeTypes.LastModified.update(childNode);
103             childNode.getSession().save();
104         } catch (RepositoryException | IOException e) {
105             throw new RuntimeException(e);
106         }
107     }
108 
109     private String getUniqueResourceName(File uploadedResource, Node parentNode) {
110         String uploadedResourceName = uploadedResource.getName();
111         String extension = FilenameUtils.getExtension(uploadedResourceName);
112 
113         return Exceptions.wrap().get(() ->
114                 nodeNameHelper.getUniqueName(parentNode.getSession(), parentNode.getPath(), uploadedResourceName, extension));
115     }
116 }