0f4009be6754082a09ed3bfc6e384ab88cdef25a
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / utils / FileUtils.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.oc_framework.utils;
19
20 import java.io.File;
21
22 import com.owncloud.android.oc_framework.network.webdav.WebdavEntry;
23 import com.owncloud.android.oc_framework.operations.RemoteFile;
24
25 import android.util.Log;
26
27 public class FileUtils {
28
29 public static final String PATH_SEPARATOR = "/";
30
31
32 public static String getParentPath(String remotePath) {
33 String parentPath = new File(remotePath).getParent();
34 parentPath = parentPath.endsWith(PATH_SEPARATOR) ? parentPath : parentPath + PATH_SEPARATOR;
35 return parentPath;
36 }
37
38 /**
39 * Validate the fileName to detect if contains any forbidden character: / , \ , < , > , : , " , | , ? , *
40 * @param fileName
41 * @return
42 */
43 public static boolean isValidName(String fileName) {
44 boolean result = true;
45
46 Log.d("FileUtils", "fileName =======" + fileName);
47 if (fileName.contains(PATH_SEPARATOR) ||
48 fileName.contains("\\") || fileName.contains("<") || fileName.contains(">") ||
49 fileName.contains(":") || fileName.contains("\"") || fileName.contains("|") ||
50 fileName.contains("?") || fileName.contains("*")) {
51 result = false;
52 }
53 return result;
54 }
55
56 /**
57 * Validate the path to detect if contains any forbidden character: \ , < , > , : , " , | , ? , *
58 * @param path
59 * @return
60 */
61 public static boolean isValidPath(String path) {
62 boolean result = true;
63
64 Log.d("FileUtils", "path ....... " + path);
65 if (path.contains("\\") || path.contains("<") || path.contains(">") ||
66 path.contains(":") || path.contains("\"") || path.contains("|") ||
67 path.contains("?") || path.contains("*")) {
68 result = false;
69 }
70 return result;
71 }
72
73 /**
74 * Creates and populates a new {@link RemoteFile} object with the data read from the server.
75 *
76 * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder).
77 * @return New OCFile instance representing the remote resource described by we.
78 */
79 public static RemoteFile fillOCFile(WebdavEntry we) {
80 RemoteFile file = new RemoteFile(we.decodedPath());
81 file.setCreationTimestamp(we.createTimestamp());
82 file.setLength(we.contentLength());
83 file.setMimeType(we.contentType());
84 file.setModifiedTimestamp(we.modifiedTimestamp());
85 file.setEtag(we.etag());
86 return file;
87 }
88
89 }