diff --git a/app/components/post_attachment_image/__snapshots__/index.test.js.snap b/app/components/post_attachment_image/__snapshots__/index.test.js.snap new file mode 100644 index 000000000..c78be4bf6 --- /dev/null +++ b/app/components/post_attachment_image/__snapshots__/index.test.js.snap @@ -0,0 +1,42 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`PostAttachmentImage should match snapshot 1`] = ` + + + + + +`; diff --git a/app/components/post_attachment_image/index.js b/app/components/post_attachment_image/index.js index ff6c6aeb0..7216fec5c 100644 --- a/app/components/post_attachment_image/index.js +++ b/app/components/post_attachment_image/index.js @@ -47,7 +47,7 @@ export default class PostAttachmentImage extends React.PureComponent { diff --git a/app/components/post_attachment_image/index.test.js b/app/components/post_attachment_image/index.test.js new file mode 100644 index 000000000..9e81959ec --- /dev/null +++ b/app/components/post_attachment_image/index.test.js @@ -0,0 +1,23 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +import React from 'react'; +import {shallow} from 'enzyme'; + +import PostAttachmentImage from './index'; + +describe('PostAttachmentImage', () => { + const baseProps = { + height: 100, + width: 100, + onError: jest.fn(), + onImagePress: jest.fn(), + uri: 'uri', + }; + + it('should match snapshot', () => { + const wrapper = shallow(); + + expect(wrapper.getElement()).toMatchSnapshot(); + }); +}); \ No newline at end of file diff --git a/app/utils/image_cache_manager.js b/app/utils/image_cache_manager.js index 1f0630b4b..80177c1a0 100644 --- a/app/utils/image_cache_manager.js +++ b/app/utils/image_cache_manager.js @@ -6,6 +6,8 @@ import {Platform} from 'react-native'; import RNFetchBlob from 'rn-fetch-blob'; +import Url from 'url-parse'; + import {Client4} from 'mattermost-redux/client'; import {DeviceTypes} from 'app/constants'; @@ -22,11 +24,12 @@ let siteUrl; export default class ImageCacheManager { static listeners = {}; - static cache = async (filename, uri, listener) => { + static cache = async (filename, fileUri, listener) => { if (!listener) { console.warn('Unable to cache image when no listener is provided'); // eslint-disable-line no-console } + const uri = parseUri(fileUri); const {path, exists} = await getCacheFile(filename, uri); const prefix = Platform.OS === 'android' ? 'file://' : ''; let pathWithPrefix = `${prefix}${path}`; @@ -94,6 +97,11 @@ export default class ImageCacheManager { }; } +const parseUri = (uri) => { + const url = new Url(uri); + return url.href; +}; + export const getCacheFile = async (name, uri) => { const filename = name || uri.substring(uri.lastIndexOf('/'), uri.indexOf('?') === -1 ? uri.length : uri.indexOf('?')); const defaultExt = `.${getExtensionFromMime(DEFAULT_MIME_TYPE)}`; @@ -150,7 +158,7 @@ const notifyAll = (uri, path) => { }; // taken from https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery -const hashCode = (str) => { +export const hashCode = (str) => { let hash = 0; let i; let chr; diff --git a/app/utils/image_cache_manager.test.js b/app/utils/image_cache_manager.test.js index d562d7420..6ac87b1ae 100644 --- a/app/utils/image_cache_manager.test.js +++ b/app/utils/image_cache_manager.test.js @@ -2,7 +2,7 @@ // See LICENSE.txt for license information. import RNFetchBlob from 'rn-fetch-blob'; -import ImageCacheManager, {getCacheFile} from 'app/utils/image_cache_manager'; +import ImageCacheManager, {getCacheFile, hashCode} from 'app/utils/image_cache_manager'; import {emptyFunction} from 'app/utils/general'; import * as fileUtils from 'app/utils/file'; import mattermostBucket from 'app/mattermost_bucket'; @@ -292,4 +292,22 @@ describe('ImageCacheManager.cache', () => { expect(existingListener).toHaveBeenCalledWith(fileUri); expect(newListener).toHaveBeenCalledWith(fileUri); }); + + it('should parse the uri to get the correct protocol', async () => { + const fileUri = 'HTTPS://file-uri/ABC123'; + const expectedFileUri = 'https://file-uri/ABC123'; + const fileName = null; + + const incorrectHash = hashCode(fileUri); + const expectedHash = hashCode(expectedFileUri); + expect(incorrectHash).not.toBe(expectedHash); + + RNFetchBlob.fs.exists.mockReturnValueOnce(false); + RNFetchBlob.fs.existsWithDiffExt.mockReturnValueOnce(null); + RNFetchBlob.fetch.mockReturnValueOnce({respInfo: {respType: '', headers: {}}}); + + const path = await ImageCacheManager.cache(fileName, fileUri, emptyFunction); // eslint-disable-line no-await-in-loop + const localFilename = path.substring(path.lastIndexOf('/') + 1, path.length); + expect(localFilename).toEqual(`${expectedHash}.png`); + }); });