FileStorageUtils.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.operations.common.RemoteFile;
  23. import android.annotation.SuppressLint;
  24. import android.content.Context;
  25. import android.net.Uri;
  26. import android.os.Environment;
  27. import android.os.StatFs;
  28. /**
  29. * Static methods to help in access to local file system.
  30. *
  31. * @author David A. Velasco
  32. */
  33. public class FileStorageUtils {
  34. //private static final String LOG_TAG = "FileStorageUtils";
  35. public static final String getSavePath(String accountName) {
  36. File sdCard = Environment.getExternalStorageDirectory();
  37. return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/" + Uri.encode(accountName, "@");
  38. // 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
  39. }
  40. public static final String getDefaultSavePathFor(String accountName, OCFile file) {
  41. return getSavePath(accountName) + file.getRemotePath();
  42. }
  43. public static final String getTemporalPath(String accountName) {
  44. File sdCard = Environment.getExternalStorageDirectory();
  45. return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/tmp/" + Uri.encode(accountName, "@");
  46. // 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
  47. }
  48. @SuppressLint("NewApi")
  49. public static final long getUsableSpace(String accountName) {
  50. File savePath = Environment.getExternalStorageDirectory();
  51. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
  52. return savePath.getUsableSpace();
  53. } else {
  54. StatFs stats = new StatFs(savePath.getAbsolutePath());
  55. return stats.getAvailableBlocks() * stats.getBlockSize();
  56. }
  57. }
  58. public static final String getLogPath() {
  59. return Environment.getExternalStorageDirectory() + File.separator + MainApp.getDataFolder() + File.separator + "log";
  60. }
  61. public static String getInstantUploadFilePath(Context context, String fileName) {
  62. String uploadPath = context.getString(R.string.instant_upload_path);
  63. String value = uploadPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
  64. return value;
  65. }
  66. public static String getParentPath(String remotePath) {
  67. String parentPath = new File(remotePath).getParent();
  68. parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
  69. return parentPath;
  70. }
  71. /**
  72. * Creates and populates a new {@link OCFile} object with the data read from the server.
  73. *
  74. * @param remote remote file read from the server (remote file or folder).
  75. * @return New OCFile instance representing the remote resource described by we.
  76. */
  77. public static OCFile fillOCFile(RemoteFile remote) {
  78. OCFile file = new OCFile(remote.getRemotePath());
  79. file.setCreationTimestamp(remote.getCreationTimestamp());
  80. file.setFileLength(remote.getLength());
  81. file.setMimetype(remote.getMimeType());
  82. file.setModificationTimestamp(remote.getModifiedTimestamp());
  83. file.setEtag(remote.getEtag());
  84. return file;
  85. }
  86. /**
  87. * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
  88. *
  89. * @param oCFile OCFile
  90. * @return New RemoteFile instance representing the resource described by ocFile.
  91. */
  92. public static RemoteFile fillRemoteFile(OCFile ocFile){
  93. RemoteFile file = new RemoteFile(ocFile.getRemotePath());
  94. file.setCreationTimestamp(ocFile.getCreationTimestamp());
  95. file.setLength(ocFile.getFileLength());
  96. file.setMimeType(ocFile.getMimetype());
  97. file.setModifiedTimestamp(ocFile.getModificationTimestamp());
  98. file.setEtag(ocFile.getEtag());
  99. return file;
  100. }
  101. }