FileStorageUtils.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2013 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.utils;
  18. import java.io.File;
  19. import com.owncloud.android.MainApp;
  20. import com.owncloud.android.R;
  21. import com.owncloud.android.datamodel.OCFile;
  22. import com.owncloud.android.lib.resources.files.RemoteFile;
  23. import android.annotation.SuppressLint;
  24. import android.content.Context;
  25. import android.content.SharedPreferences;
  26. import android.preference.PreferenceManager;
  27. import android.net.Uri;
  28. import android.os.Environment;
  29. import android.os.StatFs;
  30. /**
  31. * Static methods to help in access to local file system.
  32. *
  33. * @author David A. Velasco
  34. */
  35. public class FileStorageUtils {
  36. //private static final String LOG_TAG = "FileStorageUtils";
  37. public static final String getSavePath(String accountName) {
  38. File sdCard = Environment.getExternalStorageDirectory();
  39. return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/" + Uri.encode(accountName, "@");
  40. // URL encoding is an 'easy fix' to overcome that NTFS and FAT32 don't allow ":" in file names, that can be in the accountName since 0.1.190B
  41. }
  42. public static final String getDefaultSavePathFor(String accountName, OCFile file) {
  43. return getSavePath(accountName) + file.getRemotePath();
  44. }
  45. public static final String getTemporalPath(String accountName) {
  46. File sdCard = Environment.getExternalStorageDirectory();
  47. return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/tmp/" + Uri.encode(accountName, "@");
  48. // URL encoding is an 'easy fix' to overcome that NTFS and FAT32 don't allow ":" in file names, that can be in the accountName since 0.1.190B
  49. }
  50. @SuppressLint("NewApi")
  51. public static final long getUsableSpace(String accountName) {
  52. File savePath = Environment.getExternalStorageDirectory();
  53. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
  54. return savePath.getUsableSpace();
  55. } else {
  56. StatFs stats = new StatFs(savePath.getAbsolutePath());
  57. return stats.getAvailableBlocks() * stats.getBlockSize();
  58. }
  59. }
  60. public static final String getLogPath() {
  61. return Environment.getExternalStorageDirectory() + File.separator + MainApp.getDataFolder() + File.separator + "log";
  62. }
  63. public static String getInstantUploadFilePath(Context context, String fileName) {
  64. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
  65. String uploadPathdef = context.getString(R.string.instant_upload_path);
  66. String uploadPath = pref.getString("instant_upload_path", uploadPathdef);
  67. String value = uploadPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
  68. return value;
  69. }
  70. public static String getParentPath(String remotePath) {
  71. String parentPath = new File(remotePath).getParent();
  72. parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
  73. return parentPath;
  74. }
  75. /**
  76. * Creates and populates a new {@link OCFile} object with the data read from the server.
  77. *
  78. * @param remote remote file read from the server (remote file or folder).
  79. * @return New OCFile instance representing the remote resource described by we.
  80. */
  81. public static OCFile fillOCFile(RemoteFile remote) {
  82. OCFile file = new OCFile(remote.getRemotePath());
  83. file.setCreationTimestamp(remote.getCreationTimestamp());
  84. file.setFileLength(remote.getLength());
  85. file.setMimetype(remote.getMimeType());
  86. file.setModificationTimestamp(remote.getModifiedTimestamp());
  87. file.setEtag(remote.getEtag());
  88. file.setPermissions(remote.getPermissions());
  89. file.setRemoteId(remote.getRemoteId());
  90. return file;
  91. }
  92. /**
  93. * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
  94. *
  95. * @param oCFile OCFile
  96. * @return New RemoteFile instance representing the resource described by ocFile.
  97. */
  98. public static RemoteFile fillRemoteFile(OCFile ocFile){
  99. RemoteFile file = new RemoteFile(ocFile.getRemotePath());
  100. file.setCreationTimestamp(ocFile.getCreationTimestamp());
  101. file.setLength(ocFile.getFileLength());
  102. file.setMimeType(ocFile.getMimetype());
  103. file.setModifiedTimestamp(ocFile.getModificationTimestamp());
  104. file.setEtag(ocFile.getEtag());
  105. file.setPermissions(ocFile.getPermissions());
  106. file.setRemoteId(ocFile.getRemoteId());
  107. return file;
  108. }
  109. }