Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #26 #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/FileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
14 changes: 10 additions & 4 deletions lib/imageCacheHoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 <Image> component with style prop passed through.
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions tests/CacheableImage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions tests/FileSystem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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: () => {
Expand All @@ -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: () => {
Expand All @@ -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: () => {
Expand All @@ -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: () => {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jest.mock('react-native', () => {

});

jest.mock('react-native-fetch-blob', () => {
jest.mock('rn-fetch-blob', () => {

const { mockData } = require('./mockData');

Expand Down
6 changes: 3 additions & 3 deletions tests/imageCacheHoc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3749,13 +3749,6 @@ react-devtools-core@^2.5.0:
shell-quote "^1.6.1"
ws "^2.0.3"

[email protected]:
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"
Expand Down Expand Up @@ -4081,6 +4074,13 @@ rimraf@~2.2.6:
version "2.2.8"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.2.8.tgz#e439be2aaee327321952730f99a8929e4fc50582"

[email protected]:
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"

[email protected]:
version "1.2.0"
resolved "https://registry.yarnpkg.com/rndm/-/rndm-1.2.0.tgz#f33fe9cfb52bbfd520aa18323bc65db110a1b76c"
Expand Down