I am using the react-native-fetch-blob library. However, when a 404 message is sent it creates an empty file and calls it blag6.zip. This is my code:
try {
response = await RNFetchBlob
.config({
path: RNFetchBlob.fs.dirs.DocumentDir + "/temp/blag6.zip"
})
.fetch(
'POST',
'http://example.com/data/getTestSet.php',
{
'Accept': 'application/json',
'Content-Type': 'application/json'
},
JSON.stringify({
'passcode': "pass123",
'group_id': "1",
'file': "ShgRG/suvA/blag.zip"
})
)
temp_file = RNFS.DocumentDirectoryPath + "/temp/blag6.zip"
output_path = RNFS.DocumentDirectoryPath + "/output
await ZipArchive.unzip(temp_file, output_path)
console.log("This line is never reached.")
}
catch (err){
console.log(err)
}
I get the following error:
Error: Failed to extract file File too short to be a zip file: 0
Is there a way to prevent RNFetchBlob from saving empty files and throw an error instead? How can I tell what the error is?
Update:
For now I am using a temporary fix with the following:
status = response.info().status
//check if file exists here, throw error otherwise
file_name = RNFetchBlob.fs.dirs.DocumentDir + "/temp/blag6.zip"
file_stat = await RNFS.stat(file_name);
file_size = file_stat.size;
if (status != 200 || file_size == 0){
await RNFS.unlink(file_name);
throw "My error message";
}
Is there a reason that RNFetchBlob still creates a file if none was available? Is there any way to prevent it from creating an empty file if there is no file to save?