diff --git a/app/screens/image_preview/downloader.android.js b/app/screens/image_preview/downloader.android.js index 3154543f0..d8447cde0 100644 --- a/app/screens/image_preview/downloader.android.js +++ b/app/screens/image_preview/downloader.android.js @@ -89,7 +89,7 @@ export default class Downloader extends PureComponent { ToastAndroid.show(started, ToastAndroid.SHORT); onDownloadStart(); - let dest = `${RNFetchBlob.fs.dirs.DownloadDir}/${data.id}-${file.caption}`; + let dest = `${RNFetchBlob.fs.dirs.DownloadDir}/${data.id}-${data.name}`; let downloadFile = true; if (data.localPath) { diff --git a/app/utils/file.js b/app/utils/file.js index 1ca4257ca..4a77576bd 100644 --- a/app/utils/file.js +++ b/app/utils/file.js @@ -230,8 +230,8 @@ function populateMaps() { } export function getLocalFilePathFromFile(dir, file) { - if (dir && file && file.caption && file.data && file.data.id) { - return `${dir}/${file.data.id}-${decodeURIComponent(file.caption).replace(/\s+/g, '-')}`; + if (dir && file && file.data && file.data.id && file.data.extension) { + return `${dir}/${file.data.id}.${file.data.extension}`; } return null; diff --git a/app/utils/file.test.js b/app/utils/file.test.js index ddefabaf0..25430d79f 100644 --- a/app/utils/file.test.js +++ b/app/utils/file.test.js @@ -1,7 +1,7 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import {getExtensionFromContentDisposition} from 'app/utils/file'; +import {generateId, getLocalFilePathFromFile, getExtensionFromContentDisposition} from 'app/utils/file'; describe('getExtensionFromContentDisposition', () => { it('should return the extracted the extension', () => { @@ -44,4 +44,34 @@ describe('getExtensionFromContentDisposition', () => { expect(extension).toBe(null); }); }); + + it('should return the path for the file', () => { + const data = { + id: generateId(), + name: 'Some Video file.mp4', + extension: 'mp4', + }; + + const localFile = getLocalFilePathFromFile('Videos', {data}); + expect(localFile).toBe(`Videos/${data.id}.${data.extension}`); + }); + + it('should return the null as the path if it does not have an id set', () => { + const data = { + name: 'Some Video file.mp4', + extension: 'mp4', + }; + + const localFile = getLocalFilePathFromFile('Videos', {data}); + expect(localFile).toBeNull(); + }); + + it('should return the null as the path if it does not have an extension set', () => { + const data = { + id: generateId(), + name: 'Some Video file.mp4', + }; + const localFile = getLocalFilePathFromFile('Videos', {data}); + expect(localFile).toBeNull(); + }); });