Skip to content

Commit e3c0b89

Browse files
committed
fix(android): always return asset from S3 in order to ensure correct metadata
1 parent a02d0c1 commit e3c0b89

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

packages/android/app/src/main/java/io/literal/model/StorageObject.java

+8-24
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public enum Status {
5555
private static final Pattern ARCHIVES_PATH_PATTERN = Pattern.compile(".+/archives/(.+)");
5656
private static final Pattern SHARED_PUBLIC_READ_PATTERN = Pattern.compile("shared-public-read/onboarding/(.+)");
5757
private static final Pattern SCREENSHOT_PATH_PATTERN = Pattern.compile(".+/screenshots/(.+)");
58+
5859
private Type type;
5960
private String id;
6061
private Status status;
@@ -295,31 +296,14 @@ public TransferObserver upload(Context context, User user, Callback<Exception, A
295296
);
296297
}
297298

298-
public InputStream downloadAsInputStream(Context context, User user) throws FileNotFoundException {
299-
if (status.equals(Status.SYNCHRONIZED) || status.equals(Status.UPLOAD_REQUIRED)) {
300-
return new FileInputStream(getFile(context));
301-
} else if (status.equals(Status.DOWNLOAD_REQUIRED)) {
302-
S3Object s3Object = StorageRepository.getS3Object(context, getAmazonS3URI(context, user));
303-
File file = getFile(context);
304-
S3ObjectInputStream inputStream = null;
305-
try {
306-
inputStream = s3Object.getObjectContent();
307-
Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
308-
} catch (IOException e) {
309-
ErrorRepository.captureException(e);
310-
} finally {
311-
if (inputStream != null) {
312-
try {
313-
inputStream.close();
314-
} catch (IOException e1) {
315-
ErrorRepository.captureException(e1);
316-
}
317-
}
318-
}
299+
public ObjectMetadata download(Context context, User user) {
300+
ObjectMetadata objectMetadata = null;
301+
try {
302+
objectMetadata = StorageRepository.getObject(context, getAmazonS3URI(context, user), getFile(context));
319303
status = Status.SYNCHRONIZED;
320-
return new FileInputStream(file);
321-
} else {
322-
throw new FileNotFoundException("Unable to resolve StorageObject.");
304+
} catch (Exception e) {
305+
ErrorRepository.captureException(e);
323306
}
307+
return objectMetadata;
324308
}
325309
}

packages/android/app/src/main/java/io/literal/repository/StorageRepository.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.amazonaws.mobileconnectors.s3.transferutility.TransferState;
1010
import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility;
1111
import com.amazonaws.services.s3.AmazonS3URI;
12+
import com.amazonaws.services.s3.model.GetObjectRequest;
1213
import com.amazonaws.services.s3.model.ObjectMetadata;
1314
import com.amazonaws.services.s3.model.S3Object;
1415

@@ -64,8 +65,8 @@ public static boolean isStorageUrl(Context context, URL url) {
6465
return url.getHost().equals(storageHost);
6566
}
6667

67-
public static S3Object getS3Object(Context context, AmazonS3URI uri) {
68-
return AmazonS3ClientFactory.getInstance(context).getObject(uri.getBucket(), uri.getKey());
68+
public static ObjectMetadata getObject(Context context, AmazonS3URI uri, File destinationFile) {
69+
return AmazonS3ClientFactory.getInstance(context).getObject(new GetObjectRequest(uri.getBucket(), uri.getKey()), destinationFile);
6970
}
7071

7172
public static TransferObserver upload(

packages/android/app/src/main/java/io/literal/ui/view/SourceWebViewClient.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
import androidx.annotation.Nullable;
1212

13+
import com.amazonaws.services.s3.model.ObjectMetadata;
14+
1315
import org.json.JSONArray;
1416
import org.json.JSONException;
1517

18+
import java.io.FileInputStream;
1619
import java.net.MalformedURLException;
1720
import java.net.URL;
1821
import java.util.ArrayList;
@@ -59,13 +62,17 @@ public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceReque
5962
.flatMap((storageObject) -> {
6063
storageObject.ensureDownloadRequired(context);
6164
try {
65+
ObjectMetadata metadata = storageObject.download(context, authenticationViewModel.getUser().getValue());
6266
WebResourceResponse response = new WebResourceResponse(
63-
storageObject.getContentType(context).toMimeType(),
67+
metadata.getContentType(),
6468
null,
65-
storageObject.downloadAsInputStream(context, authenticationViewModel.getUser().getValue())
69+
new FileInputStream(storageObject.getFile(context))
6670
);
6771
Map<String, String> responseHeaders = new HashMap<>();
68-
responseHeaders.put("Cache-Control", StorageObject.CACHE_CONTROL);
72+
responseHeaders.put("Cache-Control", metadata.getCacheControl());
73+
responseHeaders.put("ETag", metadata.getETag());
74+
responseHeaders.put("Content-Language", metadata.getContentLanguage());
75+
responseHeaders.put("Last-Modified", metadata.getLastModified().toString());
6976
response.setResponseHeaders(responseHeaders);
7077
return Optional.of(response);
7178
} catch (Exception e) {

0 commit comments

Comments
 (0)