From f4ed9558ebd45edf3f1902387a88e6c41cb45929 Mon Sep 17 00:00:00 2001 From: sashiro Date: Wed, 19 Jul 2023 16:03:21 +0200 Subject: [PATCH] modify DropboxManager.java add support for large DropBox Uploads (>= 150MiB) --- .../serverbackup/utils/DropboxManager.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/de/seblii/serverbackup/utils/DropboxManager.java b/src/de/seblii/serverbackup/utils/DropboxManager.java index eec9fa6e..e93f60bd 100644 --- a/src/de/seblii/serverbackup/utils/DropboxManager.java +++ b/src/de/seblii/serverbackup/utils/DropboxManager.java @@ -66,7 +66,33 @@ public void uploadToDropbox(String filePath) { String des = ServerBackup.getInstance().getConfig().getString("CloudBackup.Options.Destination").replaceAll("/", ""); try (InputStream in = new FileInputStream(filePath)) { - FileMetadata metadata = client.files().uploadBuilder("/" + (des.equals("") ? "" : des + "/") + file.getName()).uploadAndFinish(in); + // Step 1: Upload session start + UploadSessionStartUploader uploadSessionStart = client.files().uploadSessionStart(); + UploadSessionStartResult uploadSessionStartResult = uploadSessionStart.uploadAndFinish(in); + String sessionId = uploadSessionStartResult.getSessionId(); + + // Step 2: Upload large file in chunks + long fileSize = file.length(); + byte[] buffer = new byte[4 * 1000 * 1000]; // 4 MB chunk size + long uploaded = 0; + + int bytesRead; + while ((bytesRead = in.read(buffer)) != -1) { + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer, 0, bytesRead); + UploadSessionCursor cursor = new UploadSessionCursor(sessionId, uploaded); + + // Append data to the session + client.files().uploadSessionAppendV2(cursor).uploadAndFinish(byteArrayInputStream, bytesRead); + uploaded += bytesRead; + } + + // Step 3: Upload session finish + UploadSessionCursor cursor = new UploadSessionCursor(sessionId, fileSize); + CommitInfo commitInfo = CommitInfo.newBuilder("/" + (des.equals("") ? "" : des + "/") + file.getName()) + .withClientModified(new Date()) + .withMode(WriteMode.ADD) + .build(); + FileMetadata metadata = client.files().uploadSessionFinish(cursor, commitInfo).finish(); } catch (UploadErrorException e) { throw new RuntimeException(e); } catch (IOException e) { @@ -82,5 +108,4 @@ public void uploadToDropbox(String filePath) { } } } - -} +} \ No newline at end of file