devfelipereis / ion-media-cache

Use this directive to save image in device ios or android.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ion Media Cache

Directive cache multimedia.

Usage notice

With this directive you can download file to the device using Fetch API and render the source in the component. If your application is PWA, it use FileSystem to save the blob.

Supported Platforms.

* Android
* Browser
* Ios

Installation

npm i ion-media-cache@latest.

require installation ionic native

File

Quickstart

Import ion-media-cache in you module page.

// Import the module
import { IonMediaCacheModule } from 'ion-media-cache';
...
@NgModule({
    (...)
    imports: [
        IonMediaCacheModule
    ],
    (...)
})
export class PageModule {}

Usage

image

<img [url]="image" [customCache]="{fallbackUrl: '/assets/img/default.png'}" alt="ion-media-cache" />

<ion-img [url]="image" [customCache]="{fallbackUrl: '/assets/img/default.png'}"></ion-img>

custom div background

<div
    [url]="image"
    [customCache]="{fallbackUrl: '/assets/img/default.png', render: 'background', spinner: false, fallbackReload: false}"
    style="background-repeat: no-repeat;background-position: center;background-size: cover;width: 100%;height: 100%;"></div>

audio

<audio controls name="media">
  <source [url]="audio" customCache>
</audio>

Events

<ion-img
    #elRef
    [url]="image"
    [customCache]="{ fallbackUrl: '/assets/img/bienvenida.png', cache_expire: {time: n.component?.expire_subimage, data: data } }"
    (onExpireCache)="onExpireCache($event, elRef)"
    (ionImgDidLoad)="ionImgDidLoad($event)"
    (ionError)="ionError($event)"
></ion-img>
onExpireCache(event) {
    console.log(event);
    event.el.clearImageCache(event.data.url);
}
ionImgDidLoad(event) {
    console.log(event);
}
ionError(event) {
    console.log(event);
}

Optional custom cache

customCache = {
    debugMode: false,                       // boolean default = false;
    enabled: true,                          // booleandefault = true;
    corsFromHeroku: false                    // fix https://stackoverflow.com/a/21136980/7638125
    fallbackUrl: '',                        // string usage uri fail 'assetes/img/default.png'.
    concurrency: 5,                         // number default = 5, only on android, ios.
    maxCacheSize: -1,                       // number default -1, without limit.
    maxCacheAge: -1,                        // number default -1, without limit.
    httpHeaders: ,                          // any default = {}.
    fileNameCachedWithExtension: true,      // boolean default = true, save file with extension.
    fallbackFileNameCachedExtension: '.jpg',// string default '.jpg', extension to save.
    cacheDirectoryType: 'external',         // 'cache' | 'data' | 'external' default = 'external'.
    imageReturnType: 'uri',                 // 'base64' | 'uri' default = 'uri'.
    cacheDirectoryName: 'ion-media-cache',  // string default = 'ion-media-cache'.
    cache_expire: undefined,                // any default undefined, usage { time: new Date().getTime() + (1000 * [unit_second]) }.
    cache = true,                           // boolean defaul = true, activeted cache.
    render: 'src',                          // string default, render to property src.
    spinner: `
    <ion-spinner name="crescent">
    </ion-spinner>`,                        // any usage innertHtml. or false to disabled spinner
    fallbackReload: `
    <ion-icon name="cloud-offline" style="font-size: 2.7em;"></ion-icon>
    `,                                      // any;usage innertHtml. or false to disable fallbackReload

}

IonMediaCache

// Print current cache loader in the DOM
// Use console inspect in the browser
IonMediaCache

Testing fetch

fetch('http://cors-anywhere.herokuapp.com/https://lorempixel.com/640/480/?60789', {
   headers: {},
}).then((response) => {
   return response.blob();
}).then((blob) => {
   console.log(blob);
}).catch((e) => console.log(e));

Cors Erros Configuring Cors For Laravel Public Storage

fix stackoverflow

customCache = {
    corsFromHeroku: true // this is default false, limited use fix https://stackoverflow.com/a/21136980/7638125
}

CORS Anywhere is a NodeJS proxy which adds CORS headers to the proxied request.

If you expect lots of traffic, please host your own instance of CORS Anywhere, and make sure that the CORS Anywhere server only whitelists your site to prevent others from using your instance of CORS Anywhere as an open proxy.

Demo Server CORS Anywhere

About

Use this directive to save image in device ios or android.


Languages

Language:TypeScript 97.1%Language:JavaScript 2.9%