move logic completely into function

prevent double subfolders

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
This commit is contained in:
tobiasKaminsky 2019-10-10 12:01:35 +02:00
parent 3e444725a9
commit a2f2da9c20
No known key found for this signature in database
GPG key ID: 0E00D4D47D0C5AF7
3 changed files with 103 additions and 83 deletions

View file

@ -64,8 +64,6 @@ import java.util.TimeZone;
import androidx.annotation.NonNull;
import androidx.exifinterface.media.ExifInterface;
import static com.owncloud.android.datamodel.OCFile.PATH_SEPARATOR;
/*
Job that:
- restarts existing jobs if required
@ -198,25 +196,15 @@ public class FilesSyncJob extends Job {
remotePath = syncedFolder.getRemotePath();
}
if (!subfolderByDate) {
String adaptedPath = file.getAbsolutePath()
.replace(syncedFolder.getLocalPath(), "")
.replace(PATH_SEPARATOR + file.getName(), "");
remotePath += adaptedPath;
}
String relativeSubfolderPath = new File(path.replace(syncedFolder.getLocalPath(), ""))
.getParentFile().getAbsolutePath();
requester.uploadFileWithOverwrite(
context,
account,
file.getAbsolutePath(),
FileStorageUtils.getInstantUploadFilePath(
file,
currentLocale,
remotePath,
relativeSubfolderPath,
file.getName(),
syncedFolder.getLocalPath(),
lastModificationTime,
subfolderByDate),
uploadAction,

View file

@ -54,7 +54,6 @@ import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@ -159,14 +158,13 @@ public final class FileStorageUtils {
/**
* Returns the InstantUploadFilePath on the nextcloud instance
*
* @param fileName complete file name
* @param dateTaken: Time in milliseconds since 1970 when the picture was taken.
* @return instantUpload path, eg. /Camera/2017/01/fileName
*/
public static String getInstantUploadFilePath(Locale current,
public static String getInstantUploadFilePath(File file,
Locale current,
String remotePath,
String subfolder,
@Nullable String fileName,
String syncedFolderLocalPath,
long dateTaken,
Boolean subfolderByDate) {
String subfolderByDatePath = "";
@ -174,13 +172,17 @@ public final class FileStorageUtils {
subfolderByDatePath = getSubPathFromDate(dateTaken, current);
}
String relativeSubfolderPath = new File(file.getAbsolutePath().replace(syncedFolderLocalPath, ""))
.getParentFile().getAbsolutePath();
// Path must be normalized; otherwise the next RefreshFolderOperation has a mismatch and deletes the local file.
return (remotePath +
OCFile.PATH_SEPARATOR +
subfolderByDatePath +
subfolder + // starts with / so no separator is needed
OCFile.PATH_SEPARATOR +
(fileName == null ? "" : fileName))
relativeSubfolderPath +
OCFile.PATH_SEPARATOR +
file.getName())
.replaceAll(OCFile.PATH_SEPARATOR + "+", OCFile.PATH_SEPARATOR);
}

View file

@ -24,109 +24,139 @@ package com.nextcloud.client.utils
import com.owncloud.android.utils.FileStorageUtils
import org.junit.Assert.assertEquals
import org.junit.Test
import java.io.File
import java.util.Locale
class FileStorageUtilsTest {
@Test
fun testInstantUploadPathSubfolder() {
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
"/remotePath/",
"subfolder",
"file.pdf",
123123123L,
false)
val expected = "/remotePath/subfolder/file.pdf"
val file = File("/sdcard/DCIM/subfolder/file.jpg")
val syncedFolderLocalPath = "/sdcard/DCIM"
val syncedFolderRemotePath = "/Camera"
val subFolderByDate = false
val dateTaken = 123123123L
val result = FileStorageUtils.getInstantUploadFilePath(file,
Locale.ROOT,
syncedFolderRemotePath,
syncedFolderLocalPath,
dateTaken,
subFolderByDate)
val expected = "/Camera/subfolder/file.jpg"
assertEquals(expected, result)
}
@Test
fun testInstantUploadPathNoSubfolder() {
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
"/remotePath/",
"",
"file.pdf",
123123123L,
false)
val expected = "/remotePath/file.pdf"
val file = File("/sdcard/DCIM/file.jpg")
val syncedFolderLocalPath = "/sdcard/DCIM"
val syncedFolderRemotePath = "/Camera"
val subFolderByDate = false
val dateTaken = 123123123L
val result = FileStorageUtils.getInstantUploadFilePath(file,
Locale.ROOT,
syncedFolderRemotePath,
syncedFolderLocalPath,
dateTaken,
subFolderByDate)
val expected = "/Camera/file.jpg"
assertEquals(expected, result)
}
@Test
fun testInstantUploadPathNullFilename() {
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
"/remotePath/",
"subfolder",
null,
123123123L,
false)
val expected = "/remotePath/subfolder/"
fun testInstantUploadPathEmptyDateZero() {
val file = File("/sdcard/DCIM/file.jpg")
val syncedFolderLocalPath = "/sdcard/DCIM"
val syncedFolderRemotePath = "/Camera"
val subFolderByDate = true
val dateTaken = 0L
assertEquals(expected, result)
}
@Test
fun testInstantUploadPathNullEmptyDate() {
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
"/remotePath/",
"",
"file.pdf",
0,
true)
val expected = "/remotePath/file.pdf"
val result = FileStorageUtils.getInstantUploadFilePath(file,
Locale.ROOT,
syncedFolderRemotePath,
syncedFolderLocalPath,
dateTaken,
subFolderByDate)
val expected = "/Camera/file.jpg"
assertEquals(expected, result)
}
@Test
fun testInstantUploadPath() {
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
"/remotePath/",
"",
"file.pdf",
123123123L,
false)
val expected = "/remotePath/file.pdf"
val file = File("/sdcard/DCIM/file.jpg")
val syncedFolderLocalPath = "/sdcard/DCIM"
val syncedFolderRemotePath = "/Camera"
val subFolderByDate = false
val dateTaken = 123123123L
val result = FileStorageUtils.getInstantUploadFilePath(file,
Locale.ROOT,
syncedFolderRemotePath,
syncedFolderLocalPath,
dateTaken,
subFolderByDate)
val expected = "/Camera/file.jpg"
assertEquals(expected, result)
}
@Test
fun testInstantUploadPathWithSubfolderByDate() {
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
"/remotePath/",
"",
"file.pdf",
1569918628000,
true)
val expected = "/remotePath/2019/10/file.pdf"
val file = File("/sdcard/DCIM/file.jpg")
val syncedFolderLocalPath = "/sdcard/DCIM"
val syncedFolderRemotePath = "/Camera"
val subFolderByDate = true
val dateTaken = 1569918628000L
val result = FileStorageUtils.getInstantUploadFilePath(file,
Locale.ROOT,
syncedFolderRemotePath,
syncedFolderLocalPath,
dateTaken,
subFolderByDate)
val expected = "/Camera/2019/10/file.jpg"
assertEquals(expected, result)
}
@Test
fun testInstantUploadPathWithSubfolderFile() {
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
"/remotePath/",
"",
"/sub/file.pdf",
123123123L,
false)
val expected = "/remotePath/sub/file.pdf"
val file = File("/sdcard/DCIM/subfolder/file.jpg")
val syncedFolderLocalPath = "/sdcard/DCIM"
val syncedFolderRemotePath = "/Camera"
val subFolderByDate = false
val dateTaken = 123123123L
val result = FileStorageUtils.getInstantUploadFilePath(file,
Locale.ROOT,
syncedFolderRemotePath,
syncedFolderLocalPath,
dateTaken,
subFolderByDate)
val expected = "/Camera/subfolder/file.jpg"
assertEquals(expected, result)
}
@Test
fun testInstantUploadPathWithSubfolderByDateWithSubfolderFile() {
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
"/remotePath/",
"",
"/sub/file.pdf",
1569918628000,
true)
val expected = "/remotePath/2019/10/sub/file.pdf"
val file = File("/sdcard/DCIM/subfolder/file.jpg")
val syncedFolderLocalPath = "/sdcard/DCIM"
val syncedFolderRemotePath = "/Camera"
val subFolderByDate = true
val dateTaken = 1569918628000L
val result = FileStorageUtils.getInstantUploadFilePath(file,
Locale.ROOT,
syncedFolderRemotePath,
syncedFolderLocalPath,
dateTaken,
subFolderByDate)
val expected = "/Camera/2019/10/subfolder/file.jpg"
assertEquals(expected, result)
}