Skip to content

Commit d48bbdd

Browse files
author
Philipp Hecht
committed
2 parents b4a285b + eb0a265 commit d48bbdd

File tree

4 files changed

+131
-16
lines changed

4 files changed

+131
-16
lines changed

README.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ If your project uses CocoaPods to manage React installation (especially with Exp
6262
1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
6363
2. Go to `node_modules``react-native-doc-viewer` and add `RNDocViewer.xcodeproj`
6464
3. In XCode, in the project navigator, select your project. Add `libRNDocViewer.a` to your project's `Build Phases``Link Binary With Libraries`
65-
4. Linked Frameworks and Libraries must have this 2 Libraries (AssetsLibrary.framework & QuickLock.framework). When not you have to add them.
65+
4. Linked Frameworks and Libraries must have this 2 Libraries (AssetsLibrary.framework & QuickLook.framework). When not you have to add them.
6666

6767
![Alt text](https://raw.githubusercontent.com/philipphecht/react-native-doc-viewer/master/Screenshots/screenshot_xcode_addlibrary.png "Xcode add Library")
6868

@@ -426,11 +426,7 @@ export default class DocumentViewerExample extends Component {
426426
## Donation
427427
If this project help you reduce time to develop, you can give me a cup of coffee :)
428428
429-
[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=A8YE92K9QM7NA)
430-
431-
Bitcoin wallet: 122dhCT98R6jrP5ahCKMRA1UupawtU9cVP
432-
433-
Etherum wallet: 0x68b93b03eb61a27b125416a5963f1e17c3ebad21
429+
Etherum wallet: 0x124F99647a904240945d8B582eEf1E3CD6D00a8a
434430
435431
436432

android/build.gradle

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ buildscript {
1212
apply plugin: 'com.android.library'
1313

1414
android {
15-
compileSdkVersion 23
16-
buildToolsVersion "23.0.1"
15+
compileSdkVersion 25
16+
buildToolsVersion "25.0.0"
1717

1818
defaultConfig {
1919
minSdkVersion 16
20-
targetSdkVersion 22
20+
targetSdkVersion 26
2121
versionCode 1
2222
versionName "1.0"
2323
}
@@ -33,4 +33,4 @@ repositories {
3333
dependencies {
3434
compile 'com.facebook.react:react-native:+'
3535
}
36-
36+

android/src/main/java/com/philipphecht/RNDocViewerModule.java

+123-4
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,116 @@ private File downloadFile(String url, String fileName, Boolean cache, String fil
261261

262262

263263

264+
} catch (FileNotFoundException e) {
265+
e.printStackTrace();
266+
callback.invoke(ERROR_FILE_NOT_FOUND);
267+
return null;
268+
} catch (IOException e) {
269+
e.printStackTrace();
270+
callback.invoke(ERROR_UNKNOWN_ERROR);
271+
return null;
272+
}
273+
}
274+
private File copyFile(InputStream in, String fileName, Boolean cache, String fileType, byte[] bytesData, Callback callback) {
275+
276+
try {
277+
Context context = getReactApplicationContext().getBaseContext();
278+
File outputDir = context.getCacheDir();
279+
if (bytesData.length > 0) {
280+
// use cache
281+
File f = cache != null && cache ? new File(outputDir, fileName) : File.createTempFile(FILE_TYPE_PREFIX, "." + fileType,
282+
outputDir);
283+
System.out.println("Bytes will be creating a file");
284+
final FileOutputStream fileOutputStream;
285+
try {
286+
fileOutputStream = new FileOutputStream(f);
287+
} catch (FileNotFoundException e) {
288+
e.printStackTrace();
289+
return null;
290+
}
291+
292+
final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
293+
fileOutputStream);
294+
try {
295+
bufferedOutputStream.write(bytesData);
296+
} catch (IOException e) {
297+
e.printStackTrace();
298+
return null;
299+
} finally {
300+
try {
301+
bufferedOutputStream.close();
302+
} catch (IOException e) {
303+
e.printStackTrace();
304+
}
305+
}
306+
return f;
307+
} else {
308+
String extension = MimeTypeMap.getFileExtensionFromUrl(fileName);
309+
System.out.println("Extensions DownloadFile " + extension);
310+
if (extension.equals("") && fileType.equals("")) {
311+
extension = "pdf";
312+
System.out.println("extension (default): " + extension);
313+
}
314+
315+
if (fileType != "" && extension.equals("")) {
316+
extension = fileType;
317+
System.out.println("extension (default): " + extension);
318+
}
319+
320+
// check has extension
321+
if (fileName.indexOf("\\.") == -1) {
322+
fileName = fileName + '.' + extension;
323+
}
324+
// if use cache, check exist
325+
if (cache != null && cache) {
326+
File existFile = new File(outputDir, fileName);
327+
if (existFile.exists()) {
328+
return existFile;
329+
}
330+
}
331+
332+
333+
File f;
334+
try {
335+
// use cache
336+
f = cache != null && cache ? new File(outputDir, fileName)
337+
: File.createTempFile(FILE_TYPE_PREFIX, "." + extension, outputDir);
338+
339+
// make sure the receiving app can read this file
340+
f.setReadable(true, false);
341+
System.out.println(f.getPath());
342+
343+
try {
344+
FileOutputStream outStream = new FileOutputStream(f);
345+
try {
346+
// Transfer bytes from in to out
347+
byte[] buf = new byte[1024];
348+
int len;
349+
while ((len = in.read(buf)) > 0) {
350+
outStream.write(buf, 0, len);
351+
}
352+
} finally {
353+
outStream.close();
354+
}
355+
} finally {
356+
in.close();
357+
}
358+
359+
if (f.exists()) {
360+
System.out.println("File exists");
361+
} else {
362+
System.out.println("File doesn't exist");
363+
}
364+
365+
return f;
366+
} catch (Exception err) {
367+
err.printStackTrace();
368+
}
369+
370+
return null;
371+
}
372+
373+
264374
} catch (FileNotFoundException e) {
265375
e.printStackTrace();
266376
callback.invoke(ERROR_FILE_NOT_FOUND);
@@ -281,6 +391,7 @@ private File downloadFile(String url, String fileName, Boolean cache, String fil
281391
private static String getMimeType(String url) {
282392
String mimeType = null;
283393
System.out.println("Url: " + url);
394+
url = url.replaceAll(" ", "");
284395
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
285396
if (extension != null) {
286397
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
@@ -315,12 +426,20 @@ public FileDownloaderAsyncTask(Callback callback,
315426

316427
@Override
317428
protected File doInBackground(Void... arg0) {
318-
if (!url.startsWith("file://")) {
319-
System.out.println("Url to download" +url);
429+
if (url.startsWith("content://")) {
430+
File file = null;
431+
try {
432+
InputStream in = getCurrentActivity().getContentResolver().openInputStream(Uri.parse(url));
433+
file = copyFile(in, fileName, cache, fileType, bytesData, callback);
434+
} catch (FileNotFoundException e) {
435+
System.out.println(e);
436+
}
437+
return file;
438+
} else if (!url.startsWith("file://")) {
439+
System.out.println("Url to download" + url);
320440
return downloadFile(url, fileName, cache, fileType, bytesData, callback);
321441
} else {
322-
File file = new File(url.replace("file://", ""));
323-
return file;
442+
return new File(url.replace("file://", ""));
324443
}
325444
}
326445

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"peerDependencies": {
2424
"react": "^16.3.0-alpha.1",
2525
"react-native": "0.54.3",
26-
"react-native-fs":"^2.3.2",
27-
"react-native-doc-viewer": "2.7.8"
26+
"react-native-fs":"^2.3.2",
27+
"react-native-doc-viewer": "2.7.8"
2828
},
2929
"repository": {
3030
"type": "git",

0 commit comments

Comments
 (0)