1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.opencastproject.transcription.googlespeech;
22
23 import org.apache.http.HttpHeaders;
24 import org.apache.http.HttpStatus;
25 import org.apache.http.client.methods.CloseableHttpResponse;
26 import org.apache.http.client.methods.HttpDelete;
27 import org.apache.http.client.methods.HttpPost;
28 import org.apache.http.client.methods.HttpPut;
29 import org.apache.http.entity.FileEntity;
30 import org.apache.http.impl.client.CloseableHttpClient;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import java.io.File;
35 import java.io.IOException;
36
37 public class GoogleSpeechTranscriptionServiceStorage {
38
39
40
41
42 private static final Logger logger = LoggerFactory.getLogger(GoogleSpeechTranscriptionService.class);
43
44 private static final String GOOGLE_STORAGE_LINK = "https://www.googleapis.com/upload/storage/v1/b/";
45 private static final String GOOGLE_STORAGE_MEDIA = "https://www.googleapis.com/storage/v1/b/";
46 private static final int DEFAULT_CODE = 0;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public int startUpload(CloseableHttpClient httpClient, String bucket, String mpId,
64 String fileExtension, File file, String byteSize, String contentType, String accessToken) throws IOException {
65 CloseableHttpResponse postResponse = null;
66 CloseableHttpResponse putResponse = null;
67 String location = null;
68
69 try {
70 HttpPost httpPost = new HttpPost(GOOGLE_STORAGE_LINK + String.format(
71 "%s/o?uploadType=resumable&name=%s.%s", bucket, mpId, fileExtension));
72 logger.debug("Url to store media file on Google Cloud Storage : {}", httpPost.getURI().toString());
73 httpPost.addHeader("Authorization", "Bearer " + accessToken);
74 httpPost.addHeader("X-Upload-Content-Type", contentType);
75 httpPost.addHeader("X-Upload-Content-Length", byteSize);
76 httpPost.addHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8");
77 postResponse = httpClient.execute(httpPost);
78 int postCode = postResponse.getStatusLine().getStatusCode();
79 if (postCode == HttpStatus.SC_OK) {
80 logger.info("Upload session has been successfully created");
81
82 try {
83 location = postResponse.getLastHeader("Location").getValue();
84 } catch (Exception ex) {
85 logger.warn("Exception when uploading file to Google Storage", ex);
86 return DEFAULT_CODE;
87 }
88 try {
89 HttpPut httpPut = new HttpPut(location);
90 httpPut.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
91 FileEntity entity = new FileEntity(file);
92 httpPut.setEntity(entity);
93 putResponse = httpClient.execute(httpPut);
94 int putCode = putResponse.getStatusLine().getStatusCode();
95
96 switch (putCode) {
97 case HttpStatus.SC_OK:
98 case HttpStatus.SC_CREATED:
99 logger.info("File {} uploaded to Google Storage", mpId + "." + fileExtension);
100 return putCode;
101 default:
102 logger.warn("Unable to upload file to Google Storage, returned code: {}.", putCode);
103 return putCode;
104 }
105 } catch (Exception e) {
106 logger.warn("Exception when uploading file to Google Storage", e);
107 }
108 } else {
109 logger.warn("Uploading file to Google Storage failed and returned, status: {}.", postCode);
110 return postCode;
111 }
112 } catch (Exception e) {
113 logger.warn("Exception when uploading file to Google Storage", e);
114 } finally {
115 try {
116 httpClient.close();
117 if (postResponse != null) {
118 postResponse.close();
119 }
120 if (putResponse != null) {
121 putResponse.close();
122 }
123 } catch (IOException e) {
124 }
125 }
126 return DEFAULT_CODE;
127 }
128
129
130
131
132
133
134
135
136
137
138
139 public void deleteGoogleStorageFile(
140 CloseableHttpClient httpClient,
141 String bucket,
142 String objectName,
143 String accessToken
144 ) throws IOException {
145 CloseableHttpResponse response = null;
146 try {
147 HttpDelete httpdelete = new HttpDelete(GOOGLE_STORAGE_MEDIA + String.format("%s/o/%s", bucket, objectName));
148 logger.debug("Url to delete media file from Google Cloud Storage : {}", httpdelete.getURI().toString());
149 httpdelete.addHeader("Authorization", "Bearer " + accessToken);
150 response = httpClient.execute(httpdelete);
151 int code = response.getStatusLine().getStatusCode();
152
153 switch (code) {
154 case HttpStatus.SC_NO_CONTENT:
155 logger.info("Media file: {} deleted from Google storage", objectName);
156 break;
157 default:
158 logger.warn("Unable to delete meida file: {} from Google Storage", objectName);
159 break;
160 }
161 } catch (Exception e) {
162 logger.warn("Unable to delete meida file: {} from Google Storage", objectName, e);
163 } finally {
164 try {
165 httpClient.close();
166 if (response != null) {
167 response.close();
168 }
169 } catch (IOException e) {
170 }
171 }
172 }
173
174 }