PageRenderTime 51ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/platforms/android/src/org/apache/cordova/file/Filesystem.java

https://gitlab.com/gregtyka/Ionic_Music_Player
Java | 325 lines | 247 code | 44 blank | 34 comment | 36 complexity | a32e7faffed326b437a94a046ab22aab MD5 | raw file
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. package org.apache.cordova.file;
  18. import android.net.Uri;
  19. import java.io.File;
  20. import java.io.FileNotFoundException;
  21. import java.io.FilterInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.util.ArrayList;
  26. import java.util.Arrays;
  27. import org.apache.cordova.CordovaResourceApi;
  28. import org.json.JSONArray;
  29. import org.json.JSONException;
  30. import org.json.JSONObject;
  31. public abstract class Filesystem {
  32. protected final Uri rootUri;
  33. protected final CordovaResourceApi resourceApi;
  34. public final String name;
  35. private JSONObject rootEntry;
  36. public Filesystem(Uri rootUri, String name, CordovaResourceApi resourceApi) {
  37. this.rootUri = rootUri;
  38. this.name = name;
  39. this.resourceApi = resourceApi;
  40. }
  41. public interface ReadFileCallback {
  42. public void handleData(InputStream inputStream, String contentType) throws IOException;
  43. }
  44. public static JSONObject makeEntryForURL(LocalFilesystemURL inputURL, Uri nativeURL) {
  45. try {
  46. String path = inputURL.path;
  47. int end = path.endsWith("/") ? 1 : 0;
  48. String[] parts = path.substring(0, path.length() - end).split("/+");
  49. String fileName = parts[parts.length - 1];
  50. JSONObject entry = new JSONObject();
  51. entry.put("isFile", !inputURL.isDirectory);
  52. entry.put("isDirectory", inputURL.isDirectory);
  53. entry.put("name", fileName);
  54. entry.put("fullPath", path);
  55. // The file system can't be specified, as it would lead to an infinite loop,
  56. // but the filesystem name can be.
  57. entry.put("filesystemName", inputURL.fsName);
  58. // Backwards compatibility
  59. entry.put("filesystem", "temporary".equals(inputURL.fsName) ? 0 : 1);
  60. String nativeUrlStr = nativeURL.toString();
  61. if (inputURL.isDirectory && !nativeUrlStr.endsWith("/")) {
  62. nativeUrlStr += "/";
  63. }
  64. entry.put("nativeURL", nativeUrlStr);
  65. return entry;
  66. } catch (JSONException e) {
  67. e.printStackTrace();
  68. throw new RuntimeException(e);
  69. }
  70. }
  71. public JSONObject makeEntryForURL(LocalFilesystemURL inputURL) {
  72. Uri nativeUri = toNativeUri(inputURL);
  73. return nativeUri == null ? null : makeEntryForURL(inputURL, nativeUri);
  74. }
  75. public JSONObject makeEntryForNativeUri(Uri nativeUri) {
  76. LocalFilesystemURL inputUrl = toLocalUri(nativeUri);
  77. return inputUrl == null ? null : makeEntryForURL(inputUrl, nativeUri);
  78. }
  79. public JSONObject getEntryForLocalURL(LocalFilesystemURL inputURL) throws IOException {
  80. return makeEntryForURL(inputURL);
  81. }
  82. public JSONObject makeEntryForFile(File file) {
  83. return makeEntryForNativeUri(Uri.fromFile(file));
  84. }
  85. abstract JSONObject getFileForLocalURL(LocalFilesystemURL inputURL, String path,
  86. JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException;
  87. abstract boolean removeFileAtLocalURL(LocalFilesystemURL inputURL) throws InvalidModificationException, NoModificationAllowedException;
  88. abstract boolean recursiveRemoveFileAtLocalURL(LocalFilesystemURL inputURL) throws FileExistsException, NoModificationAllowedException;
  89. abstract LocalFilesystemURL[] listChildren(LocalFilesystemURL inputURL) throws FileNotFoundException;
  90. public final JSONArray readEntriesAtLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException {
  91. LocalFilesystemURL[] children = listChildren(inputURL);
  92. JSONArray entries = new JSONArray();
  93. if (children != null) {
  94. for (LocalFilesystemURL url : children) {
  95. entries.put(makeEntryForURL(url));
  96. }
  97. }
  98. return entries;
  99. }
  100. abstract JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException;
  101. public Uri getRootUri() {
  102. return rootUri;
  103. }
  104. public boolean exists(LocalFilesystemURL inputURL) {
  105. try {
  106. getFileMetadataForLocalURL(inputURL);
  107. } catch (FileNotFoundException e) {
  108. return false;
  109. }
  110. return true;
  111. }
  112. public Uri nativeUriForFullPath(String fullPath) {
  113. Uri ret = null;
  114. if (fullPath != null) {
  115. String encodedPath = Uri.fromFile(new File(fullPath)).getEncodedPath();
  116. if (encodedPath.startsWith("/")) {
  117. encodedPath = encodedPath.substring(1);
  118. }
  119. ret = rootUri.buildUpon().appendEncodedPath(encodedPath).build();
  120. }
  121. return ret;
  122. }
  123. public LocalFilesystemURL localUrlforFullPath(String fullPath) {
  124. Uri nativeUri = nativeUriForFullPath(fullPath);
  125. if (nativeUri != null) {
  126. return toLocalUri(nativeUri);
  127. }
  128. return null;
  129. }
  130. /**
  131. * Removes multiple repeated //s, and collapses processes ../s.
  132. */
  133. protected static String normalizePath(String rawPath) {
  134. // If this is an absolute path, trim the leading "/" and replace it later
  135. boolean isAbsolutePath = rawPath.startsWith("/");
  136. if (isAbsolutePath) {
  137. rawPath = rawPath.replaceFirst("/+", "");
  138. }
  139. ArrayList<String> components = new ArrayList<String>(Arrays.asList(rawPath.split("/+")));
  140. for (int index = 0; index < components.size(); ++index) {
  141. if (components.get(index).equals("..")) {
  142. components.remove(index);
  143. if (index > 0) {
  144. components.remove(index-1);
  145. --index;
  146. }
  147. }
  148. }
  149. StringBuilder normalizedPath = new StringBuilder();
  150. for(String component: components) {
  151. normalizedPath.append("/");
  152. normalizedPath.append(component);
  153. }
  154. if (isAbsolutePath) {
  155. return normalizedPath.toString();
  156. } else {
  157. return normalizedPath.toString().substring(1);
  158. }
  159. }
  160. public abstract Uri toNativeUri(LocalFilesystemURL inputURL);
  161. public abstract LocalFilesystemURL toLocalUri(Uri inputURL);
  162. public JSONObject getRootEntry() {
  163. if (rootEntry == null) {
  164. rootEntry = makeEntryForNativeUri(rootUri);
  165. }
  166. return rootEntry;
  167. }
  168. public JSONObject getParentForLocalURL(LocalFilesystemURL inputURL) throws IOException {
  169. Uri parentUri = inputURL.uri;
  170. String parentPath = new File(inputURL.uri.getPath()).getParent();
  171. if (!"/".equals(parentPath)) {
  172. parentUri = inputURL.uri.buildUpon().path(parentPath + '/').build();
  173. }
  174. return getEntryForLocalURL(LocalFilesystemURL.parse(parentUri));
  175. }
  176. protected LocalFilesystemURL makeDestinationURL(String newName, LocalFilesystemURL srcURL, LocalFilesystemURL destURL, boolean isDirectory) {
  177. // I know this looks weird but it is to work around a JSON bug.
  178. if ("null".equals(newName) || "".equals(newName)) {
  179. newName = srcURL.uri.getLastPathSegment();;
  180. }
  181. String newDest = destURL.uri.toString();
  182. if (newDest.endsWith("/")) {
  183. newDest = newDest + newName;
  184. } else {
  185. newDest = newDest + "/" + newName;
  186. }
  187. if (isDirectory) {
  188. newDest += '/';
  189. }
  190. return LocalFilesystemURL.parse(newDest);
  191. }
  192. /* Read a source URL (possibly from a different filesystem, srcFs,) and copy it to
  193. * the destination URL on this filesystem, optionally with a new filename.
  194. * If move is true, then this method should either perform an atomic move operation
  195. * or remove the source file when finished.
  196. */
  197. public JSONObject copyFileToURL(LocalFilesystemURL destURL, String newName,
  198. Filesystem srcFs, LocalFilesystemURL srcURL, boolean move) throws IOException, InvalidModificationException, JSONException, NoModificationAllowedException, FileExistsException {
  199. // First, check to see that we can do it
  200. if (move && !srcFs.canRemoveFileAtLocalURL(srcURL)) {
  201. throw new NoModificationAllowedException("Cannot move file at source URL");
  202. }
  203. final LocalFilesystemURL destination = makeDestinationURL(newName, srcURL, destURL, srcURL.isDirectory);
  204. Uri srcNativeUri = srcFs.toNativeUri(srcURL);
  205. CordovaResourceApi.OpenForReadResult ofrr = resourceApi.openForRead(srcNativeUri);
  206. OutputStream os = null;
  207. try {
  208. os = getOutputStreamForURL(destination);
  209. } catch (IOException e) {
  210. ofrr.inputStream.close();
  211. throw e;
  212. }
  213. // Closes streams.
  214. resourceApi.copyResource(ofrr, os);
  215. if (move) {
  216. srcFs.removeFileAtLocalURL(srcURL);
  217. }
  218. return getEntryForLocalURL(destination);
  219. }
  220. public OutputStream getOutputStreamForURL(LocalFilesystemURL inputURL) throws IOException {
  221. return resourceApi.openOutputStream(toNativeUri(inputURL));
  222. }
  223. public void readFileAtURL(LocalFilesystemURL inputURL, long start, long end,
  224. ReadFileCallback readFileCallback) throws IOException {
  225. CordovaResourceApi.OpenForReadResult ofrr = resourceApi.openForRead(toNativeUri(inputURL));
  226. if (end < 0) {
  227. end = ofrr.length;
  228. }
  229. long numBytesToRead = end - start;
  230. try {
  231. if (start > 0) {
  232. ofrr.inputStream.skip(start);
  233. }
  234. InputStream inputStream = ofrr.inputStream;
  235. if (end < ofrr.length) {
  236. inputStream = new LimitedInputStream(inputStream, numBytesToRead);
  237. }
  238. readFileCallback.handleData(inputStream, ofrr.mimeType);
  239. } finally {
  240. ofrr.inputStream.close();
  241. }
  242. }
  243. abstract long writeToFileAtURL(LocalFilesystemURL inputURL, String data, int offset,
  244. boolean isBinary) throws NoModificationAllowedException, IOException;
  245. abstract long truncateFileAtURL(LocalFilesystemURL inputURL, long size)
  246. throws IOException, NoModificationAllowedException;
  247. // This method should return null if filesystem urls cannot be mapped to paths
  248. abstract String filesystemPathForURL(LocalFilesystemURL url);
  249. abstract LocalFilesystemURL URLforFilesystemPath(String path);
  250. abstract boolean canRemoveFileAtLocalURL(LocalFilesystemURL inputURL);
  251. protected class LimitedInputStream extends FilterInputStream {
  252. long numBytesToRead;
  253. public LimitedInputStream(InputStream in, long numBytesToRead) {
  254. super(in);
  255. this.numBytesToRead = numBytesToRead;
  256. }
  257. @Override
  258. public int read() throws IOException {
  259. if (numBytesToRead <= 0) {
  260. return -1;
  261. }
  262. numBytesToRead--;
  263. return in.read();
  264. }
  265. @Override
  266. public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {
  267. if (numBytesToRead <= 0) {
  268. return -1;
  269. }
  270. int bytesToRead = byteCount;
  271. if (byteCount > numBytesToRead) {
  272. bytesToRead = (int)numBytesToRead; // Cast okay; long is less than int here.
  273. }
  274. int numBytesRead = in.read(buffer, byteOffset, bytesToRead);
  275. numBytesToRead -= numBytesRead;
  276. return numBytesRead;
  277. }
  278. }
  279. }