mirror of
https://github.com/nextcloud/android.git
synced 2024-11-26 23:28:42 +03:00
Improve
Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
parent
ffc618795b
commit
013566a27b
2 changed files with 67 additions and 43 deletions
|
@ -52,9 +52,11 @@ import com.owncloud.android.utils.UriUtils;
|
|||
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.methods.RequestEntity;
|
||||
import org.apache.commons.io.input.CountingInputStream;
|
||||
import org.lukhnos.nnio.file.Files;
|
||||
import org.lukhnos.nnio.file.Paths;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
|
@ -398,72 +400,89 @@ public class UploadFileOperation extends SyncOperation {
|
|||
String timeStamp = timeStampLong.toString();
|
||||
|
||||
|
||||
boolean onSDCard = false;
|
||||
FileChannel channel = null;
|
||||
try {
|
||||
channel = new RandomAccessFile(mFile.getStoragePath(), "rw").getChannel();
|
||||
fileLock = channel.tryLock();
|
||||
} catch (FileNotFoundException e) {
|
||||
onSDCard = true;
|
||||
// this basically means that the file is on SD card
|
||||
// try to copy file to temporary dir if it doesn't exist
|
||||
if (temporalFile == null) {
|
||||
String temporalPath = FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
|
||||
mFile.setStoragePath(temporalPath);
|
||||
temporalFile = new File(temporalPath);
|
||||
String temporalPath = FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
|
||||
mFile.setStoragePath(temporalPath);
|
||||
temporalFile = new File(temporalPath);
|
||||
|
||||
result = copy(originalFile, temporalFile);
|
||||
Files.deleteIfExists(Paths.get(temporalPath));
|
||||
result = copy(originalFile, temporalFile);
|
||||
|
||||
if (result == null) {
|
||||
if (temporalFile.length() == originalFile.length()) {
|
||||
channel = new RandomAccessFile(temporalFile.getAbsolutePath(), "rw").getChannel();
|
||||
if (result == null) {
|
||||
if (temporalFile.length() == originalFile.length()) {
|
||||
channel = new RandomAccessFile(temporalFile.getAbsolutePath(), "rw").getChannel();
|
||||
fileLock = channel.tryLock();
|
||||
} else {
|
||||
Files.deleteIfExists(Paths.get(temporalPath));
|
||||
result = copy(originalFile, temporalFile);
|
||||
|
||||
if (result == null) {
|
||||
channel = new RandomAccessFile(temporalFile.getAbsolutePath(), "rw").
|
||||
getChannel();
|
||||
fileLock = channel.tryLock();
|
||||
} else {
|
||||
Files.deleteIfExists(Paths.get(temporalPath));
|
||||
result = copy(originalFile, temporalFile);
|
||||
|
||||
if (result == null) {
|
||||
channel = new RandomAccessFile(temporalFile.getAbsolutePath(), "rw").
|
||||
getChannel();
|
||||
fileLock = channel.tryLock();
|
||||
}
|
||||
result = new RemoteOperationResult(ResultCode.LOCK_FAILED);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
channel = new RandomAccessFile(temporalFile.getAbsolutePath(), "rw").getChannel();
|
||||
fileLock = channel.tryLock();
|
||||
}
|
||||
}
|
||||
|
||||
long size;
|
||||
long size = 0;
|
||||
UploadsStorageManager uploadsStorageManager = new UploadsStorageManager(mContext.getContentResolver(),
|
||||
mContext);
|
||||
if (mFile.getStoragePath().length() == 0 && !(new File(mFile.getStoragePath()).isDirectory())) {
|
||||
size = channel.size();
|
||||
} else {
|
||||
size = mFile.getStoragePath().length();
|
||||
}
|
||||
if (result == null) {
|
||||
if ((mFile.getStoragePath().length() == 0 || onSDCard) && !(new File(mFile.getStoragePath()).isDirectory())) {
|
||||
size = channel.size();
|
||||
|
||||
for (OCUpload ocUpload : uploadsStorageManager.getAllStoredUploads()) {
|
||||
if (ocUpload.getUploadId() == getOCUploadId()) {
|
||||
ocUpload.setFileSize(size);
|
||||
uploadsStorageManager.updateUpload(ocUpload);
|
||||
break;
|
||||
if (size == 0) {
|
||||
CountingInputStream countingInputStream = new CountingInputStream(new BufferedInputStream(
|
||||
new FileInputStream(mFile.getStoragePath())));
|
||||
|
||||
while (countingInputStream.read() != -1) {
|
||||
|
||||
}
|
||||
|
||||
size = countingInputStream.getByteCount();
|
||||
}
|
||||
} else {
|
||||
size = mFile.getStoragePath().length();
|
||||
}
|
||||
|
||||
for (OCUpload ocUpload : uploadsStorageManager.getAllStoredUploads()) {
|
||||
if (ocUpload.getUploadId() == getOCUploadId()) {
|
||||
ocUpload.setFileSize(size);
|
||||
uploadsStorageManager.updateUpload(ocUpload);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// perform the upload
|
||||
if (mChunked &&
|
||||
(size > ChunkedUploadRemoteFileOperation.CHUNK_SIZE)) {
|
||||
mUploadOperation = new ChunkedUploadRemoteFileOperation(mContext, mFile.getStoragePath(),
|
||||
mFile.getRemotePath(), mFile.getMimetype(), mFile.getEtagInConflict(), timeStamp, size);
|
||||
if (size > 0) {
|
||||
/// perform the upload
|
||||
if (mChunked &&
|
||||
(size > ChunkedUploadRemoteFileOperation.CHUNK_SIZE)) {
|
||||
mUploadOperation = new ChunkedUploadRemoteFileOperation(mContext, mFile.getStoragePath(),
|
||||
mFile.getRemotePath(), mFile.getMimetype(), mFile.getEtagInConflict(), timeStamp, size);
|
||||
} else {
|
||||
mUploadOperation = new UploadRemoteFileOperation(mFile.getStoragePath(),
|
||||
mFile.getRemotePath(), mFile.getMimetype(), mFile.getEtagInConflict(), timeStamp, size);
|
||||
}
|
||||
|
||||
Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
|
||||
while (listener.hasNext()) {
|
||||
mUploadOperation.addDatatransferProgressListener(listener.next());
|
||||
}
|
||||
} else {
|
||||
mUploadOperation = new UploadRemoteFileOperation(mFile.getStoragePath(),
|
||||
mFile.getRemotePath(), mFile.getMimetype(), mFile.getEtagInConflict(), timeStamp, size);
|
||||
}
|
||||
|
||||
Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
|
||||
while (listener.hasNext()) {
|
||||
mUploadOperation.addDatatransferProgressListener(listener.next());
|
||||
result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
|
||||
}
|
||||
|
||||
if (mCancellationRequested.get()) {
|
||||
|
|
|
@ -237,7 +237,11 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
|
|||
|
||||
// file size
|
||||
TextView fileSizeTextView = (TextView) view.findViewById(R.id.upload_file_size);
|
||||
fileSizeTextView.setText(DisplayUtils.bytesToHumanReadable(upload.getFileSize()) + ", ");
|
||||
if (upload.getFileSize() != 0) {
|
||||
fileSizeTextView.setText(DisplayUtils.bytesToHumanReadable(upload.getFileSize()) + ", ");
|
||||
} else {
|
||||
fileSizeTextView.setText("");
|
||||
}
|
||||
|
||||
//* upload date
|
||||
TextView uploadDateTextView = (TextView) view.findViewById(R.id.upload_date);
|
||||
|
@ -269,6 +273,7 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
|
|||
/// Reset fields visibility
|
||||
uploadDateTextView.setVisibility(View.VISIBLE);
|
||||
pathTextView.setVisibility(View.VISIBLE);
|
||||
|
||||
fileSizeTextView.setVisibility(View.VISIBLE);
|
||||
accountNameTextView.setVisibility(View.VISIBLE);
|
||||
statusTextView.setVisibility(View.VISIBLE);
|
||||
|
|
Loading…
Reference in a new issue