View Javadoc
1   /**
2    * This file Copyright (c) 2019 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.ui.framework.util;
35  
36  import info.magnolia.cms.core.FileSystemHelper;
37  import info.magnolia.ui.api.ioc.ViewScoped;
38  import info.magnolia.ui.framework.ioc.Destructible;
39  
40  import java.io.File;
41  import java.io.IOException;
42  import java.util.HashSet;
43  import java.util.Set;
44  
45  import javax.inject.Inject;
46  
47  import org.apache.commons.io.FileUtils;
48  import org.apache.commons.io.FilenameUtils;
49  import org.apache.commons.lang3.StringUtils;
50  
51  
52  /**
53   * View context holding created temp files.
54   */
55  @ViewScoped
56  public class TempFilesManager implements Destructible {
57      private static final String FILENAME_PREFIX = "___";
58      private static final String SEPARATOR = ".";
59  
60      private final Set<File> files = new HashSet<>();
61  
62      private final FileSystemHelper fileSystemHelper;
63  
64      @Inject
65      public TempFilesManager(FileSystemHelper fileSystemHelper) {
66          this.fileSystemHelper = fileSystemHelper;
67      }
68  
69      public File createTempFile(String filename) throws IOException {
70          String prefix = expandShortFileNameToLengthThree(filename);
71          String suffix = SEPARATOR + FilenameUtils.getExtension(filename);
72  
73          File tempFile = new TempFile(File.createTempFile(prefix, suffix, fileSystemHelper.getTempDirectory()).getPath());
74          tempFile.deleteOnExit();
75          register(tempFile);
76          return tempFile;
77      }
78  
79      /**
80       * Adds a {@link TempFilesManager#FILENAME_PREFIX} prefix to the filename.
81       *
82       * @param filename The name of the file that needs to be prepended with prefix.
83       * @return The expanded filename using the algorithm defined above.
84       */
85      private String expandShortFileNameToLengthThree(String filename) {
86          // A fixed length prefix is added to prevent IllegalArgumentException from
87          // the API File.createTempFile
88          return  FILENAME_PREFIX + FilenameUtils.removeExtension(filename) + SEPARATOR;
89      }
90  
91      public void register(File tempFile) {
92          files.add(tempFile);
93      }
94  
95      public String toStandardFileName(String tempFileName) {
96          String extension = FilenameUtils.getExtension(tempFileName);
97          String fileName = FilenameUtils.removeExtension(tempFileName);
98          fileName = StringUtils.substringBeforeLast(fileName, SEPARATOR);
99          fileName = StringUtils.startsWith(fileName, FILENAME_PREFIX) ? StringUtils.substringAfter(fileName, FILENAME_PREFIX) : fileName;
100         return  String.join(SEPARATOR, fileName, extension);
101     }
102 
103     @Override
104     public void destroy() {
105         files.forEach(FileUtils::deleteQuietly);
106     }
107 
108     private class TempFile extends File {
109         private String pathname;
110 
111         private TempFile(String pathname) {
112             super(pathname);
113             this.pathname = pathname;
114         }
115 
116         @Override
117         public String getName() {
118             return toStandardFileName(FilenameUtils.getName(pathname));
119         }
120     }
121 
122 }