Skip to content

Commit 6f0e82e

Browse files
committed
fix googledrive test not splitting remote dir string
1 parent d858558 commit 6f0e82e

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

DriveBackup/src/main/java/ratismal/drivebackup/uploaders/googledrive/GoogleDriveUploader.java

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,34 @@
33
import com.google.api.client.auth.oauth2.BearerToken;
44
import com.google.api.client.auth.oauth2.Credential;
55
import com.google.api.client.http.FileContent;
6-
import com.google.api.client.http.HttpRequest;
76
import com.google.api.client.http.HttpRequestInitializer;
87
import com.google.api.client.http.HttpTransport;
98
import com.google.api.client.http.javanet.NetHttpTransport;
109
import com.google.api.client.json.JsonFactory;
1110
import com.google.api.client.json.jackson2.JacksonFactory;
1211
import com.google.api.services.drive.Drive;
13-
import com.google.api.services.drive.model.*;
12+
import com.google.api.services.drive.model.ChildList;
13+
import com.google.api.services.drive.model.ChildReference;
14+
import com.google.api.services.drive.model.File;
15+
import com.google.api.services.drive.model.FileList;
16+
import com.google.api.services.drive.model.ParentReference;
1417
import okhttp3.FormBody;
1518
import okhttp3.Request;
1619
import okhttp3.RequestBody;
1720
import okhttp3.Response;
18-
21+
import org.bukkit.command.CommandSender;
22+
import org.bukkit.entity.Player;
1923
import org.jetbrains.annotations.Contract;
2024
import org.jetbrains.annotations.NotNull;
2125
import org.jetbrains.annotations.Nullable;
22-
import ratismal.drivebackup.uploaders.Uploader;
23-
import ratismal.drivebackup.uploaders.Authenticator;
24-
import ratismal.drivebackup.uploaders.Obfusticate;
25-
import ratismal.drivebackup.uploaders.Authenticator.AuthenticationProvider;
26+
import org.json.JSONObject;
2627
import ratismal.drivebackup.UploadThread.UploadLogger;
2728
import ratismal.drivebackup.config.ConfigParser;
2829
import ratismal.drivebackup.plugin.DriveBackup;
30+
import ratismal.drivebackup.uploaders.Authenticator;
31+
import ratismal.drivebackup.uploaders.Authenticator.AuthenticationProvider;
32+
import ratismal.drivebackup.uploaders.Obfusticate;
33+
import ratismal.drivebackup.uploaders.Uploader;
2934
import ratismal.drivebackup.util.MessageUtil;
3035
import ratismal.drivebackup.util.NetUtil;
3136

@@ -36,10 +41,6 @@
3641
import java.util.List;
3742
import java.util.concurrent.TimeUnit;
3843

39-
import org.bukkit.command.CommandSender;
40-
import org.bukkit.entity.Player;
41-
import org.json.JSONObject;
42-
4344
import static ratismal.drivebackup.config.Localization.intl;
4445

4546
/**
@@ -150,17 +151,11 @@ private static HttpRequestInitializer setTimeout(final HttpRequestInitializer re
150151
public void test(java.io.File testFile) {
151152
try {
152153
String sharedDriveId = ConfigParser.getConfig().backupMethods.googleDrive.sharedDriveId;
153-
String destination = ConfigParser.getConfig().backupStorage.remoteDirectory;
154+
File folder = getRemoteDir(null, sharedDriveId);
154155
File body = new File();
155156
body.setTitle(testFile.getName());
156157
body.setDescription("DriveBackupV2 test file");
157158
FileContent testContent = new FileContent("plain/txt", testFile);
158-
File folder;
159-
if (!sharedDriveId.isEmpty()) {
160-
folder = createFolder(destination, sharedDriveId);
161-
} else {
162-
folder = createFolder(destination);
163-
}
164159
ParentReference fileParent = new ParentReference();
165160
fileParent.setId(folder.getId());
166161
body.setParents(Collections.singletonList(fileParent));
@@ -174,6 +169,36 @@ public void test(java.io.File testFile) {
174169
setErrorOccurred(true);
175170
}
176171
}
172+
173+
private @NotNull List<String> getRemoteDirList(String type) {
174+
String destination = ConfigParser.getConfig().backupStorage.remoteDirectory;
175+
List<String> typeFolders = new ArrayList<>(10);
176+
Collections.addAll(typeFolders, destination.split("[/\\\\]"));
177+
if (type != null) {
178+
Collections.addAll(typeFolders, type.split("[/\\\\]"));
179+
}
180+
return typeFolders;
181+
}
182+
183+
private File getRemoteDir(String type, String sharedDriveId) throws Exception {
184+
List<String> typeFolders = getRemoteDirList(type);
185+
File folder = null;
186+
for (String typeFolder : typeFolders) {
187+
if (".".equals(typeFolder) || "..".equals(typeFolder)) {
188+
continue;
189+
}
190+
if (folder == null && !sharedDriveId.isEmpty()) {
191+
folder = createFolder(typeFolder, sharedDriveId);
192+
} else if (folder == null) {
193+
folder = createFolder(typeFolder);
194+
} else if (!sharedDriveId.isEmpty()) {
195+
folder = createFolder(typeFolder, folder, true);
196+
} else {
197+
folder = createFolder(typeFolder, folder, false);
198+
}
199+
}
200+
return folder;
201+
}
177202

178203
/**
179204
* Uploads the specified file to the authenticated user's Google Drive inside a folder for the specified file type.
@@ -183,26 +208,8 @@ public void test(java.io.File testFile) {
183208
public void uploadFile(java.io.File file, String type) {
184209
try {
185210
String sharedDriveId = ConfigParser.getConfig().backupMethods.googleDrive.sharedDriveId;
186-
String destination = ConfigParser.getConfig().backupStorage.remoteDirectory;
187211
retrieveNewAccessToken();
188-
ArrayList<String> typeFolders = new ArrayList<>();
189-
Collections.addAll(typeFolders, destination.split("[/\\\\]"));
190-
Collections.addAll(typeFolders, type.split("[/\\\\]"));
191-
File folder = null;
192-
for (String typeFolder : typeFolders) {
193-
if (typeFolder.equals(".") || typeFolder.equals("..")) {
194-
continue;
195-
}
196-
if (folder == null && !sharedDriveId.isEmpty()) {
197-
folder = createFolder(typeFolder, sharedDriveId);
198-
} else if (folder == null) {
199-
folder = createFolder(typeFolder);
200-
} else if (!sharedDriveId.isEmpty()) {
201-
folder = createFolder(typeFolder, folder, true);
202-
} else {
203-
folder = createFolder(typeFolder, folder, false);
204-
}
205-
}
212+
File folder = getRemoteDir(type, sharedDriveId);
206213
File fileMetadata = new File();
207214
fileMetadata.setTitle(file.getName());
208215
fileMetadata.setDescription("Uploaded by the DriveBackupV2 Minecraft plugin");

0 commit comments

Comments
 (0)