- int status = uploadFile(client);
-
-
- /// move local temporal file or original file to its corresponding location in the ownCloud local folder
- if (isSuccess(status)) {
- File fileToMove = null;
- if (temporalFile != null) {
- fileToMove = temporalFile;
- } else if (originalFile != null) {
- fileToMove = originalFile;
- }
- if (fileToMove != null) {
- mFile.setStoragePath(FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile));
- File finalFile = new File(mFile.getStoragePath());
- if (!fileToMove.renameTo(finalFile)) {
- result = new RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_MOVED);
+ Iterator <OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
+ while (listener.hasNext()) {
+ mUploadOperation.addDatatransferProgressListener(listener.next());
+ }
+ if (mCancellationRequested.get()) {
+ throw new OperationCancelledException();
+ }
+
+ result = mUploadOperation.execute(client);
+
+ /// move local temporal file or original file to its corresponding
+ // location in the ownCloud local folder
+ if (result.isSuccess()) {
+ if (mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_FORGET) {
+ mFile.setStoragePath(null);
+ } else if (mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_REMOVE){
+ mFile.setStoragePath(null);
+ originalFile.delete();
+ } else {
+ mFile.setStoragePath(expectedPath);
+ File fileToMove = null;
+ if (temporalFile != null) { // FileUploader.LOCAL_BEHAVIOUR_COPY
+ // ; see where temporalFile was
+ // set
+ fileToMove = temporalFile;
+ } else { // FileUploader.LOCAL_BEHAVIOUR_MOVE
+ fileToMove = originalFile;
+ }
+ if (!expectedFile.equals(fileToMove)) {
+ File expectedFolder = expectedFile.getParentFile();
+ expectedFolder.mkdirs();
+
+ 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);
+ }