Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import React from 'react'
import fs from 'react-native-fs'
import ImageViewer from 'react-native-image-zoom-viewer'
import {
Animated,
Easing,
CameraRoll
} from 'react-native'
declare var global
const psnineFolder = fs.DocumentDirectoryPath + '/psnine'
fs.stat(psnineFolder).then(data => {
const isDirectory = data.isDirectory()
if (!isDirectory) {
fs.unlink(psnineFolder).catch(() => {}).then(() => fs.mkdir(psnineFolder))
}
}).catch(() => {
fs.mkdir(psnineFolder).catch(err => console.log(err, 'ImageViewer:line#27'))
})
const onSave = (image) => {
// console.log(image, psnineFolder + '/' + image.split('/').pop())
const result = CameraRoll.saveToCameraRoll(image)
return result.then((url) => {
global.toast('保存成功')
return url
}).catch(err => global.toast('保存失败: ' + err.toString()))
}
checkImageCache = (imageUri, cachePath, cacheKey) => {
const dirPath = DocumentDirectoryPath+'/'+cachePath;
const filePath = dirPath+'/'+cacheKey;
RNFS
.stat(filePath)
.then((res) => {
if (res.isFile() && res.size > 0) {
// It's possible the component has already unmounted before setState could be called.
// It happens when the defaultSource and source have both been cached.
// An attempt is made to display the default however it's instantly removed since source is available
// means file exists, ie, cache-hit
this.setState({cacheable: true, cachedImagePath: filePath});
}
else {
throw Error("CacheableImage: Invalid file in checkImageCache()");
}
})
.catch((err) => {
const downloadResult = await RNFS.downloadFile({
begin: (download: RNFS.DownloadBeginCallbackResult): void => onStart(update.name, download),
connectionTimeout: CONFIG_CONNECTION_TIMEOUT,
fromUrl: (os !== 'android' && update.zsize && update.zurl) ? update.zurl : update.url,
headers: { 'Accept-Encoding': 'gzip,deflate' },
progress: callbacks.onDownloadProgress,
readTimeout: CONFIG_READ_TIMEOUT,
toFile: TEMP_CONFIG_DIRECTORY + update.name,
}).promise;
if (downloadResult.statusCode !== HttpStatus.OK) {
throw new Error(`Download of file ${update.name} failed. Status code: ${downloadResult.statusCode}`);
}
// Get file stats
const fileStats = await RNFS.stat(TEMP_CONFIG_DIRECTORY + update.name);
downloadResult.bytesWritten = parseInt(fileStats.size);
if (callbacks.onDownloadComplete) {
callbacks.onDownloadComplete(update.name, downloadResult);
}
}
const configRowUpdates: ConfigFile[] = [];
// Delete the old configuration files, move the new ones
for (const update of configDetails.files) {
// Delete the file if it exists
const targetLocation = `${CONFIG_DIRECTORY}${CONFIG_SUBDIRECTORIES[update.type]}${update.name}`;
const tempLocation = `${TEMP_CONFIG_DIRECTORY}${update.name}`;
const exists = await RNFS.exists(targetLocation);
if (exists) {
const downloadResult: Object = await RNFS.downloadFile({
fromUrl: configurationUpdates[i].url,
toFile: TEMP_CONFIG_DIRECTORY + configurationUpdates[i].name,
progress: callbacks.onDownloadProgress,
begin: download => onStart(configurationUpdates[i].name, download),
});
/* eslint-enable no-loop-func */
if (downloadResult.statusCode != HttpStatus.OK) {
throw new Error('Download of file ' + configurationUpdates[i].name + ' failed.'
+ ' Status code: ' + downloadResult.statusCode);
}
// Get file stats
const fileStats = await RNFS.stat(TEMP_CONFIG_DIRECTORY + configurationUpdates[i].name);
downloadResult.bytesWritten = fileStats.size;
downloadResult.filename = configurationUpdates[i].name;
if (callbacks.onDownloadComplete) {
callbacks.onDownloadComplete(downloadResult);
}
// If APP_CONFIG is updated, reset the current semester
if (configurationUpdates[i].name === APP_CONFIG_NAME) {
await AsyncStorage.setItem('app_current_semester', '0');
}
}
const configRowUpdates: Array < ConfigFile > = [];
// Delete the old configuration files, move the new ones
for (let i = 0; i < configurationUpdates.length; i++) {
async(result) => {
console.log('downloadImage success:', result);
const info = await fs.stat(filepath);
await self.addCacheImage(filename, info.size);
await self.updateStorage(info.size);
await self.checkCacheStorage();
resolve(true);
},
(error) => {
async function isLogFileLimitExceeded (filePath) {
const stats = await RNFS.stat(filePath)
const size = stats.size
return size > MAX_BYTE_SIZE_PER_FILE
}
RNFS.exists(this.state.audio_path).then((exists) => {
if (exists) {
RNFS.stat(this.state.audio_path)
.then((file_info) => {
this.state.file_info = file_info;
});
}
});
}