Skip to content

Commit 2446d4e

Browse files
Merge branch 'vu/upgrading-rn-fetch-blob' into develop
2 parents 9110ef2 + 6a113a0 commit 2446d4e

File tree

10 files changed

+139
-100
lines changed

10 files changed

+139
-100
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
For developers who interested in making contribution to this project, please see [https://github.com/joltup/rn-fetch-blob-dev](https://github.com/joltup/rn-fetch-blob-dev) for more information.
1+
For developers who interested in making contribution to this project, please see [https://github.com/joltup/react-native-fetch-blob-dev](https://github.com/joltup/react-native-fetch-blob-dev) for more information.

README.md

Lines changed: 50 additions & 50 deletions
Large diffs are not rendered by default.

android/src/main/java/com/RNFetchBlob/Utils/PathResolver.java

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
import android.content.ContentUris;
1111
import android.os.Environment;
1212
import android.content.ContentResolver;
13+
14+
import androidx.annotation.Nullable;
15+
1316
import com.RNFetchBlob.RNFetchBlobUtils;
1417
import java.io.File;
18+
import java.io.IOException;
1519
import java.io.InputStream;
1620
import java.io.FileOutputStream;
1721

@@ -41,15 +45,15 @@ else if (isDownloadsDocument(uri)) {
4145
final String id = DocumentsContract.getDocumentId(uri);
4246
//Starting with Android O, this "id" is not necessarily a long (row number),
4347
//but might also be a "raw:/some/file/path" URL
44-
if (id != null && id.startsWith("raw:/")) {
45-
Uri rawuri = Uri.parse(id);
46-
String path = rawuri.getPath();
47-
return path;
48+
if (id != null) {
49+
if (id.startsWith("raw:/")) {
50+
Uri rawUri = Uri.parse(id);
51+
return rawUri.getPath();
52+
}
53+
final Uri contentUri = ContentUris.withAppendedId(
54+
Uri.parse("content://downloads/public_downloads"), Long.parseLong(id));
55+
return getDataColumn(context, contentUri, null, null);
4856
}
49-
final Uri contentUri = ContentUris.withAppendedId(
50-
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
51-
52-
return getDataColumn(context, contentUri, null, null);
5357
}
5458
catch (Exception ex) {
5559
//something went wrong, but android should still be able to handle the original uri by returning null here (see readFile(...))
@@ -85,30 +89,16 @@ else if ("content".equalsIgnoreCase(uri.getScheme())) {
8589
if (isGooglePhotosUri(uri))
8690
return uri.getLastPathSegment();
8791

88-
return getDataColumn(context, uri, null, null);
92+
String result = getDataColumn(context, uri, null, null);
93+
if (result != null) {
94+
return result;
95+
} else {
96+
return getPathByDownloadingFromUri(context, uri);
97+
}
8998
}
9099
// Other Providers
91100
else{
92-
try {
93-
InputStream attachment = context.getContentResolver().openInputStream(uri);
94-
if (attachment != null) {
95-
String filename = getContentName(context.getContentResolver(), uri);
96-
if (filename != null) {
97-
File file = new File(context.getCacheDir(), filename);
98-
FileOutputStream tmp = new FileOutputStream(file);
99-
byte[] buffer = new byte[1024];
100-
while (attachment.read(buffer) > 0) {
101-
tmp.write(buffer);
102-
}
103-
tmp.close();
104-
attachment.close();
105-
return file.getAbsolutePath();
106-
}
107-
}
108-
} catch (Exception e) {
109-
RNFetchBlobUtils.emitWarningEvent(e.toString());
110-
return null;
111-
}
101+
return getPathByDownloadingFromUri(context, uri);
112102
}
113103
}
114104
// MediaStore (and general)
@@ -128,14 +118,17 @@ else if ("file".equalsIgnoreCase(uri.getScheme())) {
128118
return null;
129119
}
130120

121+
@Nullable
131122
private static String getContentName(ContentResolver resolver, Uri uri) {
132123
Cursor cursor = resolver.query(uri, null, null, null, null);
133-
cursor.moveToFirst();
134-
int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
135-
if (nameIndex >= 0) {
136-
String name = cursor.getString(nameIndex);
137-
cursor.close();
138-
return name;
124+
if (cursor != null) {
125+
cursor.moveToFirst();
126+
int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
127+
if (nameIndex >= 0) {
128+
String name = cursor.getString(nameIndex);
129+
cursor.close();
130+
return name;
131+
}
139132
}
140133
return null;
141134
}
@@ -179,6 +172,52 @@ public static String getDataColumn(Context context, Uri uri, String selection,
179172
return result;
180173
}
181174

175+
@Nullable
176+
private static String getPathByDownloadingFromUri(Context context, Uri uri) {
177+
InputStream attachment = null;
178+
try {
179+
attachment = context.getContentResolver().openInputStream(uri);
180+
if (attachment != null) {
181+
String fileName = getContentName(context.getContentResolver(), uri);
182+
if (fileName != null) {
183+
File file = new File(context.getCacheDir(), fileName);
184+
FileOutputStream tmp = new FileOutputStream(file);
185+
try {
186+
byte[] buffer =new byte[1024];
187+
while (attachment.read(buffer) > 0) {
188+
tmp.write(buffer);
189+
}
190+
} catch (Exception e) {
191+
e.printStackTrace();
192+
RNFetchBlobUtils.emitWarningEvent(e.toString());
193+
} finally {
194+
try {
195+
tmp.close();
196+
} catch (IOException e) {
197+
e.printStackTrace();
198+
RNFetchBlobUtils.emitWarningEvent(e.toString());
199+
}
200+
}
201+
attachment.close();
202+
return file.getAbsolutePath();
203+
}
204+
}
205+
} catch (Exception e) {
206+
e.printStackTrace();
207+
RNFetchBlobUtils.emitWarningEvent(e.toString());
208+
return null;
209+
} finally {
210+
if (attachment != null) {
211+
try {
212+
attachment.close();
213+
} catch (IOException e) {
214+
e.printStackTrace();
215+
RNFetchBlobUtils.emitWarningEvent(e.toString());
216+
}
217+
}
218+
}
219+
return null;
220+
}
182221

183222
/**
184223
* @param uri The Uri to check.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<resources>
2-
<string name="app_name">rn-fetch-blob</string>
2+
<string name="app_name">react-native-fetch-blob</string>
33
</resources>

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ emitter.addListener("RNFetchBlobMessage", (e) => {
7171
// Show warning if native module not detected
7272
if(!RNFetchBlob || !RNFetchBlob.fetchBlobForm || !RNFetchBlob.fetchBlob) {
7373
console.warn(
74-
'rn-fetch-blob could not find valid native module.',
74+
'react-native-fetch-blob could not find valid native module.',
7575
'please make sure you have linked native modules using `rnpm link`,',
7676
'and restart RN packager or manually compile IOS/Android project.'
7777
)

ios/RNFetchBlob.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@
261261
"$(SRCROOT)/../../React/**",
262262
"$(SRCROOT)/../../react-native/React/**",
263263
"$(SRCROOT)/../../node_modules/react-native/React/**",
264-
"$(SRCROOT)/../../node_modules/rn-fetch-blob/ios/RNFetchBlob",
264+
"$(SRCROOT)/../../node_modules/react-native-fetch-blob/ios/RNFetchBlob",
265265
"$(SRCROOT)/../../RNFetchBlobTest/node_modules/react-native/**",
266266
);
267267
OTHER_LDFLAGS = "-ObjC";
@@ -282,7 +282,7 @@
282282
"$(SRCROOT)/../../React/**",
283283
"$(SRCROOT)/../../react-native/React/**",
284284
"$(SRCROOT)/../../node_modules/react-native/React/**",
285-
"$(SRCROOT)/../../node_modules/rn-fetch-blob/ios/RNFetchBlob",
285+
"$(SRCROOT)/../../node_modules/react-native-fetch-blob/ios/RNFetchBlob",
286286
"$(SRCROOT)/../../RNFetchBlobTest/node_modules/react-native/**",
287287
);
288288
OTHER_LDFLAGS = "-ObjC";

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "rn-fetch-blob",
2+
"name": "react-native-fetch-blob",
33
"version": "0.12.0",
44
"description": "A module provides upload, download, and files access API. Supports file stream read/write for process large files.",
55
"main": "index.js",
@@ -22,7 +22,7 @@
2222
"image header"
2323
],
2424
"repository": {
25-
"url": "https://github.com/joltup/rn-fetch-blob.git"
25+
"url": "https://github.com/joltup/react-native-fetch-blob.git"
2626
},
2727
"author": "Joltup",
2828
"license": "MIT",

rn-fetch-blob.podspec renamed to react-native-fetch-blob.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ Pod::Spec.new do |s|
88
s.requires_arc = true
99
s.license = 'MIT'
1010
s.homepage = 'n/a'
11-
s.source = { :git => "https://github.com/joltup/rn-fetch-blob" }
11+
s.source = { :git => "https://github.com/padlet/react-native-fetch-blob" }
1212
s.author = 'Joltup'
1313
s.source_files = 'ios/**/*.{h,m}'
1414
s.platform = :ios, "8.0"
15-
s.dependency 'React-Core'
15+
s.dependency 'React/Core'
1616
end

react-native.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
dependency: {
33
hooks: {
4-
prelink: 'node ./node_modules/rn-fetch-blob/scripts/prelink.js',
4+
prelink: 'node ./node_modules/react-native-fetch-blob/scripts/prelink.js',
55
},
66
},
77
};

scripts/prelink.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ try {
66
var PACKAGE_JSON = process.cwd() + '/package.json';
77
var package = JSON.parse(fs.readFileSync(PACKAGE_JSON));
88
var APP_NAME = package.name;
9-
var PACKAGE_GRADLE = process.cwd() + '/node_modules/rn-fetch-blob/android/build.gradle'
9+
var PACKAGE_GRADLE = process.cwd() + '/node_modules/react-native-fetch-blob/android/build.gradle'
1010
var VERSION = checkVersion();
1111

1212
console.log('RNFetchBlob detected app version => ' + VERSION);
1313

1414
if(VERSION < 0.28) {
15-
console.log('You project version is '+ VERSION + ' which may not compatible to rn-fetch-blob 7.0+, please consider upgrade your application template to react-native 0.27+.')
15+
console.log('You project version is '+ VERSION + ' which may not compatible to react-native-fetch-blob 7.0+, please consider upgrade your application template to react-native 0.27+.')
1616
// add OkHttp3 dependency fo pre 0.28 project
1717
var main = fs.readFileSync(PACKAGE_GRADLE);
1818
console.log('adding OkHttp3 dependency to pre 0.28 project .. ')
@@ -52,7 +52,7 @@ try {
5252
}
5353
else {
5454
console.log(
55-
'\033[95mrn-fetch-blob \033[97mwill not automatically add Android permissions after \033[92m0.9.4 '+
55+
'\033[95mreact-native-fetch-blob \033[97mwill not automatically add Android permissions after \033[92m0.9.4 '+
5656
'\033[97mplease run the following command if you want to add default permissions :\n\n' +
5757
'\033[96m\tRNFB_ANDROID_PERMISSIONS=true react-native link \n')
5858
}
@@ -64,8 +64,8 @@ try {
6464

6565
} catch(err) {
6666
console.log(
67-
'\033[95mrn-fetch-blob\033[97m link \033[91mFAILED \033[97m\nCould not automatically link package :'+
67+
'\033[95mreact-native-fetch-blob\033[97m link \033[91mFAILED \033[97m\nCould not automatically link package :'+
6868
err.stack +
6969
'please follow the instructions to manually link the library : ' +
70-
'\033[4mhttps://github.com/joltup/rn-fetch-blob/wiki/Manually-Link-Package\n')
70+
'\033[4mhttps://github.com/joltup/react-native-fetch-blob/wiki/Manually-Link-Package\n')
7171
}

0 commit comments

Comments
 (0)