View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.file;
16  
17  import static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
18  import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
19  
20  import java.io.IOException;
21  import java.nio.charset.Charset;
22  import java.nio.charset.StandardCharsets;
23  import java.time.Instant;
24  import java.time.ZoneId;
25  import java.time.format.DateTimeFormatter;
26  
27  import org.apache.commons.io.FileUtils;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.htmlunit.BrowserVersion;
31  import org.htmlunit.corejs.javascript.Context;
32  import org.htmlunit.corejs.javascript.NativeArray;
33  import org.htmlunit.corejs.javascript.ScriptableObject;
34  import org.htmlunit.javascript.JavaScriptEngine;
35  import org.htmlunit.javascript.configuration.JsxClass;
36  import org.htmlunit.javascript.configuration.JsxConstructor;
37  import org.htmlunit.javascript.configuration.JsxFunction;
38  import org.htmlunit.javascript.configuration.JsxGetter;
39  import org.htmlunit.util.KeyDataPair;
40  
41  /**
42   * A JavaScript object for {@code File}.
43   *
44   * @author Ahmed Ashour
45   * @author Ronald Brill
46   */
47  @JsxClass
48  public class File extends Blob {
49  
50      private static final DateTimeFormatter LAST_MODIFIED_DATE_FORMATTER
51                              = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'Z");
52  
53      private static class FileBackend implements Backend {
54          private static final Log LOG = LogFactory.getLog(FileBackend.class);
55  
56          private final java.io.File file_;
57  
58          FileBackend(final String pathname) {
59              super();
60              file_ = new java.io.File(pathname);
61          }
62  
63          @Override
64          public String getName() {
65              return file_.getName();
66          }
67  
68          @Override
69          public long getLastModified() {
70              return file_.lastModified();
71          }
72  
73          @Override
74          public long getSize() {
75              return file_.length();
76          }
77  
78          @Override
79          public String getType(final BrowserVersion browserVersion) {
80              return browserVersion.getUploadMimeType(file_);
81          }
82  
83          @Override
84          public String getText() throws IOException {
85              return FileUtils.readFileToString(file_, StandardCharsets.UTF_8);
86          }
87  
88          @Override
89          public byte[] getBytes(final int start, final int end) {
90              final byte[] result = new byte[end - start];
91              try {
92                  System.arraycopy(FileUtils.readFileToByteArray(file_), start, result, 0, result.length);
93              }
94              catch (final IOException e) {
95                  LOG.error("FileBackend.getBytes failed", e);
96              }
97              return result;
98          }
99  
100         /**
101          * {@inheritDoc}
102          */
103         @Override
104         public KeyDataPair getKeyDataPair(final String name, final String fileName, final String contentType) {
105             return new KeyDataPair(name, file_, fileName, contentType, (Charset) null);
106         }
107     }
108 
109     /**
110      * Prototye ctor.
111      */
112     public File() {
113         super();
114     }
115 
116     /**
117      * Creates an instance.
118      * @param fileBits the bits
119      * @param fileName the Name
120      * @param properties the properties
121      * @throws IOException in case of error
122      */
123     @JsxConstructor
124     public void jsConstructor(final NativeArray fileBits, final String fileName,
125             final ScriptableObject properties) throws IOException {
126         if (fileBits == null
127                 || JavaScriptEngine.isUndefined(fileBits)
128                 || fileName == null
129                 || JavaScriptEngine.isUndefined(fileName)) {
130             throw JavaScriptEngine.typeError("Failed to construct 'File': 2 arguments required.");
131         }
132 
133         setBackend(InMemoryBackend.create(fileBits, fileName,
134                             extractFileTypeOrDefault(properties),
135                             extractLastModifiedOrDefault(properties)));
136     }
137 
138     File(final String pathname) {
139         super();
140         setBackend(new FileBackend(pathname));
141     }
142 
143     /**
144      * Creates an instance backed by the given file.
145      *
146      * @param file the file
147      */
148     public File(final java.io.File file) {
149         this(file.getAbsolutePath());
150     }
151 
152     /**
153      * Creates an instance holding the given bytes in memory.
154      *
155      * @param bytes the bytes
156      * @param fileName the name
157      * @param type the type
158      * @param lastModified the last modified timestamp
159      */
160     public File(final byte[] bytes, final String fileName, final String type, final long lastModified) {
161         super();
162         setBackend(new InMemoryBackend(bytes, fileName, type == null ? "" : type, lastModified));
163     }
164 
165     /**
166      * Returns the {@code name} property.
167      * @return the {@code name} property
168      */
169     @JsxGetter
170     public String getName() {
171         return getBackend().getName();
172     }
173 
174     /**
175      * Returns the {@code lastModifiedDate} property.
176      * @return the {@code lastModifiedDate} property
177      */
178     @JsxGetter({CHROME, EDGE})
179     public String getLastModifiedDate() {
180         final Context cx = Context.getCurrentContext();
181         final ZoneId zoneid = cx.getTimeZone().toZoneId();
182 
183         // strange only the time zone is locale dependent
184         String date = LAST_MODIFIED_DATE_FORMATTER.format(Instant.ofEpochMilli(getLastModified()).atZone(zoneid));
185         date += DateTimeFormatter.ofPattern(" (zzzz)", cx.getLocale())
186                     .format(Instant.ofEpochMilli(getLastModified()).atZone(zoneid));
187         return date;
188     }
189 
190     /**
191      * Returns the {@code lastModified} property.
192      * @return the {@code lastModified} property
193      */
194     @JsxGetter
195     public long getLastModified() {
196         return getBackend().getLastModified();
197     }
198 
199     /**
200      * Returns the {@code webkitRelativePath} property.
201      * @return the {@code webkitRelativePath} property
202      */
203     @JsxGetter
204     public String getWebkitRelativePath() {
205         return "";
206     }
207 
208     /**
209      * Slices the file.
210      */
211     @JsxFunction
212     public void slice() {
213         // nothing to do
214     }
215 }