Skip to content

Commit da73a7e

Browse files
use new logic for path creation
1 parent 4cfc84e commit da73a7e

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

DriveBackup/src/main/java/ratismal/drivebackup/uploaders/onedrive/OneDriveUploader.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ public boolean isAuthenticated() {
109109
* Tests the OneDrive account by uploading a small file
110110
* @param testFile the file to upload during the test
111111
*/
112+
@Override
112113
public void test(java.io.File testFile) {
113114
try {
114-
String destination = ConfigParser.getConfig().backupStorage.remoteDirectory;
115+
String destination = normalizePath(ConfigParser.getConfig().backupStorage.remoteDirectory);
116+
FQID destinationId = createPath(destination);
115117
Request request = new Request.Builder()
116118
.addHeader("Authorization", "Bearer " + accessToken)
117119
.url("https://graph.microsoft.com/v1.0/me/drive/root:/" + destination + "/" + testFile.getName() + ":/content")
@@ -127,7 +129,7 @@ public void test(java.io.File testFile) {
127129
request = new Request.Builder()
128130
.addHeader("Authorization", "Bearer " + accessToken)
129131
.url("https://graph.microsoft.com/v1.0/me/drive/root:/" + destination + "/" + testFile.getName() + ":/")
130-
.delete()
132+
.delete() // TODO delete permanently
131133
.build();
132134
response = DriveBackup.httpClient.newCall(request).execute();
133135
statusCode = response.code();
@@ -143,31 +145,20 @@ public void test(java.io.File testFile) {
143145
}
144146

145147
/**
146-
* Uploads the specified file to the authenticated user's OneDrive inside a folder for the specified file type.
148+
* Uploads the specified file to the authenticated user's OneDrive inside a folder for the specified file location.
147149
* @param file the file
148-
* @param type the type of file (ex. plugins, world)
150+
* @param location of the file (ex. plugins, world)
149151
*/
150-
public void uploadFile(java.io.File file, String type) throws IOException {
152+
@Override
153+
public void uploadFile(java.io.File file, String location) throws IOException {
151154
try {
152155
resetRanges();
153-
String destination = ConfigParser.getConfig().backupStorage.remoteDirectory;
154-
ArrayList<String> typeFolders = new ArrayList<>();
155-
Collections.addAll(typeFolders, destination.split("/"));
156-
Collections.addAll(typeFolders, type.split("[/\\\\]"));
157-
File folder = null;
158-
for (String typeFolder : typeFolders) {
159-
if (typeFolder.equals(".") || typeFolder.equals("..")) {
160-
continue;
161-
}
162-
if (folder == null) {
163-
folder = createFolder(typeFolder);
164-
} else {
165-
folder = createFolder(typeFolder, folder);
166-
}
167-
}
156+
String destinationRoot = normalizePath(ConfigParser.getConfig().backupStorage.remoteDirectory);
157+
String destinationPath = destinationRoot + '/' + location;
158+
FQID destinationId = createPath(destinationPath);
168159
Request request = new Request.Builder()
169160
.addHeader("Authorization", "Bearer " + accessToken)
170-
.url("https://graph.microsoft.com/v1.0/me/drive/root:/" + folder.getPath() + "/" + file.getName() + ":/createUploadSession")
161+
.url("https://graph.microsoft.com/v1.0/me/drive/root:/" + destinationPath + "/" + file.getName() + ":/createUploadSession")
171162
.post(RequestBody.create("{}", jsonMediaType))
172163
.build();
173164
JSONObject parsedResponse;
@@ -209,7 +200,7 @@ public void uploadFile(java.io.File file, String type) throws IOException {
209200
}
210201
}
211202
try {
212-
pruneBackups(folder);
203+
pruneBackups(destinationPath);
213204
} catch (Exception e) {
214205
logger.log(intl("backup-method-prune-failed"));
215206
throw e;
@@ -541,14 +532,14 @@ private File getFolder(String name) {
541532
* @param parent the folder containing the files
542533
* @throws Exception
543534
*/
544-
private void pruneBackups(File parent) throws Exception {
535+
private void pruneBackups(String parent) throws Exception {
545536
int fileLimit = ConfigParser.getConfig().backupStorage.keepCount;
546537
if (fileLimit == -1) {
547538
return;
548539
}
549540
Request request = new Request.Builder()
550541
.addHeader("Authorization", "Bearer " + accessToken)
551-
.url("https://graph.microsoft.com/v1.0/me/drive/root:/" + parent.getPath() + ":/children?sort_by=createdDateTime")
542+
.url("https://graph.microsoft.com/v1.0/me/drive/root:/" + parent + ":/children?sort_by=createdDateTime")
552543
.build();
553544
Response response = DriveBackup.httpClient.newCall(request).execute();
554545
JSONObject parsedResponse = new JSONObject(response.body().string());

0 commit comments

Comments
 (0)