Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.downloadTask = RNFetchBlob.config(options).fetch('GET', imageUrl);
this.downloadTask.progress((received, total) => {
const progress = (received / total) * 100;
if (this.mounted) {
this.setState({
progress,
started: true,
});
}
});
const res = await this.downloadTask;
let path = res.path();
if (saveToCameraRoll) {
path = await CameraRoll.saveToCameraRoll(path, 'photo'); /* eslint-disable-line require-atomic-updates */
}
if (this.mounted) {
this.setState({
progress: 100,
}, async () => {
if (this.state.didCancel) {
try {
await RNFetchBlob.fs.unlink(path);
} finally {
this.downloadDidCancel();
}
} else {
this.props.onDownloadSuccess();
}
});
export async function saveAttachmentToCameraRoll(filePath: string, mimeType: string): Promise {
const fileURL = 'file://' + filePath
const saveType = mimeType.startsWith('video') ? 'video' : 'photo'
const logPrefix = '[saveAttachmentToCameraRoll] '
try {
await requestPermissionsToWrite()
logger.info(logPrefix + `Attempting to save as ${saveType}`)
await CameraRoll.saveToCameraRoll(fileURL, saveType)
logger.info(logPrefix + 'Success')
} catch (e) {
// This can fail if the user backgrounds too quickly, so throw up a local notification
// just in case to get their attention.
PushNotifications.localNotification({
message: `Failed to save ${saveType} to camera roll`,
})
logger.debug(logPrefix + 'failed to save: ' + e)
throw e
} finally {
require('rn-fetch-blob').default.fs.unlink(filePath)
}
}
const saveImage = (uri: string) => (
CameraRoll.saveToCameraRoll(uri, 'photo')
)
workId,
workTitle,
workType,
saveImageSettings,
imageIndex || index,
);
try {
const res = await RNFetchBlob.config({
path: `${imagesDir}/${fileName}`,
}).fetch('GET', url, {
referer: 'http://www.pixiv.net',
});
const filePath = res.path();
if (Platform.OS === 'ios') {
try {
await CameraRoll.saveToCameraRoll(filePath);
this.showToast(
i18n.formatString(i18n.saveImageSuccess, fileName),
);
} catch (err) {
this.showToast(i18n.formatString(i18n.saveImageError, fileName));
}
} else if (Platform.OS === 'android') {
this.showToast(i18n.formatString(i18n.saveImageSuccess, fileName));
try {
await RNFetchBlob.fs.scanFile([{ path: filePath }]);
} catch (err) {}
}
} catch (err) {
this.showToast(i18n.formatString(i18n.saveImageError, fileName));
}
});
export const saveAttachmentDialog = async (filePath: string) => {
const goodPath = filePath
logger.debug('saveAttachment: ', goodPath)
await requestPermissionsToWrite()
return CameraRoll.saveToCameraRoll(goodPath)
}
const _saveImage = async uri => {
try {
if (Platform.OS === 'android') {
await checkAndroidPermission();
uri = `file://${await _downloadImage(uri)}`;
}
CameraRoll.saveToCameraRoll(uri)
.then(res => {
Alert.alert(
intl.formatMessage({ id: 'alert.success' }),
intl.formatMessage({ id: 'post.image_saved' }),
[{ text: 'OK' }],
{ cancelable: false },
);
})
.catch(error => {
Alert.alert(
intl.formatMessage({ id: 'post.image_saved_error' }),
error.message,
[{ text: 'OK' }],
{
cancelable: false,
},
}).start(async () => {
await CameraRoll.saveToCameraRoll(videoPath, 'video');
this.props.onDownloadSuccess();
InteractionManager.runAfterInteractions(() => {
this.setState({force: false, isVideo: false});
});
});
};
saveImageWithIOS = async (url: string) => {
try {
await CameraRoll.saveToCameraRoll(url, 'photo');
this.showToast('保存成功');
} catch (e) {
this.showToast('保存失败');
}
};