Merge pull request #4425 from nextcloud/autoUploadSubfolder
Upload files into subfolder
This commit is contained in:
4 files changed
+65
-22
No files matched your search
@@ -185,14 +185,13 @@ public class SyncedFolderProvider extends Observable {
|
||||
}
|
||||
|
||||
public SyncedFolder findByLocalPathAndAccount(String localPath, Account account) {
|
||||
|
||||
SyncedFolder result = null;
|
||||
Cursor cursor = mContentResolver.query(
|
||||
ProviderMeta.ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS,
|
||||
null,
|
||||
ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_LOCAL_PATH + "=? AND " +
|
||||
ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_LOCAL_PATH + "LIKE ? AND " +
|
||||
ProviderMeta.ProviderTableMeta.SYNCED_FOLDER_ACCOUNT + " =? ",
|
||||
new String[]{localPath, account.name},
|
||||
new String[]{localPath + "%", account.name},
|
||||
null
|
||||
);
|
||||
|
||||
|
||||
@@ -205,14 +205,20 @@ public class FilesSyncJob extends Job {
|
||||
remotePath += adaptedPath;
|
||||
}
|
||||
|
||||
String relativeSubfolderPath = new File(path.replace(syncedFolder.getLocalPath(), ""))
|
||||
.getParentFile().getAbsolutePath();
|
||||
|
||||
requester.uploadFileWithOverwrite(
|
||||
context,
|
||||
account,
|
||||
file.getAbsolutePath(),
|
||||
FileStorageUtils.getInstantUploadFilePath(
|
||||
currentLocale,
|
||||
remotePath, file.getName(),
|
||||
lastModificationTime, subfolderByDate),
|
||||
currentLocale,
|
||||
remotePath,
|
||||
relativeSubfolderPath,
|
||||
file.getName(),
|
||||
lastModificationTime,
|
||||
subfolderByDate),
|
||||
uploadAction,
|
||||
mimeType,
|
||||
true, // create parent folder if not existent
|
||||
@@ -223,7 +229,7 @@ public class FilesSyncJob extends Job {
|
||||
);
|
||||
|
||||
filesystemDataProvider.updateFilesystemFileAsSentForUpload(path,
|
||||
Long.toString(syncedFolder.getId()));
|
||||
Long.toString(syncedFolder.getId()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -165,16 +165,22 @@ public final class FileStorageUtils {
|
||||
*/
|
||||
public static String getInstantUploadFilePath(Locale current,
|
||||
String remotePath,
|
||||
String subfolder,
|
||||
@Nullable String fileName,
|
||||
long dateTaken,
|
||||
Boolean subfolderByDate) {
|
||||
String subPath = "";
|
||||
String subfolderByDatePath = "";
|
||||
if (subfolderByDate) {
|
||||
subPath = getSubPathFromDate(dateTaken, current);
|
||||
subfolderByDatePath = getSubPathFromDate(dateTaken, current);
|
||||
}
|
||||
|
||||
// Path must be normalized; otherwise the next RefreshFolderOperation has a mismatch and deletes the local file.
|
||||
return (remotePath + OCFile.PATH_SEPARATOR + subPath + (fileName == null ? "" : fileName))
|
||||
return (remotePath +
|
||||
OCFile.PATH_SEPARATOR +
|
||||
subfolderByDatePath +
|
||||
subfolder + // starts with / so no separator is needed
|
||||
OCFile.PATH_SEPARATOR +
|
||||
(fileName == null ? "" : fileName))
|
||||
.replaceAll(OCFile.PATH_SEPARATOR + "+", OCFile.PATH_SEPARATOR);
|
||||
}
|
||||
|
||||
|
||||
@@ -27,14 +27,41 @@ import org.junit.Test
|
||||
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"
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInstantUploadPathNoSubfolder() {
|
||||
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
|
||||
"/remotePath/",
|
||||
"",
|
||||
"file.pdf",
|
||||
123123123L,
|
||||
false)
|
||||
val expected = "/remotePath/file.pdf"
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInstantUploadPathNullFilename() {
|
||||
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
|
||||
"/subfolder/",
|
||||
"/remotePath/",
|
||||
"subfolder",
|
||||
null,
|
||||
123123123L,
|
||||
false)
|
||||
val expected = "/subfolder/"
|
||||
val expected = "/remotePath/subfolder/"
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
@@ -42,11 +69,12 @@ class FileStorageUtilsTest {
|
||||
@Test
|
||||
fun testInstantUploadPathNullEmptyDate() {
|
||||
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
|
||||
"/subfolder/",
|
||||
"/remotePath/",
|
||||
"",
|
||||
"file.pdf",
|
||||
0,
|
||||
true)
|
||||
val expected = "/subfolder/file.pdf"
|
||||
val expected = "/remotePath/file.pdf"
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
@@ -54,11 +82,12 @@ class FileStorageUtilsTest {
|
||||
@Test
|
||||
fun testInstantUploadPath() {
|
||||
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
|
||||
"/subfolder/",
|
||||
"/remotePath/",
|
||||
"",
|
||||
"file.pdf",
|
||||
123123123L,
|
||||
false)
|
||||
val expected = "/subfolder/file.pdf"
|
||||
val expected = "/remotePath/file.pdf"
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
@@ -66,11 +95,12 @@ class FileStorageUtilsTest {
|
||||
@Test
|
||||
fun testInstantUploadPathWithSubfolderByDate() {
|
||||
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
|
||||
"/subfolder/",
|
||||
"/remotePath/",
|
||||
"",
|
||||
"file.pdf",
|
||||
1569918628000,
|
||||
true)
|
||||
val expected = "/subfolder/2019/10/file.pdf"
|
||||
val expected = "/remotePath/2019/10/file.pdf"
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
@@ -78,11 +108,12 @@ class FileStorageUtilsTest {
|
||||
@Test
|
||||
fun testInstantUploadPathWithSubfolderFile() {
|
||||
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
|
||||
"/subfolder/",
|
||||
"/remotePath/",
|
||||
"",
|
||||
"/sub/file.pdf",
|
||||
123123123L,
|
||||
false)
|
||||
val expected = "/subfolder/sub/file.pdf"
|
||||
val expected = "/remotePath/sub/file.pdf"
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
@@ -90,11 +121,12 @@ class FileStorageUtilsTest {
|
||||
@Test
|
||||
fun testInstantUploadPathWithSubfolderByDateWithSubfolderFile() {
|
||||
val result = FileStorageUtils.getInstantUploadFilePath(Locale.ROOT,
|
||||
"/subfolder/",
|
||||
"/remotePath/",
|
||||
"",
|
||||
"/sub/file.pdf",
|
||||
1569918628000,
|
||||
true)
|
||||
val expected = "/subfolder/2019/10/sub/file.pdf"
|
||||
val expected = "/remotePath/2019/10/sub/file.pdf"
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user