1894e74be98970c98dc65b94d5920cb3d5a31f34
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.utils
;
21 import java
.io
.FileInputStream
;
22 import java
.io
.FileOutputStream
;
23 import java
.io
.IOException
;
24 import java
.nio
.channels
.FileChannel
;
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
;
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
;
39 * Static methods to help in access to local file system.
41 * @author David A. Velasco
43 public class FileStorageUtils
{
44 //private static final String LOG_TAG = "FileStorageUtils";
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
52 public static final String
getDefaultSavePathFor(String accountName
, OCFile file
) {
53 return getSavePath(accountName
) + file
.getRemotePath();
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
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();
69 StatFs stats
= new StatFs(savePath
.getAbsolutePath());
70 return stats
.getAvailableBlocks() * stats
.getBlockSize();
75 public static final String
getLogPath() {
76 return Environment
.getExternalStorageDirectory() + File
.separator
+ MainApp
.getDataFolder() + File
.separator
+ "log";
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
);
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
;
92 * Creates and populates a new {@link OCFile} object with the data read from the server.
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.
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());
109 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
111 * @param oCFile OCFile
112 * @return New RemoteFile instance representing the resource described by ocFile.
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());
125 * Copy file src in dst
128 * @throws IOException
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();
135 inChannel
.transferTo(0, inChannel
.size(), outChannel
);
137 if (inChannel
!= null
)
139 if (outChannel
!= null
)
148 * @return true if folder is deleted
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
]));
160 // The folder is now empty so delete it
161 return folder
.delete();