• This repository has been archived on 11/Aug/2020
  • Stars
    star
    206
  • Rank 190,504 (Top 4 %)
  • Language
    C
  • License
    Apache License 2.0
  • Created almost 10 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Download and cache remotely hosted content

phonegap-plugin-contentsync Build Status bitHound Score

Download and cache remotely hosted zipped content bundles, unzipping automatically.

Installation

This requires phonegap 5.0+ ( current stable v1.2.0 )

phonegap plugin add phonegap-plugin-contentsync

It is also possible to install via repo url directly ( unstable )

phonegap plugin add https://github.com/phonegap/phonegap-plugin-contentsync

Supported Platforms

  • Android
  • iOS
  • WP8

Quick Example

// Create a new instance of ContentSync pointing to zipped resource 'movie-1.zip' - note
// that the url need not end in zip - it just needs to point to something producing
// a application/octet-stream mime type
var sync = ContentSync.sync({
        src: 'https://myserver/assets/movie-1.zip',
        id: 'movie-1'
});

sync.on('progress', function(data) {
    // data.progress
});

sync.on('complete', function(data) {
    // data.localPath
});

sync.on('error', function(e) {
    // e
});

sync.on('cancel', function() {
    // triggered if event is cancelled
});

Security note:

For updating a production app using ContentSync.sync, always use HTTPS. Other Updaters have had vulnerabilities exposed when updating over insecure HTTP.

API

ContentSync.sync(options)

Parameter Description
options.src String URL to the remotely hosted content. For updates in production, this URL should always use HTTPS
options.id String Unique identifer to reference the cached content.
options.type String (Optional) Defines the copy strategy for the cached content.
The type replace is the default behaviour that deletes the old content and caches the new content.
The type merge will add the new content to the existing content. This will replace existing files, add new files, but never delete files.
The type local returns the full path to the cached content if it exists or downloads it from options.src if it doesn't. options.src is not required if cached content actually exists.
options.headers Object (Optional) Set of headers to use when requesting the remote content from options.src.
options.copyCordovaAssets Boolean (Optional) Copies cordova.js, cordova_plugins.js and plugins/ to sync'd folder. This operation happens after the source content has been cached, so it will override any existing Cordova assets. Default is false.
options.copyRootApp Boolean (Optional) Copies the www folder to sync'd folder. This operation happens before the source content has been cached, then the source content is cached and finally it copies cordova.js, cordova_plugins.js and plugins/ to sync'd folder to remain consistent with the installed plugins. Default is false.
options.timeout Double (Optional) Request timeout. Default is 15 seconds.
options.trustHost Boolean (Optional) Trust SSL host. Host defined in options.src will be trusted. Ignored if options.src is undefined. Not supported on Android.
options.manifest String (Optional) If specified the copyRootApp functionality will use the list of files contained in the manifest file during it's initial copy. {Android only}
options.validateSrc Boolean (Optional) Whether to validate src url with a HEAD request before download (ios only, default true).

Returns

  • Instance of ContentSync.

Example

var sync = ContentSync.sync({
        src: 'https://myserver/app/1',
        id: 'app-1'
});

sync.on(event, callback)

Parameter Description
event String Name of the event to listen to. See below for all the event names.
callback Function is called when the event is triggered.

sync.on('progress', callback)

The event progress will be triggered on each update as the native platform downloads and caches the content.

Callback Parameter Description
data.progress Integer Progress percentage between 0 - 100. The progress includes all actions required to cache the remote content locally. This is different on each platform, but often includes requesting, downloading, and extracting the cached content along with any system cleanup tasks.
data.status Integer Enumeration of PROGRESS_STATE to describe the current progress state.

Example

sync.on('progress', function(data) {
    // data.progress
    // data.status
});

sync.on('complete', callback)

The event complete will be triggered when the content has been successfully cached onto the device.

Callback Parameter Description
data.localPath String The file path to the cached content. The file path will be different on each platform and may be relative or absolute. However, it is guaraneteed to be a compatible reference in the browser.
data.cached Boolean Set to true if options.type is set to local and cached content exists. Set to false otherwise.

Example

sync.on('complete', function(data) {
    // data.localPath
    // data.cached
});

sync.on('error', callback)

The event error will trigger when an internal error occurs and the cache is aborted.

Callback Parameter Description
e.type Integer Enumeration of ERROR_STATE to describe the current error
e.responseCode Integer HTTP error code if available, -1 otherwise

Example

sync.on('error', function(e) {
    // e
});

sync.on('cancel', callback)

The event cancel will trigger when sync.cancel is called.

Callback Parameter Description
no parameters

Example

sync.on('cancel', function() {
    // user cancelled the sync operation
});

sync.cancel()

Cancels the content sync operation and triggers the cancel callback.

var sync = ContentSync.sync({
        src: 'https://myserver/app/1',
        id: 'app-1'
});

sync.on('cancel', function() {
    console.log('content sync was cancelled');
});

sync.cancel();

ContentSync.PROGRESS_STATE

An enumeration that describes the current progress state. The mapped String values can be customized for the user's app.

Integer Description
0 STOPPED
1 DOWNLOADING
2 EXTRACTING
3 COMPLETE

ContentSync.ERROR_STATE

An enumeration that describes the received error. The mapped String values can be customized for the user's app.

Error Code Description
1 INVALID_URL_ERR
2 CONNECTION_ERR
3 UNZIP_ERR

ContentSync.unzip || Zip.unzip - ContentSync.download

If you are using the Chromium Zip plugin this plugin won't work for you on iOS. However, it supports the same interface so you don't have to install both.

zip.unzip(<source zip>, <destination dir>, <callback>, [<progressCallback>]);

There is also an extra convenience method that can be used to download an archive

ContentSync.download(url, headers, cb)

The progress events described above also apply for these methods.

Example

ContentSync.PROGRESS_STATE[1] = 'Downloading the media content...';

ContentSync.loadUrl (cordova-ios > 4.x with cordova-plugin-wkwebview-engine)

Use this API to load assets after extraction on cordova-ios > 4.x and cordova-plugin-wkwebview-engine. Do not use document.location as it probably won't work. Make sure to prefix your url with file://

var sync = ContentSync.sync({
        src: 'https://myserver/app/1',
        id: 'app-1'
});

sync.on('complete', function(data) {
    ContentSync.loadUrl('file://' + data.localPath, function() {
        console.log('success');
    });
});

Working with the Native File System

One of the main benefits of the content sync plugin is that it does not depend on the File or FileTransfer plugins. As a result the end user should not care where the ContentSync plugin stores it's files as long as it fills the requirements that it is private and removed when it's associated app is uninstalled.

However, if you do need to use the File plugin to navigate the data downloaded by ContentSync you can use the following code snippet to get a DirectoryEntry for the synced content.

var sync = ContentSync.sync({
        src: 'https://myserver/app/1',
        id: 'app-1'
});

sync.on('complete', function(data) {
    window.resolveLocalFileSystemURL("file://" + data.localPath, function(entry) {
        // entry is a DirectoryEntry object
    }, function(error) {
        console.log("Error: " + error.code);
    });
});

As of version 1.2.0 of the plugin the location in which the plugin stores the synched content is equivaltent to the cordova.file.dataDirectory path from the cordova-plugin-file package. This is a change from previous versions so please be aware you may need to do a full sync after upgrading to version 1.2.0.

Platform Path
Android /data/data/<app-id>/files/<options.id>
iOS /var/mobile/Applications/<UUID>/Library/NoCloud/<options.id>

Copy Root App

The asset file system is pretty slow on Android so in order to speed up the initial copy of your app to the content sync location you can specify a manifest file on Android. The file must be in the format:

{
    'files': [
        'img/logo.png',
        'index.html',
        'js/index.js'
   ]
}

and if the file is placed in your apps www folder you would invoke it via:

var sync = ContentSync.sync({
        src: 'https://myserver/app/1',
        id: 'app-1',
        copyRootApp: true,
        manifest: 'manifest.json'
});

This results in the copyRootApp taking about a third of the time as when a manifest file is not specified.

Persistence of Synced Content

Content downloaded via this plugin persists between runs of the application or reboots of the phone. The content will only be removed if the application is uninstalled or you use the File API to remove the location of the synched content.

Native Requirements

  • There should be no dependency on the existing File or FileTransfer plugins.
  • The native cached file path should be uniquely identifiable with the id parameter. This will allow the Content Sync plugin to lookup the file path at a later time using the id parameter.
  • The first version of the plugin assumes that all cached content is downloaded as a compressed ZIP. The native implementation must properly extract content and clean up any temporary files, such as the downloaded zip.
  • The locally compiled Cordova web assets should be copied to the cached content. This includes cordova.js, cordova_plugins.js, and plugins/**/*.
  • Multiple syncs should be supported at the same time.

Running Tests ( static tests against source code )

npm test

Emulator Testing

The emulator tests use cordova-paramedic and the cordova-plugin-test-framework. To run them you will need cordova-paramedic installed:

npm install -g cordova-paramedic

Some of the tests require a simple HTTP server to host .zip payloads:

./tests/scripts/start-server.sh

Run the tests:

// From the root of this repo
// test ios :
cordova-paramedic --platform ios --plugin .

// test android :
cordova-paramedic --platform android --plugin .

Once complete, the simple HTTP server can be stopped:

./tests/scripts/stop-server.sh

Contributing

Editor Config

The project uses .editorconfig to define the coding style of each file. We recommend that you install the Editor Config extension for your preferred IDE.

JSHint

The project uses .jshint to define the JavaScript coding conventions. Most editors now have a JSHint add-on to provide on-save or on-edit linting.

Install JSHint for vim

  1. Install jshint.
  2. Install jshint.vim.

Install JSHint for Sublime

  1. Install Package Control
  2. Restart Sublime
  3. Type CMD+SHIFT+P
  4. Type Install Package
  5. Type JSHint Gutter
  6. Sublime -> Preferences -> Package Settings -> JSHint Gutter
  7. Set lint_on_load and lint_on_save to true

More Repositories

1

phonegap-start

PhoneGap Hello World app
JavaScript
3,427
star
2

phonegap-app-developer

PhoneGap Developer App
JavaScript
1,999
star
3

phonegap-plugin-push

Register and receive push notifications
Java
1,941
star
4

phonegap-plugin-barcodescanner

cross-platform BarcodeScanner for Cordova / PhoneGap
Objective-C++
1,271
star
5

phonegap-app-desktop

PhoneGap Desktop App
JavaScript
841
star
6

phonegap-cli

PhoneGap and PhoneGap/Build command-line interface
JavaScript
492
star
7

phonegap-plugin-fast-canvas

Fast, 2D, mostly-HTML5-canvas-compatible rendering surface for Android.
C
195
star
8

phonegap-mobile-accessibility

PhoneGap plugin to expose mobile accessibility APIs.
JavaScript
145
star
9

phonegap-docs

PhoneGap Documentation
Pug
121
star
10

build

This is the public repository for PhoneGap Build source and bug tracking
92
star
11

phonegap-community

PhoneGap Community Release Notes
81
star
12

phonegap-template-react-hot-loader

PhoneGap Template using React, ES2015, Webpack, and hot module reloading
JavaScript
79
star
13

phonegap-template-hello-world

PhoneGap Hello World app
JavaScript
72
star
14

node-phonegap-build-api

Node.js REST Client for the PhoneGap Build API
JavaScript
62
star
15

connect-phonegap

Stream a PhoneGap app to any device.
JavaScript
61
star
16

phonegap-app-star-track

The PhoneGap media example app
CSS
54
star
17

phonegap-plugin-pwa

A plugin to provide progressive web app API's
45
star
18

phonegap-template-framework7

A starter template for creating a hybrid app with Framework7.
CSS
43
star
19

phonegap-app-anyconference

AnyConference example app
JavaScript
41
star
20

phonegap-2-style-3

PhoneGap 3.0 project that includes all of the plugins by default
JavaScript
39
star
21

phonegap-plugin-local-notification

An implementation of the Web Notifications API for end-user notifications.
Objective-C
38
star
22

phonegap-webview-ios

Native iOS + PhoneGap Template
Ruby
36
star
23

phonegap-plugin-media-stream

JavaScript
34
star
24

phonegap-template-vue-f7-blank

A blank PhoneGap template using Vue.js and Framework7
JavaScript
32
star
25

phonegap-template-vue-f7-tabs

A TabBar PhoneGap template using Vue.js and Framework7
JavaScript
31
star
26

phonegap-symbian.wrt

Symbian WRT implementation of the PhoneGap API
JavaScript
28
star
27

emulate.phonegap.com

Ripple emulation for PhoneGap's JavaScript environment
JavaScript
28
star
28

node-phonegap-build

PhoneGap Build node module to login, create, and build apps.
JavaScript
25
star
29

phonegap-sample-hybrid-ios

A sample application showing a hybrid application with both native and webview components and communication.
Objective-C
24
star
30

phonegap-sample-hybrid-android

Java
22
star
31

phonegap-app-fast-canvas

Example game using phonegap-fast-canvas-plugin
JavaScript
20
star
32

phonegap.github.io

Main pages for phonegap.com hosted on GitHub pages.
JavaScript
20
star
33

phonegap-plugin-multiview

Spawn multiple cordova enabled webviews in one app
JavaScript
18
star
34

phonegap-template-vue-f7-split-panel

A Split View PhoneGap template using Vue.js and Framework7 that degrades to a Panel View on smaller devices
JavaScript
16
star
35

phonegap-plugin-media-recorder

Objective-C
14
star
36

phonegap-plugin-image-capture

JavaScript
11
star
37

build-bot-model

PhoneGap Build Bot 3D model
11
star
38

phonegap-plugin-template

This repo is a template for starting a new plugin.
JavaScript
10
star
39

phonegap-template-push

A sample application for getting started with push notifications
JavaScript
8
star
40

app

The app showcase found at http://phonegap.github.io/app
HTML
7
star
41

app.phonegap.com

Micro-site for the PhoneGap Developer App and PhoneGap Desktop app.
JavaScript
7
star
42

phonegap-app-anyconference-pgday

AnyConference app for PhoneGap Day
JavaScript
7
star
43

phonegap-app-augmented-reality

CSS
6
star
44

phonegap-template-vue-f7-todos-pwa

A Todo's app template with PWA support using Vue.js and Framework7
JavaScript
6
star
45

adobe-creative-sdk-foundation

OBSOLETE - see README
4
star
46

phonegap-roadmap

Upcoming milestones and projects for PhoneGap
4
star
47

native-plugin-sync-demo

Demo of native-plugin-sync
CSS
4
star
48

phonegap-template-blank

A blank PhoneGap app.
HTML
3
star
49

book

http://phonegap.com/book/
HTML
3
star
50

workshop-plugins

Workshop / lab content covering end to end plugin development
3
star
51

phonegap-plugin-multidex

Enable multidex in a Apache Cordova/PhoneGap application
3
star
52

tool

A collection of 3rd party tools for PhoneGap development.
HTML
3
star
53

cordova-android

DO NOT DELETE - contains the thread-safe bridge code
Java
3
star
54

phonegap-app-stockpile

n. - A storage pile accumulated for future use
JavaScript
3
star
55

phonegap-day

The website for PhoneGap Day
JavaScript
2
star
56

phonegap-app-todo

JavaScript
2
star
57

linting-and-editorconfig

Reference repo for eslint (and other linters) as well as editorconfig settings and info
JavaScript
1
star
58

topcoat-preact

React components implementing Topcoat components
JavaScript
1
star
59

phonegap-template-webvr

PhoneGap template for the WebVR Boilerplate at https://github.com/borismus/webvr-boilerplate
JavaScript
1
star
60

dotfiles

A repo to collect the dot files we use consistently in our other repositories
1
star
61

phonegap-plugin-developer-mode

Utility functions for the Phonegap Developer app
JavaScript
1
star