mirror of
https://github.com/apache/cordova-plugin-file-transfer.git
synced 2024-10-06 07:42:20 +08:00
CB-7006 Empty file is created on file transfer if server response is 304
Adds a corresponding test. Fixes the Windows proxy to not to delete the existing file in case of a download error (including 304) - a temporary file is deleted instead.
This commit is contained in:
parent
416be7633b
commit
73223a7d45
@ -286,6 +286,9 @@ exec(win, fail, 'FileTransfer', 'upload',
|
||||
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR));
|
||||
return;
|
||||
}
|
||||
// Download to a temp file to avoid the file deletion on 304
|
||||
// CB-7006 Empty file is created on file transfer if server response is 304
|
||||
var tempFileName = '~' + fileName;
|
||||
|
||||
var download = null;
|
||||
|
||||
@ -293,7 +296,7 @@ exec(win, fail, 'FileTransfer', 'upload',
|
||||
fileTransferOps[downloadId] = new FileTransferOperation(FileTransferOperation.PENDING, null);
|
||||
|
||||
var downloadCallback = function(storageFolder) {
|
||||
storageFolder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting).then(function(storageFile) {
|
||||
storageFolder.createFileAsync(tempFileName, Windows.Storage.CreationCollisionOption.replaceExisting).then(function (storageFile) {
|
||||
|
||||
// check if download isn't already cancelled
|
||||
var downloadOp = fileTransferOps[downloadId];
|
||||
@ -335,13 +338,15 @@ exec(win, fail, 'FileTransfer', 'upload',
|
||||
currentDownloadOp.promise = null;
|
||||
}
|
||||
|
||||
var nativeURI = storageFile.path.replace(appData.localFolder.path, 'ms-appdata:///local')
|
||||
storageFile.renameAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting).done(function () {
|
||||
var nativeURI = storageFile.path.replace(appData.localFolder.path, 'ms-appdata:///local')
|
||||
.replace(appData.temporaryFolder.path, 'ms-appdata:///temp')
|
||||
.replace(/\\/g, '/');
|
||||
|
||||
// Passing null as error callback here because downloaded file should exist in any case
|
||||
// otherwise the error callback will be hit during file creation in another place
|
||||
FileProxy.resolveLocalFileSystemURI(successCallback, null, [nativeURI]);
|
||||
// Passing null as error callback here because downloaded file should exist in any case
|
||||
// otherwise the error callback will be hit during file creation in another place
|
||||
FileProxy.resolveLocalFileSystemURI(successCallback, null, [nativeURI]);
|
||||
});
|
||||
}, function(error) {
|
||||
|
||||
var getTransferError = new WinJS.Promise(function (resolve) {
|
||||
|
@ -676,6 +676,60 @@ exports.defineAutoTests = function () {
|
||||
}
|
||||
});
|
||||
}, DOWNLOAD_TIMEOUT);
|
||||
|
||||
it('filetransfer.spec.35 304 should not result in the deletion of a cached file', function (done) {
|
||||
|
||||
if(isWP8) {
|
||||
pending();
|
||||
return;
|
||||
}
|
||||
|
||||
var imageURL = "http://apache.org/images/feather-small.gif";
|
||||
var lastModified = new Date();
|
||||
|
||||
var downloadFail = function (error) {
|
||||
expect(error.http_status).toBe(304);
|
||||
expect(error.code).toBe(FileTransferError.NOT_MODIFIED_ERR);
|
||||
|
||||
persistentRoot.getFile(fileName, { create: false },
|
||||
function (entry) {
|
||||
var fileWin = function (file) {
|
||||
var reader = new FileReader();
|
||||
|
||||
reader.onerror = unexpectedCallbacks.fileOperationFail;
|
||||
reader.onloadend = function () {
|
||||
|
||||
expect(reader.result).toBeTruthy();
|
||||
if(reader.result != null) {
|
||||
expect(reader.result.length).toBeGreaterThan(0);
|
||||
}
|
||||
|
||||
done();
|
||||
};
|
||||
|
||||
reader.readAsBinaryString(file);
|
||||
};
|
||||
|
||||
entry.file(fileWin, unexpectedCallbacks.fileSystemFail);
|
||||
},
|
||||
function (err) {
|
||||
expect('Could not open test file \'' + fileName + '\': ' + JSON.stringify(err)).not.toBeDefined();
|
||||
done();
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
// Adding parameters to the requests to avoid caching on iOS, which leads to 200
|
||||
// instead of 304 in result of the second request. (a similar issue is described in CB-8606, CB-10088)
|
||||
transfer.download(imageURL + "?q=" + lastModified.getTime(), localFilePath, function () {
|
||||
transfer.download(imageURL + "?q=" + (lastModified.getTime() + 1), localFilePath, unexpectedCallbacks.httpWin, downloadFail, null,
|
||||
{
|
||||
headers: {
|
||||
'If-Modified-Since': lastModified.toUTCString()
|
||||
}
|
||||
});
|
||||
}, unexpectedCallbacks.httpFail);
|
||||
}, DOWNLOAD_TIMEOUT);
|
||||
});
|
||||
|
||||
describe('upload', function() {
|
||||
|
Loading…
Reference in New Issue
Block a user