Browse Source

Merge pull request #35 from nextcloud/feature_uploadsubfolders

Feature uploadsubfolders
Andy Scherzinger 8 years ago
parent
commit
c875bbace1

+ 2 - 0
res/values/strings.xml

@@ -307,6 +307,8 @@
 
     <string name="error__upload__local_file_not_copied">%1$s could not be copied to %2$s local folder</string>
     <string name="prefs_instant_upload_path_title">Upload path</string>
+    <string name="prefs_instant_upload_path_use_subfolders_title">Use subfolders</string>
+    <string name="prefs_instant_upload_path_use_subfolders_summary">Store in subfolders based on year and month</string>
 
 	<string name="share_link_no_support_share_api">Sorry, sharing is not enabled on your server. Please contact your
 		administrator.</string>

+ 9 - 1
res/xml/preferences.xml

@@ -23,9 +23,13 @@
 		<com.owncloud.android.ui.CheckBoxPreferenceWithLongTitle android:key="instant_uploading"
 	                        android:title="@string/prefs_instant_upload"
 	                        android:summary="@string/prefs_instant_upload_summary"/>
-         <com.owncloud.android.ui.PreferenceWithLongSummary
+        <com.owncloud.android.ui.PreferenceWithLongSummary
 							android:title="@string/prefs_instant_upload_path_title"
 							android:key="instant_upload_path" />
+		<com.owncloud.android.ui.CheckBoxPreferenceWithLongTitle
+							android:title="@string/prefs_instant_upload_path_use_subfolders_title"
+                            android:summary="@string/prefs_instant_upload_path_use_subfolders_summary"
+							android:key="instant_upload_path_use_subfolders" />
 	    <com.owncloud.android.ui.CheckBoxPreferenceWithLongTitle
 	        				android:title="@string/instant_upload_on_wifi"
 	        				android:key="instant_upload_on_wifi"/>
@@ -35,6 +39,10 @@
 	    <com.owncloud.android.ui.PreferenceWithLongSummary
 							android:title="@string/prefs_instant_video_upload_path_title"
 							android:key="instant_video_upload_path" />
+		<com.owncloud.android.ui.CheckBoxPreferenceWithLongTitle
+			android:title="@string/prefs_instant_upload_path_use_subfolders_title"
+			android:summary="@string/prefs_instant_upload_path_use_subfolders_summary"
+			android:key="instant_video_upload_path_use_subfolders" />
 	    <com.owncloud.android.ui.CheckBoxPreferenceWithLongTitle
 	        				android:title="@string/instant_video_upload_on_wifi"
 	        				android:key="instant_video_upload_on_wifi"/>

+ 10 - 0
src/com/owncloud/android/db/PreferenceManager.java

@@ -37,8 +37,10 @@ public abstract class PreferenceManager {
     private static final String AUTO_PREF__SORT_ASCENDING = "sort_ascending";
     private static final String PREF__INSTANT_UPLOADING = "instant_uploading";
     private static final String PREF__INSTANT_VIDEO_UPLOADING = "instant_video_uploading";
+    private static final String PREF__INSTANT_UPLOAD_PATH_USE_SUBFOLDERS = "instant_upload_path_use_subfolders";
     private static final String PREF__INSTANT_UPLOAD_ON_WIFI = "instant_upload_on_wifi";
     private static final String PREF__INSTANT_VIDEO_UPLOAD_ON_WIFI = "instant_video_upload_on_wifi";
+    private static final String PREF__INSTANT_VIDEO_UPLOAD_PATH_USE_SUBFOLDERS = "instant_video_upload_path_use_subfolders";
 
     public static boolean instantPictureUploadEnabled(Context context) {
         return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOADING, false);
@@ -48,10 +50,18 @@ public abstract class PreferenceManager {
         return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOADING, false);
     }
 
+    public static boolean instantPictureUploadPathUseSubfolders(Context context) {
+        return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOAD_PATH_USE_SUBFOLDERS, false);
+    }
+
     public static boolean instantPictureUploadViaWiFiOnly(Context context) {
         return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_UPLOAD_ON_WIFI, false);
     }
 
+    public static boolean instantVideoUploadPathUseSubfolders(Context context) {
+        return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOAD_PATH_USE_SUBFOLDERS, false);
+    }
+
     public static boolean instantVideoUploadViaWiFiOnly(Context context) {
         return getDefaultSharedPreferences(context).getBoolean(PREF__INSTANT_VIDEO_UPLOAD_ON_WIFI, false);
     }

+ 6 - 2
src/com/owncloud/android/files/InstantUploadBroadcastReceiver.java

@@ -83,6 +83,7 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
         String file_path = null;
         String file_name = null;
         String mime_type = null;
+        long date_taken = 0;
 
         Log_OC.i(TAG, "New photo received");
 
@@ -116,6 +117,7 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
         file_path = c.getString(c.getColumnIndex(Images.Media.DATA));
         file_name = c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME));
         mime_type = c.getString(c.getColumnIndex(Images.Media.MIME_TYPE));
+        date_taken = System.currentTimeMillis();
         c.close();
 
         if (file_path.equals(lastUploadedPhotoPath)) {
@@ -134,7 +136,7 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
                 context,
                 account,
                 file_path,
-                FileStorageUtils.getInstantUploadFilePath(context, file_name),
+                FileStorageUtils.getInstantUploadFilePath(context, file_name, date_taken),
                 behaviour,
                 mime_type,
                 true,           // create parent folder if not existent
@@ -161,6 +163,7 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
         String file_path = null;
         String file_name = null;
         String mime_type = null;
+        long date_taken = 0;
 
         Log_OC.i(TAG, "New video received");
 
@@ -186,6 +189,7 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
         file_name = c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME));
         mime_type = c.getString(c.getColumnIndex(Video.Media.MIME_TYPE));
         c.close();
+        date_taken = System.currentTimeMillis();
         Log_OC.d(TAG, file_path + "");
 
         int behaviour = getUploadBehaviour(context);
@@ -194,7 +198,7 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
                 context,
                 account,
                 file_path,
-                FileStorageUtils.getInstantVideoUploadFilePath(context, file_name),
+                FileStorageUtils.getInstantVideoUploadFilePath(context, file_name, date_taken),
                 behaviour,
                 mime_type,
                 true,           // create parent folder if not existent

+ 10 - 2
src/com/owncloud/android/ui/activity/Preferences.java

@@ -81,9 +81,11 @@ public class Preferences extends PreferenceActivity {
     private Preference mPrefInstantUpload;
     private Preference mPrefInstantUploadBehaviour;
     private Preference mPrefInstantUploadPath;
+    private Preference mPrefInstantUploadUseSubfolders;
     private Preference mPrefInstantUploadPathWiFi;
     private Preference mPrefInstantVideoUpload;
     private Preference mPrefInstantVideoUploadPath;
+    private Preference mPrefInstantVideoUploadUseSubfolders;
     private Preference mPrefInstantVideoUploadPathWiFi;
     private String mUploadVideoPath;
 
@@ -286,7 +288,8 @@ public class Preferences extends PreferenceActivity {
         
         mPrefInstantUploadCategory =
                 (PreferenceCategory) findPreference("instant_uploading_category");
-        
+
+        mPrefInstantUploadUseSubfolders = findPreference("instant_upload_path_use_subfolders");
         mPrefInstantUploadPathWiFi =  findPreference("instant_upload_on_wifi");
         mPrefInstantUpload = findPreference("instant_uploading");
         
@@ -321,7 +324,8 @@ public class Preferences extends PreferenceActivity {
                     }
                 });
         }
-        
+
+        mPrefInstantVideoUploadUseSubfolders = findPreference("instant_video_upload_path_use_subfolders");
         mPrefInstantVideoUploadPathWiFi =  findPreference("instant_video_upload_on_wifi");
         mPrefInstantVideoUpload = findPreference("instant_video_uploading");
         toggleInstantVideoOptions(((CheckBoxPreference) mPrefInstantVideoUpload).isChecked());
@@ -359,9 +363,11 @@ public class Preferences extends PreferenceActivity {
         if (value){
             mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPathWiFi);
             mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPath);
+            mPrefInstantUploadCategory.addPreference(mPrefInstantUploadUseSubfolders);
         } else {
             mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPathWiFi);
             mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPath);
+            mPrefInstantUploadCategory.removePreference(mPrefInstantUploadUseSubfolders);
         }
     }
     
@@ -369,9 +375,11 @@ public class Preferences extends PreferenceActivity {
         if (value){
             mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPathWiFi);
             mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPath);
+            mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadUseSubfolders);
         } else {
             mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPathWiFi);
             mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPath);
+            mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadUseSubfolders);
         }
     }
 

+ 50 - 6
src/com/owncloud/android/utils/FileStorageUtils.java

@@ -21,8 +21,11 @@
 package com.owncloud.android.utils;
 
 import java.io.File;
+import java.text.SimpleDateFormat;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.Date;
+import java.util.Locale;
 import java.util.Vector;
 
 import third_parties.daveKoeller.AlphanumComparator;
@@ -30,6 +33,7 @@ import third_parties.daveKoeller.AlphanumComparator;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.files.RemoteFile;
 
 import android.accounts.Account;
@@ -47,6 +51,8 @@ import android.webkit.MimeTypeMap;
  * Static methods to help in access to local file system.
  */
 public class FileStorageUtils {
+    private static final String TAG = FileStorageUtils.class.getSimpleName();
+
     public static final Integer SORT_NAME = 0;
     public static final Integer SORT_DATE = 1;
     public static final Integer SORT_SIZE = 2;
@@ -117,12 +123,46 @@ public class FileStorageUtils {
         return Environment.getExternalStorageDirectory() + File.separator + MainApp.getDataFolder() + File.separator + "log";
     }
 
-    public static String getInstantUploadFilePath(Context context, String fileName) {
+    /**
+     * Returns the a string like 2016/08/ for the passed date. If date is 0 an empty
+     * string is returned
+     *
+     * @param date: date in microseconds since 1st January 1970
+     * @return
+     */
+    private static String getSubpathFromDate(long date) {
+        if (date == 0) {
+            return "";
+        }
+        try {
+            SimpleDateFormat formatter = new SimpleDateFormat(
+                    "yyyy" + OCFile.PATH_SEPARATOR + "MM" + OCFile.PATH_SEPARATOR, Locale.ENGLISH);
+            return formatter.format(new Date(date));
+        }
+        catch(RuntimeException ex) {
+            Log_OC.w(TAG, "could not extract date from timestamp");
+            return "";
+        }
+    }
+
+    /**
+     * Returns the InstantUploadFilePath on the owncloud instance
+     *
+     * @param context
+     * @param fileName
+     * @param dateTaken: Time in milliseconds since 1970 when the picture was taken.
+     * @return
+     */
+    public static String getInstantUploadFilePath(Context context, String fileName, long dateTaken) {
         SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
         String uploadPathdef = context.getString(R.string.instant_upload_path);
         String uploadPath = pref.getString("instant_upload_path", uploadPathdef);
-        String value = uploadPath + OCFile.PATH_SEPARATOR +  (fileName == null ? "" : fileName);
-        return value;
+        String subPath = "";
+        if (com.owncloud.android.db.PreferenceManager.instantPictureUploadPathUseSubfolders(context)) {
+           subPath = getSubpathFromDate(dateTaken);
+        }
+        return uploadPath + OCFile.PATH_SEPARATOR + subPath
+                + (fileName == null ? "" : fileName);
     }
 
     /**
@@ -131,12 +171,16 @@ public class FileStorageUtils {
      * @param fileName: video file name
      * @return String: video file path composed
      */
-    public static String getInstantVideoUploadFilePath(Context context, String fileName) {
+    public static String getInstantVideoUploadFilePath(Context context, String fileName, long dateTaken) {
         SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
         String uploadVideoPathdef = context.getString(R.string.instant_upload_path);
         String uploadVideoPath = pref.getString("instant_video_upload_path", uploadVideoPathdef);
-        String value = uploadVideoPath + OCFile.PATH_SEPARATOR +  (fileName == null ? "" : fileName);
-        return value;
+        String subPath = "";
+        if (com.owncloud.android.db.PreferenceManager.instantVideoUploadPathUseSubfolders(context)) {
+            subPath = getSubpathFromDate(dateTaken);
+        }
+        return uploadVideoPath + OCFile.PATH_SEPARATOR + subPath
+                + (fileName == null ? "" : fileName);
     }
     
     public static String getParentPath(String remotePath) {