diff --git a/lib/FileSystem.js b/lib/FileSystem.js index 870fcff..02288e0 100644 --- a/lib/FileSystem.js +++ b/lib/FileSystem.js @@ -9,7 +9,7 @@ import { Platform } from 'react-native'; import pathLib from 'path'; -import RNFetchBlob from 'react-native-fetch-blob'; +import RNFetchBlob from 'rn-fetch-blob'; import sha1 from 'crypto-js/sha1'; import URL from 'url-parse'; diff --git a/lib/imageCacheHoc.js b/lib/imageCacheHoc.js index 4fc4fde..9fb9199 100644 --- a/lib/imageCacheHoc.js +++ b/lib/imageCacheHoc.js @@ -31,6 +31,8 @@ export default function imageCacheHoc(Image, options = {}) { if (options.fileDirName && typeof options.fileDirName !== 'string') { throw new Error('fileDirName option must be string'); } if (options.defaultPlaceholder && (!options.defaultPlaceholder.component || !options.defaultPlaceholder.props)) { throw new Error('defaultPlaceholder option object must include "component" and "props" properties (props can be an empty object)'); } + const defaultCachePruneTriggerLimit = 1024 * 1024 * 15; // Maximum size of image file cache in bytes before pruning occurs. Defaults to 15 MB. + return class extends React.PureComponent { static propTypes = { @@ -57,7 +59,9 @@ export default function imageCacheHoc(Image, options = {}) { */ static async cacheFile(url, permanent = false) { - const fileSystem = FileSystemFactory(); + const cachePruneTriggerLimit = options.cachePruneTriggerLimit || defaultCachePruneTriggerLimit; + const fileDirName = options.fileDirName || null; + const fileSystem = FileSystemFactory(cachePruneTriggerLimit, fileDirName); const localFilePath = await fileSystem.getLocalFilePathFromUrl(url, permanent); return { @@ -76,8 +80,10 @@ export default function imageCacheHoc(Image, options = {}) { * @returns {Promise} promise that resolves to an object that contains the flush results. */ static async flush() { - - const fileSystem = FileSystemFactory(); + + const cachePruneTriggerLimit = options.cachePruneTriggerLimit || defaultCachePruneTriggerLimit; + const fileDirName = options.fileDirName || null; + const fileSystem = FileSystemFactory(cachePruneTriggerLimit, fileDirName); const results = await Promise.all([fileSystem.unlink('permanent'), fileSystem.unlink('cache')]); return { @@ -105,7 +111,7 @@ export default function imageCacheHoc(Image, options = {}) { this.options = { validProtocols: options.validProtocols || ['https'], fileHostWhitelist: options.fileHostWhitelist || [], - cachePruneTriggerLimit: options.cachePruneTriggerLimit || 1024 * 1024 * 15, // Maximum size of image file cache in bytes before pruning occurs. Defaults to 15 MB. + cachePruneTriggerLimit: options.cachePruneTriggerLimit || defaultCachePruneTriggerLimit, fileDirName: options.fileDirName || null, // Namespace local file writing to this directory. Defaults to 'react-native-image-cache-hoc'. defaultPlaceholder: options.defaultPlaceholder || null, // Default placeholder component to render while remote image file is downloading. Can be overridden with placeholder prop. Defaults to component with style prop passed through. }; diff --git a/package.json b/package.json index eb66843..6668b2f 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "crypto-js": "^3.1.9-1", "path": "^0.12.7", "prop-types": "^15.6.0", - "react-native-fetch-blob": "0.10.8", + "rn-fetch-blob": "0.10.12", "react-native-uuid": "^1.4.9", "traverse": "^0.6.6", "url-parse": "^1.2.0", diff --git a/tests/CacheableImage.test.js b/tests/CacheableImage.test.js index 3cd9726..4dd0c47 100644 --- a/tests/CacheableImage.test.js +++ b/tests/CacheableImage.test.js @@ -93,7 +93,7 @@ describe('CacheableImage', function() { it('#cacheFile static method should work as expected for cache dir files.', () => { // RNFetchBlob Mocks - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); // Mock that file does not exist on local fs. RNFetchBlob.fs.exists @@ -125,7 +125,7 @@ describe('CacheableImage', function() { it('#cacheFile static method should work as expected for permanent dir files.', () => { // RNFetchBlob Mocks - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); // Mock that file does not exist on local fs. RNFetchBlob.fs.exists @@ -157,7 +157,7 @@ describe('CacheableImage', function() { it('#flush static method should work as expected.', () => { // RNFetchBlob Mocks - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); // Mock unlink to always be true. RNFetchBlob.fs.unlink diff --git a/tests/FileSystem.test.js b/tests/FileSystem.test.js index 0f8cbe1..8b8b775 100644 --- a/tests/FileSystem.test.js +++ b/tests/FileSystem.test.js @@ -74,7 +74,7 @@ describe('lib/FileSystem', function() { it('#exists mocked as true.', () => { - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); RNFetchBlob.fs.exists .mockReturnValue(true); @@ -191,7 +191,7 @@ describe('lib/FileSystem', function() { it('#getLocalFilePathFromUrl should return local filepath if it exists on local fs in permanent dir.', () => { - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); RNFetchBlob.fs.exists .mockReturnValueOnce(true) // mock exist in local permanent dir .mockReturnValue(true); @@ -207,7 +207,7 @@ describe('lib/FileSystem', function() { it('#getLocalFilePathFromUrl should return local filepath if it exists on local fs in cache dir.', () => { - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); RNFetchBlob.fs.exists .mockReturnValueOnce(false) // mock not exist in local permanent dir .mockReturnValueOnce(true) // mock exist in local cache dir @@ -224,7 +224,7 @@ describe('lib/FileSystem', function() { it('#getLocalFilePathFromUrl should download file and write to disk (default to cache dir) if it does not exist on local fs.', () => { - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); RNFetchBlob.fs.exists .mockReturnValueOnce(false) // mock not exist in local permanent dir .mockReturnValueOnce(false) // mock not exist in local cache dir @@ -268,7 +268,7 @@ describe('lib/FileSystem', function() { const fileSystem = FileSystemFactory(); - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); RNFetchBlob.fetch .mockReturnValue({ path: () => { @@ -291,7 +291,7 @@ describe('lib/FileSystem', function() { const fileSystem = FileSystemFactory(); - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); RNFetchBlob.fetch .mockReturnValue({ path: () => { @@ -318,7 +318,7 @@ describe('lib/FileSystem', function() { const fileSystem = FileSystemFactory(); - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); RNFetchBlob.fetch .mockReturnValue({ path: () => { @@ -345,7 +345,7 @@ describe('lib/FileSystem', function() { const fileSystem = FileSystemFactory(); - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); RNFetchBlob.fetch .mockReturnValue({ path: () => { @@ -398,7 +398,7 @@ describe('lib/FileSystem', function() { it('#unlink should work as expected for valid paths.', () => { // RNFetchBlob Mocks - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); // Mock unlink to be true. RNFetchBlob.fs.unlink diff --git a/tests/config.js b/tests/config.js index a8ac74e..c116ab9 100644 --- a/tests/config.js +++ b/tests/config.js @@ -19,7 +19,7 @@ jest.mock('react-native', () => { }); -jest.mock('react-native-fetch-blob', () => { +jest.mock('rn-fetch-blob', () => { const { mockData } = require('./mockData'); diff --git a/tests/imageCacheHoc.test.js b/tests/imageCacheHoc.test.js index 1f163f4..f6f6c5e 100644 --- a/tests/imageCacheHoc.test.js +++ b/tests/imageCacheHoc.test.js @@ -25,7 +25,7 @@ describe('CacheableImage', function() { it('renders correctly', () => { //Mock values for local/remote file request logic. - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); RNFetchBlob.fs.exists .mockReturnValueOnce(false) // mock not exist in local permanent dir .mockReturnValueOnce(false) // mock not exist in local cache dir @@ -77,7 +77,7 @@ describe('CacheableImage', function() { it('renders correctly with placeholder prop set', () => { //Mock values for local/remote file request logic. - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); RNFetchBlob.fs.exists .mockReturnValueOnce(false) // mock not exist in local permanent dir .mockReturnValueOnce(false) // mock not exist in local cache dir @@ -141,7 +141,7 @@ describe('CacheableImage', function() { it('renders correctly with placeholder option set', () => { //Mock values for local/remote file request logic. - const RNFetchBlob = require('react-native-fetch-blob'); + const RNFetchBlob = require('rn-fetch-blob'); RNFetchBlob.fs.exists .mockReturnValueOnce(false) // mock not exist in local permanent dir .mockReturnValueOnce(false) // mock not exist in local cache dir diff --git a/yarn.lock b/yarn.lock index 2280196..6ff339f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3749,13 +3749,6 @@ react-devtools-core@^2.5.0: shell-quote "^1.6.1" ws "^2.0.3" -react-native-fetch-blob@0.10.8: - version "0.10.8" - resolved "https://registry.yarnpkg.com/react-native-fetch-blob/-/react-native-fetch-blob-0.10.8.tgz#4fc256abae0cb5f10e7c41f28c11b3ff330d72a9" - dependencies: - base-64 "0.1.0" - glob "7.0.6" - react-native-uuid@^1.4.9: version "1.4.9" resolved "https://registry.yarnpkg.com/react-native-uuid/-/react-native-uuid-1.4.9.tgz#a526742f8fddfe6414500655212ca8d109c40229" @@ -4081,6 +4074,13 @@ rimraf@~2.2.6: version "2.2.8" resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.2.8.tgz#e439be2aaee327321952730f99a8929e4fc50582" +rn-fetch-blob@0.10.12: + version "0.10.12" + resolved "https://registry.yarnpkg.com/rn-fetch-blob/-/rn-fetch-blob-0.10.12.tgz#7b9a309149eb136a1b063b282a1847a205297924" + dependencies: + base-64 "0.1.0" + glob "7.0.6" + rndm@1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/rndm/-/rndm-1.2.0.tgz#f33fe9cfb52bbfd520aa18323bc65db110a1b76c"