Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid duplicate files #1168

Merged
merged 25 commits into from
Dec 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
69bbb5c
#819: sending files to owncloud from another app: only upload, no copy
tobiasKaminsky Sep 18, 2015
4b8a5a9
#819, part 2: Instant uploads, depends on setting
tobiasKaminsky Sep 19, 2015
a1b2655
wip
tobiasKaminsky Sep 21, 2015
a64308f
wip
tobiasKaminsky Sep 21, 2015
2b6a61d
#819 part3: When uploading from filesystem inside the owncloud app, s…
tobiasKaminsky Sep 21, 2015
a0b061a
java 1.5 does not support switch with strings :/
tobiasKaminsky Sep 21, 2015
5034e42
extracted hard-coded string to string resource
tobiasKaminsky Sep 23, 2015
19bc90b
default value: do nothing
tobiasKaminsky Sep 25, 2015
126d251
fixed bug: behaviour is now obeyed for files that are uploaded after …
tobiasKaminsky Sep 25, 2015
ff5202a
Merge remote-tracking branch 'remotes/upstream/master' into avoidDupl…
tobiasKaminsky Sep 25, 2015
57862ef
added 16dp padding
tobiasKaminsky Sep 25, 2015
35a68aa
custom list preference implementation for visual material consistency…
AndyScherzinger Sep 27, 2015
2ffdb4d
16dp right and left for checkpoxed
AndyScherzinger Sep 27, 2015
9170394
added a divider to the bottom part
AndyScherzinger Sep 27, 2015
e61641b
center align radio buttons
AndyScherzinger Oct 21, 2015
2aac1e5
Merge remote-tracking branch 'remotes/upstream/master' into avoidDupl…
tobiasKaminsky Nov 21, 2015
4471d3b
changes according to CR (code review)
tobiasKaminsky Nov 21, 2015
d901208
rename file is not working across mount points: therefore copying it …
tobiasKaminsky Nov 22, 2015
5d77cfc
fix 1313
tobiasKaminsky Nov 27, 2015
4f7d416
BugFix for #1312 - setting ListPreference selection on pre kitkat
AndyScherzinger Nov 30, 2015
d1a06c2
Prevent that repeated requests to FileDownloader/FileUploader/SyncFol…
davivel Nov 19, 2015
a730435
Fix issue 1319: Error on the preview, Android 4.1.2
Dec 1, 2015
04fc2e3
changed according to latest discussion
tobiasKaminsky Dec 1, 2015
9e678fc
changed according to latest comments
tobiasKaminsky Dec 2, 2015
59442c8
Update string
Dec 3, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion res/layout/upload_files_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,59 @@
android:layout_weight="1"
class="com.owncloud.android.ui.fragment.LocalFileListFragment" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:src="@drawable/uploader_list_separator"/>

</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp">

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_radio_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/upload_copy_files"
android:id="@+id/upload_radio_copy"
android:paddingRight="8dp"
android:checked="false" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/upload_move_files"
android:id="@+id/upload_radio_move"
android:paddingRight="8dp"
android:checked="false" />
</RadioGroup>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
android:orientation="horizontal"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp">

<android.support.v7.widget.AppCompatButton
android:id="@+id/upload_files_btn_cancel"
Expand Down
9 changes: 9 additions & 0 deletions res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@

</declare-styleable>

<string-array name="pref_behaviour_entries">
<item>@string/pref_behaviour_entries_keep_file</item>
<item>@string/pref_behaviour_entries_move</item>
</string-array>

<string-array name="pref_behaviour_entryValues">
<item>NOTHING</item>
<item>MOVE</item>
</string-array>
</resources>
7 changes: 7 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,13 @@
<string name="file_list__footer__files">%1$d files</string>
<string name="file_list__footer__files_and_folder">%1$d files, 1 folder</string>
<string name="file_list__footer__files_and_folders">%1$d files, %2$d folders</string>
<string name="prefs_instant_behaviour_dialogTitle">Original file will be...</string>
<string name="prefs_instant_behaviour_title">Original file will be...</string>
<string name="upload_copy_files">Copy file</string>
<string name="upload_move_files">Move file</string>

<string name="pref_behaviour_entries_keep_file">kept in original folder</string>
<string name="pref_behaviour_entries_move">moved to app folder</string>

<string name="share_dialog_title">Sharing</string>
<string name="share_with_user_section_title">Share with Users and Groups</string>
Expand Down
10 changes: 9 additions & 1 deletion res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</PreferenceCategory>

<PreferenceCategory android:title="@string/prefs_category_instant_uploading" android:key="instant_uploading_category">
<com.owncloud.android.ui.CheckBoxPreferenceWithLongTitle android:key="instant_uploading"
<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
Expand All @@ -44,6 +44,14 @@
<com.owncloud.android.ui.CheckBoxPreferenceWithLongTitle
android:title="@string/instant_video_upload_on_wifi"
android:key="instant_video_upload_on_wifi"/>
<com.owncloud.android.ui.dialog.OwnCloudListPreference android:key="prefs_instant_behaviour"
android:dialogTitle="@string/prefs_instant_behaviour_dialogTitle"
android:title="@string/prefs_instant_behaviour_title"
android:entries="@array/pref_behaviour_entries"
android:entryValues="@array/pref_behaviour_entryValues"
android:defaultValue="NOTHING"
android:summary="%s"
/>
<!-- DISABLED FOR RELEASE UNTIL FIXED
CheckBoxPreference android:key="log_to_file"
android:title="@string/prefs_log_title"
Expand Down
10 changes: 6 additions & 4 deletions src/com/owncloud/android/datamodel/FileDataStorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1557,10 +1557,12 @@ public ArrayList<OCShare> getSharesWithForAFile(String filePath, String accountN
return shares;
}

public void triggerMediaScan(String path) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(new File(path)));
MainApp.getAppContext().sendBroadcast(intent);
public static void triggerMediaScan(String path) {
if (path != null) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(new File(path)));
MainApp.getAppContext().sendBroadcast(intent);
}
}

public void deleteFileInMediaScan(String path) {
Expand Down
27 changes: 27 additions & 0 deletions src/com/owncloud/android/files/InstantUploadBroadcastReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
Expand Down Expand Up @@ -122,9 +123,27 @@ private void handleNewPictureAction(Context context, Intent intent) {
i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);

// instant upload behaviour
i = addInstantUploadBehaviour(i, context);

context.startService(i);
}

private Intent addInstantUploadBehaviour(Intent i, Context context){
SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block is three times in the same file.
Could you include this code in a method and use it instead of repeat it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done.

String behaviour = appPreferences.getString("prefs_instant_behaviour", "NOTHING");

if (behaviour.equalsIgnoreCase("NOTHING")) {
Log_OC.d(TAG, "upload file and do nothing");
i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_FORGET);
} else if (behaviour.equalsIgnoreCase("MOVE")) {
i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
Log_OC.d(TAG, "upload file and move file to oc folder");
}
return i;
}

private void handleNewVideoAction(Context context, Intent intent) {
Cursor c = null;
String file_path = null;
Expand Down Expand Up @@ -167,6 +186,10 @@ private void handleNewVideoAction(Context context, Intent intent) {
i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);

// instant upload behaviour
i = addInstantUploadBehaviour(i, context);

context.startService(i);

}
Expand Down Expand Up @@ -207,6 +230,10 @@ && isOnline(context)
i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, f.getName()));
i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);

// instant upload behaviour
i = addInstantUploadBehaviour(i, context);

context.startService(i);

} else {
Expand Down
9 changes: 5 additions & 4 deletions src/com/owncloud/android/files/services/FileDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ public int onStartCommand(Intent intent, int flags, int startId) {
Pair<String, String> putResult = mPendingDownloads.putIfAbsent(
account, file.getRemotePath(), newDownload
);
String downloadKey = putResult.first;
requestedDownloads.add(downloadKey);

sendBroadcastNewDownload(newDownload, putResult.second);
if (putResult != null) {
String downloadKey = putResult.first;
requestedDownloads.add(downloadKey);
sendBroadcastNewDownload(newDownload, putResult.second);
} // else, file already in the queue of downloads; don't repeat the request

} catch (IllegalArgumentException e) {
Log_OC.e(TAG, "Not enough information provided in intent: " + e.getMessage());
Expand Down
8 changes: 5 additions & 3 deletions src/com/owncloud/android/files/services/FileUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {

boolean forceOverwrite = intent.getBooleanExtra(KEY_FORCE_OVERWRITE, false);
boolean isInstant = intent.getBooleanExtra(KEY_INSTANT_UPLOAD, false);
int localAction = intent.getIntExtra(KEY_LOCAL_BEHAVIOUR, LOCAL_BEHAVIOUR_COPY);
int localAction = intent.getIntExtra(KEY_LOCAL_BEHAVIOUR, LOCAL_BEHAVIOUR_FORGET);

if (intent.hasExtra(KEY_FILE) && files == null) {
Log_OC.e(TAG, "Incorrect array for OCFiles provided in upload intent");
Expand Down Expand Up @@ -299,8 +299,10 @@ public int onStartCommand(Intent intent, int flags, int startId) {
Pair<String, String> putResult = mPendingUploads.putIfAbsent(
account, files[i].getRemotePath(), newUpload
);
uploadKey = putResult.first;
requestedUploads.add(uploadKey);
if (putResult != null) {
uploadKey = putResult.first;
requestedUploads.add(uploadKey);
} // else, file already in the queue of uploads; don't repeat the request
}

} catch (IllegalArgumentException e) {
Expand Down
59 changes: 33 additions & 26 deletions src/com/owncloud/android/files/services/IndexedForest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,38 +101,45 @@ public void clearPayload() {
public /* synchronized */ Pair<String, String> putIfAbsent(Account account, String remotePath, V value) {
String targetKey = buildKey(account, remotePath);
Node<V> valuedNode = new Node(targetKey, value);
mMap.putIfAbsent(
targetKey,
valuedNode
Node<V> previousValue = mMap.putIfAbsent(
targetKey,
valuedNode
);
if (previousValue != null) {
// remotePath already known; not replaced
return null;

String currentPath = remotePath, parentPath = null, parentKey = null;
Node<V> currentNode = valuedNode, parentNode = null;
boolean linked = false;
while (!OCFile.ROOT_PATH.equals(currentPath) && !linked) {
parentPath = new File(currentPath).getParent();
if (!parentPath.endsWith(OCFile.PATH_SEPARATOR)) {
parentPath += OCFile.PATH_SEPARATOR;
} else {
// value really added
String currentPath = remotePath, parentPath = null, parentKey = null;
Node<V> currentNode = valuedNode, parentNode = null;
boolean linked = false;
while (!OCFile.ROOT_PATH.equals(currentPath) && !linked) {
parentPath = new File(currentPath).getParent();
if (!parentPath.endsWith(OCFile.PATH_SEPARATOR)) {
parentPath += OCFile.PATH_SEPARATOR;
}
parentKey = buildKey(account, parentPath);
parentNode = mMap.get(parentKey);
if (parentNode == null) {
parentNode = new Node(parentKey, null);
parentNode.addChild(currentNode);
mMap.put(parentKey, parentNode);
} else {
parentNode.addChild(currentNode);
linked = true;
}
currentPath = parentPath;
currentNode = parentNode;
}
parentKey = buildKey(account, parentPath);
parentNode = mMap.get(parentKey);
if (parentNode == null) {
parentNode = new Node(parentKey, null);
parentNode.addChild(currentNode);
mMap.put(parentKey, parentNode);
} else {
parentNode.addChild(currentNode);
linked = true;

String linkedTo = OCFile.ROOT_PATH;
if (linked) {
linkedTo = parentNode.getKey().substring(account.name.length());
}
currentPath = parentPath;
currentNode = parentNode;
}

String linkedTo = OCFile.ROOT_PATH;
if (linked) {
linkedTo = parentNode.getKey().substring(account.name.length());
return new Pair<String, String>(targetKey, linkedTo);
}
return new Pair<String, String>(targetKey, linkedTo);
};


Expand Down
50 changes: 36 additions & 14 deletions src/com/owncloud/android/operations/UploadFileOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
Expand All @@ -39,6 +40,7 @@
import android.net.Uri;

import com.owncloud.android.MainApp;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.files.services.FileUploader;
import com.owncloud.android.lib.common.OwnCloudClient;
Expand Down Expand Up @@ -330,8 +332,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
// location in the ownCloud local folder
if (result.isSuccess()) {
if (mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_FORGET) {
mFile.setStoragePath(null);

mFile.setStoragePath("");
} else {
mFile.setStoragePath(expectedPath);
File fileToMove = null;
Expand All @@ -345,21 +346,42 @@ protected RemoteOperationResult run(OwnCloudClient client) {
if (!expectedFile.equals(fileToMove)) {
File expectedFolder = expectedFile.getParentFile();
expectedFolder.mkdirs();
if (!expectedFolder.isDirectory() || !fileToMove.renameTo(expectedFile)) {
mFile.setStoragePath(null); // forget the local file
// by now, treat this as a success; the file was
// uploaded; the user won't like that the local file
// is not linked, but this should be a very rare
// fail;
// the best option could be show a warning message
// (but not a fail)
// result = new
// RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_MOVED);
// return result;

if (expectedFolder.isDirectory()){
if (!fileToMove.renameTo(expectedFile)){
// try to copy and then delete
expectedFile.createNewFile();
FileChannel inChannel = new FileInputStream(fileToMove).getChannel();
FileChannel outChannel = new FileOutputStream(expectedFile).getChannel();

try {
inChannel.transferTo(0, inChannel.size(), outChannel);
fileToMove.delete();
} catch (Exception e){
mFile.setStoragePath(null); // forget the local file
// by now, treat this as a success; the file was
// uploaded; the user won't like that the local file
// is not linked, but this should be a very rare
// fail;
// the best option could be show a warning message
// (but not a fail)
// result = new
// RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_MOVED);
// return result;
}
finally {
if (inChannel != null) inChannel.close();
if (outChannel != null) outChannel.close();
}
}

} else {
mFile.setStoragePath(null);
}
}
}

FileDataStorageManager.triggerMediaScan(originalFile.getAbsolutePath());
FileDataStorageManager.triggerMediaScan(expectedFile.getAbsolutePath());
} else if (result.getHttpCode() == HttpStatus.SC_PRECONDITION_FAILED ) {
result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
}
Expand Down
7 changes: 5 additions & 2 deletions src/com/owncloud/android/services/SyncFolderHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,11 @@ private void doOperation(Account account, String remotePath) {

public void add(Account account, String remotePath,
SynchronizeFolderOperation syncFolderOperation){
mPendingOperations.putIfAbsent(account, remotePath, syncFolderOperation);
sendBroadcastNewSyncFolder(account, remotePath); // TODO upgrade!
Pair<String, String> putResult =
mPendingOperations.putIfAbsent(account, remotePath, syncFolderOperation);
if (putResult != null) {
sendBroadcastNewSyncFolder(account, remotePath); // TODO upgrade!
}
}


Expand Down
Loading