Skip to content

Commit 31a3de4

Browse files
move upload call to uploadSmallFile function
1 parent a7e415d commit 31a3de4

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

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

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import ratismal.drivebackup.util.MessageUtil;
2121
import ratismal.drivebackup.util.NetUtil;
2222

23+
import java.io.File;
2324
import java.io.IOException;
2425
import java.io.RandomAccessFile;
2526
import java.util.Arrays;
@@ -38,7 +39,7 @@ public class OneDriveUploader extends Uploader {
3839
public static final int MAX_RETRY_ATTEMPTS = 3;
3940

4041
private final UploadLogger logger;
41-
42+
4243
private long totalUploaded;
4344
private String accessToken = "";
4445
private String refreshToken;
@@ -47,6 +48,7 @@ public class OneDriveUploader extends Uploader {
4748

4849
private static final MediaType zipMediaType = MediaType.parse("application/zip; charset=utf-8");
4950
private static final MediaType jsonMediaType = MediaType.parse("application/json; charset=utf-8");
51+
private static final MediaType textMediaType = MediaType.parse("text/plain");
5052

5153
/**
5254
* Size of the file chunks to upload to OneDrive
@@ -108,26 +110,13 @@ public boolean isAuthenticated() {
108110
* @param testFile the file to upload during the test
109111
*/
110112
@Override
111-
public void test(java.io.File testFile) {
113+
public void test(File testFile) {
112114
try {
113115
String destination = normalizePath(ConfigParser.getConfig().backupStorage.remoteDirectory);
114116
FQID destinationId = createPath(destination);
115-
Request uploadRequest = new Request.Builder()
116-
.addHeader("Authorization", "Bearer " + accessToken)
117-
.url("https://graph.microsoft.com/v1.0/drives/" + destinationId.driveId + "/items/" + destinationId.itemId
118-
+ ":/" + testFile.getName() + ":/content")
119-
.put(RequestBody.create(testFile, MediaType.parse("plain/txt")))
120-
.build();
121-
String testFileId;
122-
try (Response response = DriveBackup.httpClient.newCall(uploadRequest).execute()) {
123-
if (response.code() != 201) {
124-
setErrorOccurred(true);
125-
}
126-
JSONObject parsedResponse = new JSONObject(response.body().string());
127-
testFileId = parsedResponse.getString("id");
128-
}
117+
FQID testFileId = uploadSmallFile(testFile, destinationId);
129118
TimeUnit.SECONDS.sleep(5);
130-
recycleItem(destinationId.driveId, testFileId);
119+
recycleItem(testFileId.driveId, testFileId.itemId);
131120
} catch (Exception exception) {
132121
NetUtil.catchException(exception, "graph.microsoft.com", logger);
133122
MessageUtil.sendConsoleException(exception);
@@ -141,7 +130,7 @@ public void test(java.io.File testFile) {
141130
* @param location of the file (ex. plugins, world)
142131
*/
143132
@Override
144-
public void uploadFile(java.io.File file, String location) throws IOException {
133+
public void uploadFile(File file, String location) throws IOException {
145134
try {
146135
resetRanges();
147136
String destinationRoot = normalizePath(ConfigParser.getConfig().backupStorage.remoteDirectory);
@@ -436,6 +425,31 @@ private void recycleItem(@NotNull String driveId, @NotNull String itemId) throws
436425
}
437426
}
438427

428+
/**
429+
* upload a file up to 250MB in size
430+
* @param file to upload
431+
* @param destinationFolder to upload the file into
432+
* @return FQID of the uploaded file
433+
* @throws IOException if the request could not be executed
434+
* @throws GraphApiErrorException if the file could not be uploaded
435+
*/
436+
@NotNull
437+
private FQID uploadSmallFile(@NotNull File file, @NotNull FQID destinationFolder) throws IOException, GraphApiErrorException {
438+
Request uploadRequest = new Request.Builder()
439+
.addHeader("Authorization", "Bearer " + accessToken)
440+
.url("https://graph.microsoft.com/v1.0/drives/" + destinationFolder.driveId + "/items/" + destinationFolder.itemId
441+
+ ":/" + file.getName() + ":/content")
442+
.put(RequestBody.create(file, textMediaType))
443+
.build();
444+
try (Response response = DriveBackup.httpClient.newCall(uploadRequest).execute()) {
445+
if (response.code() != 201) {
446+
throw new GraphApiErrorException(response);
447+
}
448+
JSONObject parsedResponse = new JSONObject(response.body().string());
449+
return new FQID(destinationFolder.driveId, parsedResponse.getString("id"));
450+
}
451+
}
452+
439453
/**
440454
* Deletes the oldest files in the specified folder past the number to retain from the authenticated user's OneDrive.
441455
* <p>

0 commit comments

Comments
 (0)