From 1787a04f6ee85a7ce2c61354e4e869ec88334257 Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Mon, 5 Nov 2018 15:24:30 -0300 Subject: [PATCH] Set the file prefix in image_cache_manager (#2313) --- app/components/emoji/emoji.js | 8 +------- .../file_attachment_list/file_attachment_list.js | 8 +------- .../markdown/markdown_image/markdown_image.js | 6 +----- .../message_attachments/message_attachment.js | 8 +------- .../post_attachment_opengraph.js | 12 ++---------- .../post_body_additional_content.js | 10 ++-------- .../progressive_image/progressive_image.js | 16 ++-------------- app/components/team_icon/team_icon.js | 8 +------- app/utils/image_cache_manager.js | 10 ++++++---- 9 files changed, 17 insertions(+), 69 deletions(-) diff --git a/app/components/emoji/emoji.js b/app/components/emoji/emoji.js index ff2de2395..388f1fb24 100644 --- a/app/components/emoji/emoji.js +++ b/app/components/emoji/emoji.js @@ -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, }); }; diff --git a/app/components/file_attachment_list/file_attachment_list.js b/app/components/file_attachment_list/file_attachment_list.js index 3fbec8455..311e184b9 100644 --- a/app/components/file_attachment_list/file_attachment_list.js +++ b/app/components/file_attachment_list/file_attachment_list.js @@ -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({ diff --git a/app/components/markdown/markdown_image/markdown_image.js b/app/components/markdown/markdown_image/markdown_image.js index 99825dd66..489b89859 100644 --- a/app/components/markdown/markdown_image/markdown_image.js +++ b/app/components/markdown/markdown_image/markdown_image.js @@ -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); diff --git a/app/components/message_attachments/message_attachment.js b/app/components/message_attachments/message_attachment.js index a73e1e8d2..e604f9558 100644 --- a/app/components/message_attachments/message_attachment.js +++ b/app/components/message_attachments/message_attachment.js @@ -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) { diff --git a/app/components/post_attachment_opengraph/post_attachment_opengraph.js b/app/components/post_attachment_opengraph/post_attachment_opengraph.js index de55813b5..de587d4ce 100644 --- a/app/components/post_attachment_opengraph/post_attachment_opengraph.js +++ b/app/components/post_attachment_opengraph/post_attachment_opengraph.js @@ -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); diff --git a/app/components/post_body_additional_content/post_body_additional_content.js b/app/components/post_body_additional_content/post_body_additional_content.js index 1de92008a..addbadcbb 100644 --- a/app/components/post_body_additional_content/post_body_additional_content.js +++ b/app/components/post_body_additional_content/post_body_additional_content.js @@ -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})); } diff --git a/app/components/progressive_image/progressive_image.js b/app/components/progressive_image/progressive_image.js index 1fe939d7b..00c3c87ec 100644 --- a/app/components/progressive_image/progressive_image.js +++ b/app/components/progressive_image/progressive_image.js @@ -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); diff --git a/app/components/team_icon/team_icon.js b/app/components/team_icon/team_icon.js index 5151c3e2e..649553bd1 100644 --- a/app/components/team_icon/team_icon.js +++ b/app/components/team_icon/team_icon.js @@ -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() { diff --git a/app/utils/image_cache_manager.js b/app/utils/image_cache_manager.js index be5c58bf9..98a086ffa 100644 --- a/app/utils/image_cache_manager.js +++ b/app/utils/image_cache_manager.js @@ -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);