-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathResumableUpload.java
217 lines (188 loc) · 8.72 KB
/
ResumableUpload.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import com.google.api.client.auth.oauth2.Credential;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
/**
* A helpful java library/class for uploading large files in chunks on google drive.
*
* @author Piyush M.
*/
public class ResumableUpload
{
public static final int CHUNK_LIMIT = 262144; // = (256*1024)
public static final int OK = 200;
public static final int CREATED = 201;
public static final int INCOMPLETE = 308;
/**
* This function returns url to which file is to be uploaded
* @param credential google credential for AccessToken
* @param jsonStructure It will be used to get structure of file it should contain
* 1) MimeType of file
* 2) Size of file
* 3) Name of file
* @return SessionUri
* @throws MalformedURLException
* @throws IOException
*/
public static String requestUploadUrl(Credential credential, com.google.api.services.drive.model.File jsonStructure) throws MalformedURLException, IOException
{
URL url = new URL("https://www.googleapis.com/upload/drive/v3/files"+((jsonStructure.getId() != null)?"/"+jsonStructure.getId():"")+"?uploadType=resumable");
HttpURLConnection req = (HttpURLConnection) url.openConnection();
req.setRequestMethod("POST");
req.setDoInput(true);
req.setDoOutput(true);
req.setRequestProperty("Authorization", "Bearer " + credential.getAccessToken());
req.setRequestProperty("X-Upload-Content-Type", jsonStructure.getMimeType());
req.setRequestProperty("X-Upload-Content-Length", String.valueOf(jsonStructure.getSize()));
req.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
String body = "{ \"name\": \""+jsonStructure.getName()+"\""+((jsonStructure.getParents() != null)?", \"parents\":[\""+jsonStructure.getParents().get(0)+"\"]":"")+" }";
req.setRequestProperty("Content-Length", String.format(Locale.ENGLISH, "%d", body.getBytes().length));
try (OutputStream outputStream = req.getOutputStream()) {
outputStream.write(body.getBytes());
}
req.connect();
String sessionUri = null;
if (req.getResponseCode() == HttpURLConnection.HTTP_OK) {
sessionUri = req.getHeaderField("location");
}
return sessionUri;
}
/**
* Uploads String packet
* @param sessionUri Last Session Url
* @param jsonStructure It will be used to get structure of file it should contain
* 1) MimeType of file
* 2) Size of file
* 3) Name of file
* @param packet Text to upload
* @param chunkStart offset of start of chunk
* @param uploadBytes length of packet must be greater than CHUNK_LIMIT except last packet
* @return Response Code
* @throws MalformedURLException
* @throws IOException
*/
public static int uploadStringPacket(String sessionUri, com.google.api.services.drive.model.File jsonStructure, String packet, long chunkStart, long uploadBytes) throws MalformedURLException, IOException
{
URL url = new URL(sessionUri);
HttpURLConnection req = (HttpURLConnection) url.openConnection();
req.setRequestMethod("PUT");
req.setDoOutput(true);
req.setDoInput(true);
req.setConnectTimeout(10000);
req.setRequestProperty("Content-Type", jsonStructure.getMimeType());
req.setRequestProperty("Content-Length", String.valueOf(uploadBytes));
req.setRequestProperty("Content-Range", "bytes " + chunkStart + "-" + (chunkStart + uploadBytes -1) + "/" + jsonStructure.getSize());
byte[] buffer = packet.substring((int)chunkStart, (int)(chunkStart + uploadBytes)).getBytes();
try (OutputStream outputStream = req.getOutputStream()) {
outputStream.write(buffer);
}
req.connect();
return req.getResponseCode();
}
/**
* Upload java.io.File packet
* @param sessionUri Last Session Url
* @param jsonStructure It will be used to get structure of file it should contain
* 1) MimeType of file
* 2) Size of file
* 3) Name of file
* @param file File to upload
* @param chunkStart offset of start of chunk
* @param uploadBytes length of packet must be greater than CHUNK_LIMIT except last packet
* @return Response Code
* @throws MalformedURLException
* @throws IOException
*/
public static int uploadFilePacket(String sessionUri, com.google.api.services.drive.model.File jsonStructure, java.io.File file, long chunkStart, long uploadBytes) throws MalformedURLException, IOException
{
URL url1 = new URL(sessionUri);
HttpURLConnection req = (HttpURLConnection) url1.openConnection();
req.setRequestMethod("PUT");
req.setDoOutput(true);
req.setDoInput(true);
req.setConnectTimeout(10000);
req.setRequestProperty("Content-Type", jsonStructure.getMimeType());
req.setRequestProperty("Content-Length", String.valueOf(uploadBytes));
req.setRequestProperty("Content-Range", "bytes " + chunkStart + "-" + (chunkStart + uploadBytes -1) + "/" + jsonStructure.getSize());
try (OutputStream outstream = req.getOutputStream()) {
byte[] buffer = new byte[(int) uploadBytes];
try (FileInputStream fileInputStream = new FileInputStream(file)) {
fileInputStream.getChannel().position(chunkStart);
if (fileInputStream.read(buffer, 0, (int) uploadBytes) != -1)
outstream.write(buffer);
}
}
req.connect();
return req.getResponseCode();
}
/**
* Upload File
* Upload java.io.File packet
* @param credential google credential for AccessToken
* @param jsonStructure It will be used to get structure of file it should contain
* 1) MimeType of file
* 2) Size of file
* 3) Name of file
* @param file File to upload
* @throws IOException
*/
public static void uploadFile(Credential credential, com.google.api.services.drive.model.File jsonStructure, java.io.File file) throws IOException, UploadFileException
{
String sessionUrl = requestUploadUrl(credential, jsonStructure);
for(long i = 1, j = CHUNK_LIMIT;i <= jsonStructure.getSize();i+=CHUNK_LIMIT)
{
if(i+CHUNK_LIMIT >= jsonStructure.getSize())
{
j = jsonStructure.getSize() - i + 1;
}
int responseCode = uploadFilePacket(sessionUrl, jsonStructure, file, i-1, j);
if(!(responseCode == OK || responseCode == CREATED || responseCode == INCOMPLETE)) throw new UploadFileException(responseCode);
}
}
/**
*
* Upload String
* @param credential google credential for AccessToken
* @param jsonStructure It will be used to get structure of file it should contain
* 1) MimeType of file
* 2) Size of file
* 3) Name of file
* @param text Text to upload
* @throws IOException
*/
public static void uploadString(Credential credential, com.google.api.services.drive.model.File jsonStructure, String text) throws IOException, UploadFileException
{
String sessionUrl = requestUploadUrl(credential, jsonStructure);
for(long i = 1, j = CHUNK_LIMIT;i <= jsonStructure.getSize();i+=CHUNK_LIMIT)
{
if(i+CHUNK_LIMIT >= jsonStructure.getSize())
{
j = jsonStructure.getSize() - i + 1;
}
int responseCode = uploadStringPacket(sessionUrl, jsonStructure, text, i-1, j);
if(!(responseCode == OK || responseCode == CREATED || responseCode == INCOMPLETE)) throw new UploadFileException(responseCode);
}
}
/**
* Exception thrown when there is a problem while uploading file
*/
public static class UploadFileException extends Exception
{
public UploadFileException()
{
super("Unable to upload file!");
}
public UploadFileException(int responsecode)
{
super("Unable to upload file! ResponseCode: "+responsecode);
}
public UploadFileException(String msg, int responsecode)
{
super(msg+responsecode);
}
}
}