Skip to content

Commit 3f22d01

Browse files
nitzanjtocker
authored andcommitted
Add offset to uploadLargePartsto support upload resume.
1 parent 01ea940 commit 3f22d01

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

cloudinary-core/src/main/java/com/cloudinary/Uploader.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ public Map uploadLarge(Object file, Map options, int bufferSize) throws IOExcept
110110
}
111111

112112
public Map uploadLarge(Object file, Map options, int bufferSize, ProgressCallback progressCallback) throws IOException {
113+
return uploadLarge(file, options, bufferSize, 0, null, progressCallback);
114+
}
115+
116+
public Map uploadLarge(Object file, Map options, int bufferSize, long offset, String uniqueUploadId, ProgressCallback progressCallback) throws IOException {
113117
InputStream input;
114118
long length = -1;
115119
boolean remote = false;
@@ -136,7 +140,7 @@ public Map uploadLarge(Object file, Map options, int bufferSize, ProgressCallbac
136140
if (remote) {
137141
result = upload(file, options);
138142
} else {
139-
result = uploadLargeParts(input, options, bufferSize, length, progressCallback);
143+
result = uploadLargeParts(input, options, bufferSize, length, offset, uniqueUploadId, progressCallback);
140144
}
141145
return result;
142146
} finally {
@@ -146,24 +150,25 @@ public Map uploadLarge(Object file, Map options, int bufferSize, ProgressCallbac
146150
}
147151
}
148152

149-
private Map uploadLargeParts(InputStream input, Map options, int bufferSize, long length, final ProgressCallback progressCallback) throws IOException {
153+
private Map uploadLargeParts(InputStream input, Map options, int bufferSize, long length, long offset, String uniqueUploadId, final ProgressCallback progressCallback) throws IOException {
150154
Map params = buildUploadParams(options);
151155

152156
Map sentOptions = new HashMap();
153157
sentOptions.putAll(options);
154158
Map extraHeaders = new HashMap();
155-
extraHeaders.put("X-Unique-Upload-Id", cloudinary().randomPublicId());
159+
extraHeaders.put("X-Unique-Upload-Id", StringUtils.isBlank(uniqueUploadId) ? cloudinary().randomPublicId() : uniqueUploadId);
156160
sentOptions.put("extra_headers", extraHeaders);
157161

158162
byte[] buffer = new byte[bufferSize];
159163
byte[] nibbleBuffer = new byte[1];
160164
int bytesRead = 0;
161165
int currentBufferSize = 0;
162166
int partNumber = 0;
163-
long totalBytes = 0;
167+
long totalBytes = offset;
164168
Map response = null;
165169
final long knownLengthBeforeUpload = length;
166-
long totalBytesUploaded = 0;
170+
long totalBytesUploaded = offset;
171+
input.skip(offset);
167172
while (true) {
168173
bytesRead = input.read(buffer, currentBufferSize, bufferSize - currentBufferSize);
169174
boolean atEnd = bytesRead == -1;
@@ -172,7 +177,7 @@ private Map uploadLargeParts(InputStream input, Map options, int bufferSize, lon
172177

173178
if (atEnd || fullBuffer) {
174179
totalBytes += currentBufferSize;
175-
int currentLoc = bufferSize * partNumber;
180+
long currentLoc = offset + bufferSize * partNumber;
176181
if (!atEnd) {
177182
//verify not on end - try read another byte
178183
bytesRead = input.read(nibbleBuffer, 0, 1);

0 commit comments

Comments
 (0)