Skip to content

Commit 45201ce

Browse files
westphalenihadeed
authored andcommitted
feat(): replace FileTransfer plugin with HttpClient (#125)
1 parent b0edd81 commit 45201ce

File tree

7 files changed

+47
-52
lines changed

7 files changed

+47
-52
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
<a name="4.2.2"></a>
2+
## [4.2.2](https://github.com/zyra/ionic-image-loader/compare/v4.2.1...v4.2.2) (2018-01-13)
3+
4+
5+
### Compatibility
6+
7+
* **provider:** use Angular HttpClient instead of deprecated FileTransfer plugin, closes ([#124](https://github.com/zyra/ionic-image-loader/issues/124))
8+
9+
110
<a name="4.2.1"></a>
211
## [4.2.1](https://github.com/zyra/ionic-image-loader/compare/v4.2.0...v4.2.1) (2017-09-07)
312

README.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
[![npm](https://img.shields.io/npm/dm/ionic-image-loader.svg)](https://www.npmjs.com/package/ionic-image-loader)
44

55
# Ionic Image Loader
6-
**Ionic** Module that loads images in a native background thread and caches them for later use. Uses `cordova-plugin-file` and `cordova-plugin-file-transfer` via [`ionic-native`](https://github.com/driftyco/ionic-native) wrappers.
6+
**Ionic** Module that loads images in a background thread and caches them for later use. Uses `HttpClient` from `Angular 4+`, and `cordova-plugin-file` via [`ionic-native`](https://github.com/driftyco/ionic-native) wrappers.
7+
78

89
## Features
910
- Downloads images via a **native thread**. Images will download faster and they will not use the Webview's resources.
@@ -36,9 +37,6 @@ npm install --save ionic-image-loader
3637
```
3738
npm i --save @ionic-native/file
3839
ionic cordova plugin add cordova-plugin-file
39-
40-
npm i --save @ionic-native/file-transfer
41-
ionic cordova plugin add cordova-plugin-file-transfer
4240
```
4341

4442
#### 3. Import `IonicImageLoader` module
@@ -267,13 +265,12 @@ Example:
267265
this.imageLoaderConfig.enableFallbackAsPlaceholder(true);
268266
```
269267
---
270-
#### setFileTransferOptions(options: any)
271-
Set options for FileTransfer plugin to use. If you would like to set a value for the `trustAllHosts` param, you can add it to the options object.
268+
#### setHttpRequestOptions(options: any)
269+
Set options for HttpClient to use.
272270
273271
Example:
274272
```ts
275-
this.imageLoaderConfig.setFileTransferOptions({
276-
trustAllHosts: true, // defaults to false
273+
this.imageLoaderConfig.setHttpRequestOptions({
277274
headers: {
278275
Authorization: 'Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=='
279276
}

package-lock.json

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ionic-image-loader",
3-
"version": "4.2.1",
3+
"version": "4.2.2",
44
"description": "Ionic Component and Service to load images in a background thread and cache them for later use",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",
@@ -39,15 +39,13 @@
3939
"@angular/platform-browser-dynamic": "4.4.4",
4040
"@ionic-native/core": "^4.3.3",
4141
"@ionic-native/file": "^4.3.3",
42-
"@ionic-native/file-transfer": "^4.3.3",
4342
"conventional-changelog-cli": "1.3.4",
4443
"ionic-angular": "3.8.0",
4544
"rxjs": "5.4.3",
4645
"typescript": "2.3.4",
4746
"zone.js": "0.8.18"
4847
},
4948
"peerDependencies": {
50-
"@ionic-native/file": "^4.0.0",
51-
"@ionic-native/file-transfer": "^4.0.0"
49+
"@ionic-native/file": "^4.0.0"
5250
}
5351
}

src/image-loader.module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import { NgModule, ModuleWithProviders } from '@angular/core';
1+
import { ModuleWithProviders, NgModule } from '@angular/core';
22
import { ImgLoader } from './components/img-loader';
33
import { ImageLoader } from './providers/image-loader';
44
import { ImageLoaderConfig } from './providers/image-loader-config';
55
import { IonicModule } from 'ionic-angular';
66
import { File } from '@ionic-native/file';
7-
import { FileTransfer } from '@ionic-native/file-transfer';
7+
import { HttpClientModule } from '@angular/common/http';
88

99
@NgModule({
1010
declarations: [
1111
ImgLoader
1212
],
1313
imports: [
14-
IonicModule
14+
IonicModule,
15+
HttpClientModule,
1516
],
1617
exports: [
1718
ImgLoader
@@ -25,7 +26,6 @@ export class IonicImageLoader {
2526
ImageLoaderConfig,
2627
ImageLoader,
2728
File,
28-
FileTransfer
2929
]
3030
};
3131
}

src/providers/image-loader-config.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Injectable } from '@angular/core';
2+
import { HttpHeaders } from '@angular/common/http';
23

34
@Injectable()
45
export class ImageLoaderConfig {
@@ -35,9 +36,7 @@ export class ImageLoaderConfig {
3536

3637
spinnerColor: string;
3738

38-
fileTransferOptions: any = {
39-
trustAllHosts: false
40-
};
39+
httpHeaders: HttpHeaders;
4140

4241
private _cacheDirectoryName: string = 'image-loader-cache';
4342

@@ -186,12 +185,21 @@ export class ImageLoaderConfig {
186185
this.spinnerColor = color;
187186
}
188187

188+
/**
189+
* Set headers options for the HttpClient transfers.
190+
* @param headers
191+
*/
192+
setHttpHeaders(headers: HttpHeaders): void {
193+
this.httpHeaders = headers;
194+
}
195+
189196
/**
190197
* Set options for the FileTransfer plugin
191198
* @param options
199+
* @deprecated FileTransfer plugin removed.
192200
*/
193201
setFileTransferOptions(options: { trustAllHosts: boolean; [key: string]: any; }): void {
194-
this.fileTransferOptions = options;
202+
// do nothing, plugin deprecated.
195203
}
196204

197205
}

src/providers/image-loader.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@angular/core';
2-
import { File, FileEntry, FileError, DirectoryEntry } from '@ionic-native/file';
3-
import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer';
2+
import { DirectoryEntry, File, FileEntry, FileError } from '@ionic-native/file';
3+
import { HttpClient } from '@angular/common/http';
44
import { ImageLoaderConfig } from "./image-loader-config";
55
import { Platform } from 'ionic-angular';
66
import { Observable } from 'rxjs/Observable';
@@ -22,7 +22,7 @@ interface QueueItem {
2222
export class ImageLoader {
2323

2424
get nativeAvailable(): boolean {
25-
return File.installed() && FileTransfer.installed();
25+
return File.installed();
2626
}
2727

2828
/**
@@ -51,8 +51,6 @@ export class ImageLoader {
5151
*/
5252
private queue: QueueItem[] = [];
5353

54-
private transferInstances: FileTransferObject[] = [];
55-
5654
private processing: number = 0;
5755

5856
private cacheIndex: IndexItem[] = [];
@@ -76,7 +74,7 @@ export class ImageLoader {
7674
constructor(
7775
private config: ImageLoaderConfig,
7876
private file: File,
79-
private fileTransfer: FileTransfer,
77+
private http: HttpClient,
8078
private platform: Platform
8179
) {
8280
if (!platform.is('cordova')) {
@@ -239,16 +237,6 @@ export class ImageLoader {
239237
// take the first item from queue
240238
const currentItem: QueueItem = this.queue.splice(0, 1)[0];
241239

242-
// create FileTransferObject instance if needed
243-
// we would only reach here if current jobs < concurrency limit
244-
// so, there's no need to check anything other than the length of
245-
// the FileTransferObject instances we have in memory
246-
if (this.transferInstances.length === 0) {
247-
this.transferInstances.push(this.fileTransfer.create());
248-
}
249-
250-
const transfer: FileTransferObject = this.transferInstances.splice(0, 1)[0];
251-
252240
// process more items concurrently if we can
253241
if (this.canProcess) this.processQueue();
254242

@@ -257,29 +245,30 @@ export class ImageLoader {
257245
// then will execute this function again to process any remaining items
258246
const done = () => {
259247
this.processing--;
260-
this.transferInstances.push(transfer);
261248
this.processQueue();
262249
};
263250

264-
const localPath = this.file.cacheDirectory + this.config.cacheDirectoryName + '/' + this.createFileName(currentItem.imageUrl);
251+
const localDir = this.file.cacheDirectory + this.config.cacheDirectoryName + '/';
252+
const fileName = this.createFileName(currentItem.imageUrl);
265253

266-
transfer.download(currentItem.imageUrl, localPath, Boolean(this.config.fileTransferOptions.trustAllHosts), this.config.fileTransferOptions)
267-
.then((file: FileEntry) => {
254+
this.http.get(currentItem.imageUrl, {
255+
responseType: 'blob',
256+
headers: this.config.httpHeaders,
257+
}).subscribe((data: Blob) => {
258+
this.file.writeFile(localDir, fileName, data).then((file: FileEntry) => {
268259
if (this.shouldIndex) {
269260
this.addFileToIndex(file).then(this.maintainCacheSize.bind(this));
270261
}
271262
return this.getCachedImagePath(currentItem.imageUrl);
272-
})
273-
.then((localUrl) => {
263+
}).then((localUrl) => {
274264
currentItem.resolve(localUrl);
275265
done();
276-
})
277-
.catch((e) => {
266+
}).catch((e) => {
278267
currentItem.reject();
279268
this.throwError(e);
280269
done();
281270
});
282-
271+
});
283272
}
284273

285274
/**

0 commit comments

Comments
 (0)