1894e74be98970c98dc65b94d5920cb3d5a31f34
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / FileStorageUtils.java
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
18 package com.owncloud.android.utils;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.nio.channels.FileChannel;
25
26 import com.owncloud.android.MainApp;
27 import com.owncloud.android.R;
28 import com.owncloud.android.datamodel.OCFile;
29 import com.owncloud.android.lib.resources.files.RemoteFile;
30
31 import android.annotation.SuppressLint;
32 import android.content.Context;
33 import android.net.Uri;
34 import android.os.Environment;
35 import android.os.StatFs;
36
37
38 /**
39 * Static methods to help in access to local file system.
40 *
41 * @author David A. Velasco
42 */
43 public class FileStorageUtils {
44 //private static final String LOG_TAG = "FileStorageUtils";
45
46 public static final String getSavePath(String accountName) {
47 File sdCard = Environment.getExternalStorageDirectory();
48 return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/" + Uri.encode(accountName, "@");
49 // 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
50 }
51
52 public static final String getDefaultSavePathFor(String accountName, OCFile file) {
53 return getSavePath(accountName) + file.getRemotePath();
54 }
55
56 public static final String getTemporalPath(String accountName) {
57 File sdCard = Environment.getExternalStorageDirectory();
58 return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/tmp/" + Uri.encode(accountName, "@");
59 // 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
60 }
61
62 @SuppressLint("NewApi")
63 public static final long getUsableSpace(String accountName) {
64 File savePath = Environment.getExternalStorageDirectory();
65 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
66 return savePath.getUsableSpace();
67
68 } else {
69 StatFs stats = new StatFs(savePath.getAbsolutePath());
70 return stats.getAvailableBlocks() * stats.getBlockSize();
71 }
72
73 }
74
75 public static final String getLogPath() {
76 return Environment.getExternalStorageDirectory() + File.separator + MainApp.getDataFolder() + File.separator + "log";
77 }
78
79 public static String getInstantUploadFilePath(Context context, String fileName) {
80 String uploadPath = context.getString(R.string.instant_upload_path);
81 String value = uploadPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
82 return value;
83 }
84
85 public static String getParentPath(String remotePath) {
86 String parentPath = new File(remotePath).getParent();
87 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
88 return parentPath;
89 }
90
91 /**
92 * Creates and populates a new {@link OCFile} object with the data read from the server.
93 *
94 * @param remote remote file read from the server (remote file or folder).
95 * @return New OCFile instance representing the remote resource described by we.
96 */
97 public static OCFile fillOCFile(RemoteFile remote) {
98 OCFile file = new OCFile(remote.getRemotePath());
99 file.setCreationTimestamp(remote.getCreationTimestamp());
100 file.setFileLength(remote.getLength());
101 file.setMimetype(remote.getMimeType());
102 file.setModificationTimestamp(remote.getModifiedTimestamp());
103 file.setEtag(remote.getEtag());
104
105 return file;
106 }
107
108 /**
109 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
110 *
111 * @param oCFile OCFile
112 * @return New RemoteFile instance representing the resource described by ocFile.
113 */
114 public static RemoteFile fillRemoteFile(OCFile ocFile){
115 RemoteFile file = new RemoteFile(ocFile.getRemotePath());
116 file.setCreationTimestamp(ocFile.getCreationTimestamp());
117 file.setLength(ocFile.getFileLength());
118 file.setMimeType(ocFile.getMimetype());
119 file.setModifiedTimestamp(ocFile.getModificationTimestamp());
120 file.setEtag(ocFile.getEtag());
121 return file;
122 }
123
124 /**
125 * Copy file src in dst
126 * @param src
127 * @param dst
128 * @throws IOException
129 */
130 @SuppressWarnings("resource")
131 public static void copyFile(File src, File dst) throws IOException {
132 FileChannel inChannel = new FileInputStream(src).getChannel();
133 FileChannel outChannel = new FileOutputStream(dst).getChannel();
134 try {
135 inChannel.transferTo(0, inChannel.size(), outChannel);
136 } finally {
137 if (inChannel != null)
138 inChannel.close();
139 if (outChannel != null)
140 outChannel.close();
141 }
142 }
143
144
145 /**
146 * Delete folder
147 * @param folder
148 * @return true if folder is deleted
149 */
150 public static boolean deleteFolder(File folder){
151 if (folder.isDirectory()) {
152 String[] children = folder.list();
153 for (int i=0; i<children.length; i++) {
154 boolean success = deleteFolder(new File(folder, children[i]));
155 if (!success) {
156 return false;
157 }
158 }
159 }
160 // The folder is now empty so delete it
161 return folder.delete();
162
163 }
164
165 }