Skip to content

Commit

Permalink
FIX file load (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: lucca-dev <>
  • Loading branch information
shajz authored May 7, 2021
1 parent 3cf6564 commit 1c6a6b1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
23 changes: 17 additions & 6 deletions src/android/cc/fovea/openwith/ByteStreams.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.util.Base64;
import android.util.Base64OutputStream;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -18,16 +19,26 @@ private ByteStreams() {
}

public static String toBase64(final InputStream in) throws IOException, OutOfMemoryError {
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream(Math.max(32, in.available()));
byte[] buffer = new byte[3 * 1024];
int len = 0;

final BufferedInputStream bufferedInputStream = new BufferedInputStream(in);
ByteArrayOutputStream output = new ByteArrayOutputStream(Math.max(32, bufferedInputStream.available()));
Base64OutputStream output64 = new Base64OutputStream(output, Base64.NO_WRAP);
while ((bytesRead = in.read(buffer)) != -1) {
output64.write(buffer, 0, bytesRead);

while ((len = bufferedInputStream.read(buffer)) >= 0) {
output64.write(buffer, 0, len);
}

output64.flush();
output64.close();
return output.toString();

String result = new String(output.toByteArray(), "UTF-8");

output.close();
bufferedInputStream.close();

return result;
}
}
// vim: ts=4:sw=4:et
10 changes: 10 additions & 0 deletions src/android/cc/fovea/openwith/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.text.TextUtils;

import java.io.IOException;
Expand Down Expand Up @@ -207,6 +208,15 @@ public static String getDataFromURI(
final ContentResolver contentResolver,
final Uri uri) {
try {
Cursor c = contentResolver.query(uri, null, null, null, null);
int sizeIndex = c.getColumnIndex(OpenableColumns.SIZE);
c.moveToFirst();
long fileSize = c.getLong(sizeIndex);

if (fileSize > 20971520) {
return "";
}

final InputStream inputStream = contentResolver.openInputStream(uri);
return ByteStreams.toBase64(inputStream);
} catch (IOException e) {
Expand Down
4 changes: 1 addition & 3 deletions src/ios/OpenWithPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ - (void) setVerbosity:(CDVInvokedUrlCommand*)command {
andClass:[NSNumber class]];
self.verbosityLevel = value.integerValue;
[self.userDefaults setInteger:self.verbosityLevel forKey:@"verbosityLevel"];
[self.userDefaults synchronize];
[self debug:[NSString stringWithFormat:@"[setVerbosity] %d", self.verbosityLevel]];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
Expand Down Expand Up @@ -257,11 +256,10 @@ - (void) checkForFileToShare {
}

NSMutableArray* items = [[NSMutableArray alloc] init];
[self.userDefaults synchronize];
NSArray *object = [self.userDefaults objectForKey:@"items"];
if (object == nil) {
[self debug:@"[checkForFileToShare] Nothing to share"];
return [self sendResults:items];
return;
}

for (NSDictionary* item in object) {
Expand Down
3 changes: 1 addition & 2 deletions src/ios/ShareExtension/ShareViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ - (void) sendItemForIdentifier:(NSString*) utiToLoad itemProvider:(NSItemProvide
else {
NSData *tempData = [NSData dataWithContentsOfURL:(NSURL*)item];
if (tempData.length <= 10485760) {
// Max size is 100MB
// Max size is 10MB
data = tempData;
}
fileName = [(NSURL*)item lastPathComponent];
Expand Down Expand Up @@ -217,7 +217,6 @@ - (void) didSelectPost {

- (void) sendItems:(NSArray*) items {
[self.userDefaults setObject:items forKey:@"items"];
[self.userDefaults synchronize];

// Emit a URL that opens the cordova app
NSString *url = [NSString stringWithFormat:@"%@://items", SHAREEXT_URL_SCHEME];
Expand Down
2 changes: 1 addition & 1 deletion www/openwith.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function initOpenwithPlugin(root) {
errorCallback(err, dataDescriptor);
}
};
if (dataDescriptor.base64) {
if (dataDescriptor.base64 !== null && typeof dataDescriptor.base64 !== 'undefined') {
loadSuccess(dataDescriptor.base64);
} else {
cordova.exec(loadSuccess, loadError, PLUGIN_NAME, "load", [
Expand Down

0 comments on commit 1c6a6b1

Please sign in to comment.