|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { Cordova, IonicNativePlugin, Plugin } from '@ionic-native/core'; |
| 3 | + |
| 4 | +export enum NotificationVisibility { |
| 5 | + Visible = 0, |
| 6 | + VisibleNotifyCompleted = 1, |
| 7 | + VisibilityHidden = 2, |
| 8 | + VisibleNotifyOnlyCompletion = 3 |
| 9 | +} |
| 10 | + |
| 11 | +export interface DownloadHttpHeader { |
| 12 | + header: string; |
| 13 | + value: string; |
| 14 | +} |
| 15 | + |
| 16 | +export interface DestinationDirectory { |
| 17 | + dirType: string; |
| 18 | + subPath: string; |
| 19 | +} |
| 20 | + |
| 21 | +export interface DownloadRequest { |
| 22 | + /** |
| 23 | + * Location of the resource to download |
| 24 | + */ |
| 25 | + uri: string; |
| 26 | + |
| 27 | + /** |
| 28 | + * Set the title of this download, to be displayed in notifications (if enabled). |
| 29 | + * If no title is given, a default one will be assigned based on the download filename, once the download starts. |
| 30 | + */ |
| 31 | + title?: string; |
| 32 | + /** |
| 33 | + * Set a description of this download, to be displayed in notifications (if enabled) |
| 34 | + */ |
| 35 | + description?: string; |
| 36 | + /** |
| 37 | + * Set the MIME content type of this download. This will override the content type declared in the server's response. |
| 38 | + */ |
| 39 | + mimeType?: string; |
| 40 | + /** |
| 41 | + * Set whether this download should be displayed in the system's Downloads UI. True by default. |
| 42 | + */ |
| 43 | + visibleInDownloadsUi?: boolean; |
| 44 | + /** |
| 45 | + * Control whether a system notification is posted by the download manager while this download is running or when it is completed. |
| 46 | + */ |
| 47 | + notificationVisibility?: NotificationVisibility; |
| 48 | + /** |
| 49 | + * Set the local destination for the downloaded file to a path within the application's external files directory |
| 50 | + */ |
| 51 | + destinationInExternalFilesDir?: DestinationDirectory; |
| 52 | + /** |
| 53 | + * Set the local destination for the downloaded file to a path within the public external storage directory |
| 54 | + */ |
| 55 | + destinationInExternalPublicDir?: DestinationDirectory; |
| 56 | + /** |
| 57 | + * Set the local destination for the downloaded file. |
| 58 | + * Must be a file URI to a path on external storage, and the calling application must have the WRITE_EXTERNAL_STORAGE permission. |
| 59 | + */ |
| 60 | + destinationUri?: string; |
| 61 | + /** |
| 62 | + * Add an HTTP header to be included with the download request. The header will be added to the end of the list. |
| 63 | + */ |
| 64 | + headers?: DownloadHttpHeader[]; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * @name Downloader |
| 69 | + * @description |
| 70 | + * This plugin is designed to support downloading files using Android DownloadManager. |
| 71 | + * |
| 72 | + * |
| 73 | + * @usage |
| 74 | + * ```typescript |
| 75 | + * import { Downloader } from '@ionic-native/downloader/ngx'; |
| 76 | + * |
| 77 | + * |
| 78 | + * constructor(private downloader: Downloader) { } |
| 79 | + * |
| 80 | + * ... |
| 81 | + * |
| 82 | + * var request: DownloadRequest = { |
| 83 | + * uri: YOUR_URI, |
| 84 | + * title: 'MyDownload', |
| 85 | + * description: '', |
| 86 | + * mimeType: '', |
| 87 | + * visibleInDownloadsUi: true, |
| 88 | + * notificationVisibility: NotificationVisibility.VisibleNotifyCompleted, |
| 89 | + * destinationInExternalFilesDir: { |
| 90 | + * dirType: 'Downloads', |
| 91 | + * subPath: 'MyFile.apk' |
| 92 | + * } |
| 93 | + * }; |
| 94 | + * |
| 95 | + * |
| 96 | + * this.downloader.download(request) |
| 97 | + * .then((location: string) => console.log('File downloaded at:'+location)) |
| 98 | + * .catch((error: any) => console.error(error)); |
| 99 | + * |
| 100 | + * ``` |
| 101 | + * @interfaces |
| 102 | + * NotificationVisibility |
| 103 | + * Header |
| 104 | + * DestinationDirectory |
| 105 | + * DownloadHttpHeader |
| 106 | + */ |
| 107 | +@Plugin({ |
| 108 | + pluginName: 'Downloader', |
| 109 | + plugin: 'integrator-cordova-plugin-downloader', |
| 110 | + pluginRef: 'cordova.plugins.Downloader', |
| 111 | + repo: 'https://github.com/Luka313/integrator-cordova-plugin-downloader.git', |
| 112 | + platforms: ['Android'] |
| 113 | +}) |
| 114 | +@Injectable() |
| 115 | +export class Downloader extends IonicNativePlugin { |
| 116 | + /** |
| 117 | + * Starts a new download and returns location of the downloaded file on completion |
| 118 | + * @param request {DownloadRequest} |
| 119 | + */ |
| 120 | + @Cordova() |
| 121 | + download(request: DownloadRequest): Promise<string> { |
| 122 | + return; |
| 123 | + } |
| 124 | +} |
0 commit comments