Skip to content

Commit 2adbe44

Browse files
author
Russell.H.Sun
committed
fix tempFile download failed bug
1 parent 4d5c80b commit 2adbe44

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

android/src/main/java/org/hstar/reactnative/esayupgrade/RNEasyUpgradeModule.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ private Uri getFileUri(String filepath) throws IORejectionException {
9999
return uri;
100100
}
101101

102+
private String getRealPath(String filePath) {
103+
File externalStorageDirectory = Environment.getExternalStorageDirectory();
104+
if (externalStorageDirectory != null && !filePath.contains(externalStorageDirectory.getAbsolutePath())) {
105+
filePath = externalStorageDirectory.getAbsolutePath() + filePath;
106+
}
107+
108+
return filePath;
109+
}
102110

103111
@ReactMethod
104112
public void download(String url, ReadableMap headers, ReadableMap config, Callback onDone) {
@@ -213,6 +221,7 @@ public void copyFile(String filepath, String destPath, Promise promise) {
213221
@ReactMethod
214222
public void unlink(String filepath, Promise promise) {
215223
try {
224+
filepath = getRealPath(filepath);
216225
File file = new File(filepath);
217226

218227
if (!file.exists()) throw new Exception("File does not exist");

index.js

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NativeModules, NetInfo, Platform } from 'react-native';
2-
import { download, isFileExists } from './lib/downloadHelper';
2+
import { deleteFile, download, isFileExists } from './lib/downloadHelper';
33

44
const { RNEasyUpgrade } = NativeModules;
55

@@ -35,7 +35,7 @@ class AppUpgrade {
3535
}
3636

3737
get downloadDestPath() {
38-
return this.options.downloadDestDirectory + '/' + this.options.downloadApkName;
38+
return this.options.downloadDestDirectory + '/' + this.options.downloadApkName;
3939
}
4040

4141
get downloadDestDirectory() {
@@ -47,7 +47,7 @@ class AppUpgrade {
4747
}
4848

4949
_handleError(err) {
50-
console.log('downloadApkError', err);
50+
console.log('EasyUpgradeError', err);
5151
this.options.onError(err);
5252
}
5353

@@ -63,20 +63,39 @@ class AppUpgrade {
6363

6464
checkApkHasDownloaded = async (path = this.downloadDestPath) => {
6565
return await isFileExists(path);
66-
};
66+
}
67+
/**
68+
* download file
69+
* @param fileUrl
70+
* @param downloadConf
71+
*/
72+
downloadFile(fileUrl, downloadConf = {}) {
73+
jobId = 1;
74+
download(fileUrl, downloadConf)
75+
.then(async res => {
76+
jobId = -1;
77+
if (this.options.shouldCheckApkHasDownloaded) {
78+
await RNEasyUpgrade.moveFile(downloadConf.tempDownloadPath, this.downloadDestPath);
79+
}
80+
this.options.downloadApkEnd(this.downloadDestPath);
81+
})
82+
.catch(err => {
83+
jobId = -1;
84+
this._handleError(err);
85+
});
86+
}
6787

6888
/**
6989
* update app and install
7090
* @param apkUrl
7191
*/
72-
updateAndroidApp(apkUrl) {
92+
async updateAndroidApp(apkUrl) {
7393
if (this.downloading) {
7494
return;
7595
}
7696

7797
const tempDownloadApkName = 'temp_download.apk';
7898
const tempDownloadPath = this.downloadDestDirectory + '/' + tempDownloadApkName;
79-
8099
const downloadConf = {
81100
downloadTitle: this.options.downloadTitle,
82101
downloadDescription: this.options.downloadDescription,
@@ -85,26 +104,23 @@ class AppUpgrade {
85104
allowedInMetered: true,
86105
showInDownloads: true,
87106
external: true, //when false basically means use the default Download path
88-
path: this.downloadDestDirectory //if "external" is true then use this path
107+
path: this.downloadDestDirectory, //if "external" is true then use this path
108+
tempDownloadPath: tempDownloadPath
89109
};
90-
jobId = 1;
91-
download(apkUrl, downloadConf)
92-
.then(async res => {
93-
jobId = -1;
94-
if (this.options.shouldCheckApkHasDownloaded) {
95-
await RNEasyUpgrade.moveFile(tempDownloadPath, this.downloadDestPath);
110+
if (this.options.shouldCheckApkHasDownloaded) {
111+
try {
112+
const isTempFileExisted = await isFileExists(tempDownloadPath);
113+
if (isTempFileExisted) {
114+
await deleteFile(tempDownloadPath);
96115
}
97-
this.options.downloadApkEnd(this.downloadDestPath);
98-
})
99-
.catch(err => {
100-
console.log(err);
101-
jobId = -1;
116+
} catch (err) {
102117
this._handleError(err);
103-
});
118+
}
119+
}
120+
this.downloadFile(apkUrl, downloadConf);
104121
}
105122

106123
installApk(apkPath = this.downloadDestPath) {
107-
console.log(apkPath);
108124
RNEasyUpgrade.installApk(apkPath);
109125
}
110126

lib/downloadHelper.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,16 @@ const isFileExists = path => {
7777
return RNEasyUpgrade.isFileExists(normalizeFilePath(path));
7878
};
7979

80+
const deleteFile = path => {
81+
return RNEasyUpgrade.unlink(normalizeFilePath(path));
82+
};
8083

8184
export {
8285
download,
8386
queueDownload,
8487
attachOnCompleteListener,
8588
cancel,
8689
checkStatus,
87-
isFileExists
90+
isFileExists,
91+
deleteFile
8892
}

0 commit comments

Comments
 (0)