Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle statuscode and delete file when loading failed #56

Merged
merged 1 commit into from Dec 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 30 additions & 10 deletions wechatgame/libs/wx-downloader.js
Expand Up @@ -191,8 +191,28 @@ function readText (item, callback) {
filePath: url,
encoding: 'utf8',
success: function (res) {
item.states[cc.loader.downloader.id] = cc.Pipeline.ItemState.COMPLETE;
callback(null, res.data);
var queue = cc.LoadingItems.getQueue(item);
queue.addListener(item.id, function (item) {
if (item.error) {
fs.unlink({
filePath: url,
success: function () {
cc.log('Load failed, removed local file ' + url + ' successfully!');
}
});
}
});

if (res.data) {
item.states[cc.loader.downloader.id] = cc.Pipeline.ItemState.COMPLETE;
callback(null, res.data);
}
else {
callback({
status: 0,
errorMessage: "Empty file: " + url
});
}
},
fail: function (res) {
cc.warn('Read file failed: ' + url);
Expand Down Expand Up @@ -277,14 +297,7 @@ function downloadRemoteFile (item, callback) {
wx.downloadFile({
url: remoteUrl,
success: function (res) {
if (res.statusCode === 404) {
cc.warn("Download file failed: " + remoteUrl);
callback({
status: 0,
errorMessage: res && res.errMsg ? res.errMsg : "Download file failed: " + remoteUrl
});
}
else if (res.tempFilePath) {
if (res.statusCode === 200 && res.tempFilePath) {
// http reading is not cached
var temp = res.tempFilePath;
var localPath = wx.env.USER_DATA_PATH + '/' + relatUrl;
Expand Down Expand Up @@ -319,6 +332,13 @@ function downloadRemoteFile (item, callback) {
});
});
}
else {
cc.warn("Download file failed: " + remoteUrl);
callback({
status: 0,
errorMessage: res && res.errMsg ? res.errMsg : "Download file failed: " + remoteUrl
});
}
},
fail: function (res) {
// Continue to try download with downloader, most probably will also fail
Expand Down