Set the file prefix in image_cache_manager (#2313)

This commit is contained in:
Elias Nahum 2018-11-05 15:24:30 -03:00
parent 76ed62b153
commit 1787a04f6e
No known key found for this signature in database
GPG key ID: E038DB71E0B61702
9 changed files with 17 additions and 69 deletions

View file

@ -82,14 +82,8 @@ export default class Emoji extends React.PureComponent {
}
setImageUrl = (imageUrl) => {
let prefix = '';
if (Platform.OS === 'android') {
prefix = 'file://';
}
const uri = `${prefix}${imageUrl}`;
this.setState({
imageUrl: uri,
imageUrl,
});
};

View file

@ -5,7 +5,6 @@ import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {
Keyboard,
Platform,
ScrollView,
StyleSheet,
TouchableOpacity,
@ -101,12 +100,7 @@ export default class FileAttachmentList extends Component {
}
if (cache) {
let path = cache.path;
if (Platform.OS === 'android') {
path = `file://${path}`;
}
uri = path;
uri = cache.path;
}
results.push({

View file

@ -187,11 +187,7 @@ export default class MarkdownImage extends React.Component {
};
setImageUrl = (imageURL) => {
let uri = imageURL;
if (Platform.OS === 'android') {
uri = `file://${imageURL}`;
}
const uri = imageURL;
this.setState({uri});
this.loadImageSize(uri);

View file

@ -267,13 +267,7 @@ export default class MessageAttachment extends PureComponent {
}
};
setImageUrl = (imageURL) => {
let imageUri = imageURL;
if (Platform.OS === 'android') {
imageUri = `file://${imageURL}`;
}
setImageUrl = (imageUri) => {
Image.getSize(imageUri, (width, height) => {
const dimensions = calculateDimensions(height, width, this.maxImageWidth);
if (this.mounted) {

View file

@ -6,7 +6,6 @@ import PropTypes from 'prop-types';
import {
Image,
Linking,
Platform,
Text,
TouchableOpacity,
TouchableWithoutFeedback,
@ -112,14 +111,7 @@ export default class PostAttachmentOpenGraph extends PureComponent {
};
getImageSize = (imageUrl) => {
let prefix = '';
if (Platform.OS === 'android') {
prefix = 'file://';
}
const uri = `${prefix}${imageUrl}`;
Image.getSize(uri, (width, height) => {
Image.getSize(imageUrl, (width, height) => {
const dimensions = calculateDimensions(height, width, this.getViewPostWidth());
if (this.mounted) {
@ -127,7 +119,7 @@ export default class PostAttachmentOpenGraph extends PureComponent {
...dimensions,
originalHeight: height,
originalWidth: width,
imageUrl: uri,
imageUrl,
});
}
}, () => null);

View file

@ -254,13 +254,7 @@ export default class PostBodyAdditionalContent extends PureComponent {
const viewPortWidth = deviceSize - VIEWPORT_IMAGE_OFFSET - (isReplyPost ? VIEWPORT_IMAGE_REPLY_OFFSET : 0);
if (link && path) {
let prefix = '';
if (Platform.OS === 'android') {
prefix = 'file://';
}
const uri = `${prefix}${path}`;
Image.getSize(uri, (width, height) => {
Image.getSize(path, (width, height) => {
if (!this.mounted) {
return;
}
@ -282,7 +276,7 @@ export default class PostBodyAdditionalContent extends PureComponent {
originalHeight: height,
originalWidth: width,
linkLoaded: true,
uri,
uri: path,
});
}, () => this.setState({linkLoadError: true}));
}

View file

@ -81,26 +81,14 @@ export default class ProgressiveImage extends PureComponent {
setImage = (uri) => {
if (this.subscribedToCache) {
let path = uri;
if (Platform.OS === 'android') {
path = `file://${uri}`;
}
this.setState({uri: path});
this.setState({uri});
}
};
setThumbnail = (thumb) => {
if (this.subscribedToCache) {
const {filename, imageUri} = this.props;
let path = thumb;
if (Platform.OS === 'android') {
path = `file://${thumb}`;
}
this.setState({thumb: path}, () => {
this.setState({thumb}, () => {
setTimeout(() => {
ImageCacheManager.cache(filename, imageUri, this.setImage);
}, 300);

View file

@ -6,7 +6,6 @@ import PropTypes from 'prop-types';
import {
Image,
Platform,
Text,
View,
} from 'react-native';
@ -50,12 +49,7 @@ export default class TeamIcon extends React.PureComponent {
}
setImageURL = (teamIcon) => {
let prefix = '';
if (Platform.OS === 'android') {
prefix = 'file://';
}
this.setState({teamIcon: `${prefix}${teamIcon}`});
this.setState({teamIcon});
};
render() {

View file

@ -3,6 +3,7 @@
// Based on the work done by https://github.com/wcandillon/react-native-expo-image-cache/
import {Platform} from 'react-native';
import RNFetchBlob from 'rn-fetch-blob';
import {DeviceTypes} from 'app/constants';
@ -21,10 +22,11 @@ export default class ImageCacheManager {
}
const {path, exists} = await getCacheFile(filename, uri);
const prefix = Platform.OS === 'android' ? 'file://' : '';
if (isDownloading(uri)) {
addListener(uri, listener);
} else if (exists) {
listener(path);
listener(`${prefix}${path}`);
} else {
addListener(uri, listener);
if (uri.startsWith('http')) {
@ -44,9 +46,9 @@ export default class ImageCacheManager {
throw new Error();
}
notifyAll(uri, path);
notifyAll(uri, `${prefix}${path}`);
} catch (e) {
RNFetchBlob.fs.unlink(path);
RNFetchBlob.fs.unlink(`${prefix}${path}`);
notifyAll(uri, uri);
}
} else {
@ -62,7 +64,7 @@ export default class ImageCacheManager {
export const getCacheFile = async (name, uri) => {
const filename = name || uri.substring(uri.lastIndexOf('/'), uri.indexOf('?') === -1 ? uri.length : uri.indexOf('?'));
const ext = filename.indexOf('.') === -1 ? '.png' : filename.substring(filename.lastIndexOf('.'));
const path = `${IMAGES_PATH}/${hashCode(uri)}${ext}`;
const path = `${IMAGES_PATH}/${Math.abs(hashCode(uri))}${ext}`;
try {
const isDir = await RNFetchBlob.fs.isDir(IMAGES_PATH);