Skip to content

Commit a7e415d

Browse files
move delete calls to recycleItem function
1 parent 77f21de commit a7e415d

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,7 @@ public void test(java.io.File testFile) {
127127
testFileId = parsedResponse.getString("id");
128128
}
129129
TimeUnit.SECONDS.sleep(5);
130-
Request delteRequest = new Request.Builder()
131-
.addHeader("Authorization", "Bearer " + accessToken)
132-
.url("https://graph.microsoft.com/v1.0/drives/" + destinationId.driveId + "/items/" + testFileId)
133-
.delete()
134-
.build();
135-
try (Response response = DriveBackup.httpClient.newCall(delteRequest).execute()) {
136-
if (response.code() != 204) {
137-
setErrorOccurred(true);
138-
}
139-
}
130+
recycleItem(destinationId.driveId, testFileId);
140131
} catch (Exception exception) {
141132
NetUtil.catchException(exception, "graph.microsoft.com", logger);
142133
MessageUtil.sendConsoleException(exception);
@@ -424,6 +415,27 @@ private FQID getFolder(@NotNull FQID root, @NotNull String folder) {
424415
return null;
425416
}
426417

418+
/**
419+
* moves an item to the recycle bin
420+
*
421+
* @param driveId the ID of the drive of the item
422+
* @param itemId the ID of the item to be deleted
423+
* @throws IOException if the request could not be executed
424+
* @throws GraphApiErrorException if the item was not deleted
425+
*/
426+
private void recycleItem(@NotNull String driveId, @NotNull String itemId) throws IOException, GraphApiErrorException {
427+
Request delteRequest = new Request.Builder()
428+
.addHeader("Authorization", "Bearer " + accessToken)
429+
.url("https://graph.microsoft.com/v1.0/drives/" + driveId + "/items/" + itemId)
430+
.delete()
431+
.build();
432+
try (Response response = DriveBackup.httpClient.newCall(delteRequest).execute()) {
433+
if (response.code() != 204 && response.code() != 404) {
434+
throw new GraphApiErrorException(response);
435+
}
436+
}
437+
}
438+
427439
/**
428440
* Deletes the oldest files in the specified folder past the number to retain from the authenticated user's OneDrive.
429441
* <p>
@@ -456,13 +468,8 @@ private void pruneBackups(@NotNull FQID parent) throws Exception, JSONException
456468
"file-limit", String.valueOf(fileLimit));
457469
int itemsToDelete = items.length() - fileLimit;
458470
for (int i = 0; i < itemsToDelete; i++) {
459-
String fileIDValue = items.getJSONObject(i).getString("id");
460-
Request deleteRequest = new Request.Builder()
461-
.addHeader("Authorization", "Bearer " + accessToken)
462-
.url("https://graph.microsoft.com/v1.0/drives/" + parent.driveId + "/items/" + fileIDValue)
463-
.delete()
464-
.build();
465-
DriveBackup.httpClient.newCall(deleteRequest).execute().close(); // TODO handle deletion failure
471+
String itemId = items.getJSONObject(i).getString("id");
472+
recycleItem(parent.driveId, itemId);
466473
}
467474
// TODO handle @odata.nextLink
468475
}

0 commit comments

Comments
 (0)