Skip to content

Commit 1b6b848

Browse files
committed
Add SyncRepo.storeFile.
This is to support storing a file into a directory path in the repo. Only implemented for ContentRepo and DirectoryRepo for now.
1 parent 32c8c0d commit 1b6b848

File tree

9 files changed

+71
-4
lines changed

9 files changed

+71
-4
lines changed

app/src/main/java/com/orgzly/android/data/DataRepository.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,6 @@ class DataRepository @Inject constructor(
15551555
val fileName = documentFile.name
15561556

15571557
val attachDir = notePayload.attachDir(context)
1558-
val filePath = attachDir + File.separator + fileName
15591558

15601559
val book = getBookView(bookId)
15611560
?: throw IOException(resources.getString(R.string.book_does_not_exist_anymore))
@@ -1572,7 +1571,7 @@ class DataRepository @Inject constructor(
15721571
LogUtils.d(TAG, "Wrote to file $tempFile")
15731572
}
15741573

1575-
repo.storeBook(tempFile, filePath)
1574+
repo.storeFile(tempFile, attachDir, fileName)
15761575
LogUtils.d(TAG, "Stored file to repo")
15771576
tempFile.delete()
15781577
}

app/src/main/java/com/orgzly/android/repos/ContentRepo.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,25 @@ public VersionedRook retrieveBook(String fileName, File destinationFile) throws
128128

129129
@Override
130130
public VersionedRook storeBook(File file, String fileName) throws IOException {
131+
return storeFile(file, "", fileName);
132+
}
133+
134+
@Override
135+
public VersionedRook storeFile(File file, String pathInRepo, String fileName) throws IOException {
131136
if (!file.exists()) {
132137
throw new FileNotFoundException("File " + file + " does not exist");
133138
}
134139

140+
DocumentFile documentFile = createRecursive(repoDocumentFile, pathInRepo);
141+
135142
/* Delete existing file. */
136-
DocumentFile existingFile = repoDocumentFile.findFile(fileName);
143+
DocumentFile existingFile = documentFile.findFile(fileName);
137144
if (existingFile != null) {
138145
existingFile.delete();
139146
}
140147

141148
/* Create new file. */
142-
DocumentFile destinationFile = repoDocumentFile.createFile("text/*", fileName);
149+
DocumentFile destinationFile = documentFile.createFile("text/*", fileName);
143150

144151
if (destinationFile == null) {
145152
throw new IOException("Failed creating " + fileName + " in " + repoUri);
@@ -163,6 +170,27 @@ public VersionedRook storeBook(File file, String fileName) throws IOException {
163170
return new VersionedRook(repoId, RepoType.DOCUMENT, getUri(), uri, rev, mtime);
164171
}
165172

173+
private DocumentFile createRecursive(DocumentFile parent, String path) {
174+
if (".".equals(path) || "".equals(path)) {
175+
return parent;
176+
}
177+
int l = path.lastIndexOf('/');
178+
DocumentFile p;
179+
if (l >= 0) {
180+
p = createRecursive(parent, path.substring(0, l));
181+
} else {
182+
p = parent;
183+
}
184+
String subdir = path.substring(l+1);
185+
DocumentFile f = p.findFile(subdir);
186+
if (f != null) {
187+
// already exist, return it
188+
return f;
189+
}
190+
// Otherwise, create the directory
191+
return p.createDirectory(subdir);
192+
}
193+
166194
@Override
167195
public VersionedRook renameBook(Uri from, String name) throws IOException {
168196
DocumentFile fromDocFile = DocumentFile.fromSingleUri(context, from);

app/src/main/java/com/orgzly/android/repos/DatabaseRepo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public VersionedRook storeBook(File file, String fileName) throws IOException {
6666
return dbRepo.createBook(repoId, vrook, content);
6767
}
6868

69+
@Override
70+
public VersionedRook storeFile(File file, String pathInRepo, String fileName) throws IOException {
71+
throw new UnsupportedOperationException();
72+
}
73+
6974
@Override
7075
public VersionedRook renameBook(Uri fromUri, String name) {
7176
Uri toUri = UriUtils.getUriForNewName(fromUri, name);

app/src/main/java/com/orgzly/android/repos/DirectoryRepo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ public VersionedRook storeBook(File file, String fileName) throws IOException {
158158
return new VersionedRook(repoId, RepoType.DIRECTORY, repoUri, uri, rev, mtime);
159159
}
160160

161+
@Override
162+
public VersionedRook storeFile(File file, String pathInRepo, String fileName) throws IOException {
163+
return storeBook(file, pathInRepo + File.separator + fileName);
164+
}
165+
161166
@Override
162167
public VersionedRook renameBook(Uri fromUri, String name) throws IOException {
163168
String fromFilePath = fromUri.getPath();

app/src/main/java/com/orgzly/android/repos/DropboxRepo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public VersionedRook storeBook(File file, String fileName) throws IOException {
5151
return client.upload(file, repoUri, fileName);
5252
}
5353

54+
@Override
55+
public VersionedRook storeFile(File file, String pathInRepo, String fileName) throws IOException {
56+
throw new UnsupportedOperationException();
57+
}
58+
5459
@Override
5560
public VersionedRook renameBook(Uri fromUri, String name) throws IOException {
5661
Uri toUri = UriUtils.getUriForNewName(fromUri, name);

app/src/main/java/com/orgzly/android/repos/GitRepo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ public VersionedRook storeBook(File file, String fileName) throws IOException {
193193
return currentVersionedRook(Uri.EMPTY.buildUpon().appendPath(fileName).build());
194194
}
195195

196+
@Override
197+
public VersionedRook storeFile(File file, String pathInRepo, String fileName) throws IOException {
198+
throw new UnsupportedOperationException();
199+
}
200+
196201
private RevWalk walk() {
197202
return new RevWalk(git.getRepository());
198203
}

app/src/main/java/com/orgzly/android/repos/MockRepo.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public VersionedRook storeBook(File file, String fileName) throws IOException {
6262
return databaseRepo.storeBook(file, fileName);
6363
}
6464

65+
@Override
66+
public VersionedRook storeFile(File file, String pathInRepo, String fileName) throws IOException {
67+
SystemClock.sleep(SLEEP_FOR_STORE_BOOK);
68+
return databaseRepo.storeFile(file, pathInRepo, fileName);
69+
}
70+
6571
@Override
6672
public VersionedRook renameBook(Uri fromUri, String name) throws IOException {
6773
SystemClock.sleep(SLEEP_FOR_STORE_BOOK);

app/src/main/java/com/orgzly/android/repos/SyncRepo.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ public interface SyncRepo {
3939
*/
4040
VersionedRook storeBook(File file, String fileName) throws IOException;
4141

42+
/**
43+
* Uploads file storing it under directory (pathInRepo) under repo's url.
44+
* @param file The contents of this file should be stored at the remote location/repo
45+
* @param pathInRepo The "/" separated path within the remote location/repo, create it if it doesn't exist
46+
* @param fileName The contents ({@code file}) should be stored under this name
47+
* @return {@code VersionedRook}
48+
* @throws IOException
49+
*/
50+
VersionedRook storeFile(File file, String pathInRepo, String fileName) throws IOException;
51+
4252
VersionedRook renameBook(Uri from, String name) throws IOException;
4353

4454
// VersionedRook moveBook(Uri from, Uri uri) throws IOException;

app/src/main/java/com/orgzly/android/repos/WebdavRepo.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ class WebdavRepo(
164164
return sardine.list(fileUrl).first().toVersionedRook()
165165
}
166166

167+
override fun storeFile(file: File?, pathInRepo: String?, fileName: String?): VersionedRook {
168+
TODO("Not yet implemented")
169+
}
170+
167171
override fun renameBook(from: Uri, name: String?): VersionedRook {
168172
val destUrl = UriUtils.getUriForNewName(from, name).toUrl()
169173
sardine.move(from.toUrl(), destUrl)

0 commit comments

Comments
 (0)