summaryrefslogtreecommitdiffstats
path: root/afb-client/bower_components/jszip/documentation/api_jszip/generate.md
blob: 7b00f97ed842c61754e52b15dd66a5aed1d4eaf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
---
title: "generate(options)"
layout: default
section: api
---

__Description__ : Generates the complete zip file.

__Arguments__

name                | type    | default | description
--------------------|---------|---------|------------
options             | object  |         | the options to generate the zip file :
options.base64      | boolean | false   | **deprecated**, use `type` instead. If `type` is not used, set to `false` to get the result as a raw byte string, `true` to encode it as base64.
options.compression | string  | `STORE` (no compression) | the default file compression method to use. Available methods are `STORE` and `DEFLATE`. You can also provide your own compression method.
options.compressionOptions | object | `null` | the options to use when compressing the file, see below.
options.type        | string  | `base64` | The type of zip to return, see below for the other types.
options.comment     | string  |          | The comment to use for the zip file.
options.mimeType    | string  | `application/zip` | mime-type for the generated file. Useful when you need to generate a file with a different extension, ie: ".ods".
options.platform    | string  | `DOS`    | The platform to use when generating the zip file.

Possible values for `type` :

* `base64` (default) : the result will be a string, the binary in a base64 form.
* `string` : the result will be a string in "binary" form, using 1 byte per char (2 bytes).
* `uint8array` : the result will be a Uint8Array containing the zip. This requires a compatible browser.
* `arraybuffer` : the result will be a ArrayBuffer containing the zip. This requires a compatible browser.
* `blob` : the result will be a Blob containing the zip. This requires a compatible browser.
* `nodebuffer` : the result will be a nodejs Buffer containing the zip. This requires nodejs.

Note : when using type = "uint8array", "arraybuffer" or "blob", be sure to
check if the browser supports it (you can use [`JSZip.support`]({{site.baseurl}}/documentation/api_jszip/support.html)).

The `compressionOptions` parameter depends on the compression type. With
`STORE` (no compression), this parameter is ignored. With `DEFLATE`, you can
give the compression level with `compressionOptions : {level:6}` (or any level
between 1 (best speed) and 9 (best compression)).

Note : if the entry is *already* compressed (coming from a compressed zip file),
calling `generate()` with a different compression level won't update the entry.
The reason is simple : JSZip doesn't know how compressed the content was and
how to match the compression level with the implementation we use.

Note for the `comment` option : the zip format has no flag or field to give the
encoding of this field and JSZip will use UTF-8. With non ASCII characters you
might get encoding issues if the file archiver doesn't use UTF-8 to decode the
comment.

If not set, JSZip will use the field `comment` on its `options`.

Possible values for `platform` : `DOS` and `UNIX`. It also accepts nodejs
`process.platform` values.
When using `DOS`, the attribute `dosPermissions` of each file is used.
When using `UNIX`, the attribute `unixPermissions` of each file is used.

If you set the platform value on nodejs, be sure to use `process.platform`.
`fs.stats` returns a non executable mode for folders on windows, if you
force the platform to `UNIX` the generated zip file will have a strange
behavior on UNIX platforms.

__Returns__ : The generated zip file.

__Throws__ : An exception if the asked `type` is not available in the browser,
see [JSZip.support]({{site.baseurl}}/documentation/api_jszip/support.html).

<!-- __Complexity__ : TODO : worst case, with/out compression, etc -->

__Example__

```js
var content = zip.generate({type:"blob"});
// see FileSaver.js
saveAs(content, "hello.zip");
```

```js
var content = zip.generate({type:"base64"});
location.href="data:application/zip;base64,"+content;
```

```js
var content = zip.generate({type:"nodebuffer"});
require("fs").writeFile("hello.zip", content, function(err){/*...*/});
```

```js
// on nodejs
zip.file(pathname, content, {
    date: stat.mtime,
    unixPermissions: stat.mode
});

// ...

zip.generate({
    type: 'nodebuffer',
    platform: process.platform
});
```

```js
//This example will Generate a Open Document Spreasheet, with the correct mime type
var zip = new JSZip();
zip.file("mimetype", "application/vnd.oasis.opendocument.spreadsheet");
var conf2 = zip.folder("Configurations2");
conf2.folder("acceleator");
conf2.folder("images");
conf2.folder("popupmenu");
conf2.folder("statusbar");
conf2.folder("floater");
conf2.folder("menubar");
conf2.folder("progressbar");
conf2.folder("toolbar");

var manifest = "<..."; //xml containing manifest.xml 
var styles = "<..."; //xml containing styles.xml
var settings = "<..."; //xml containing settings.xml
var meta = "<..."; //xml containing meta.xml
var content = "<..."; //xml containing content.xml

var metaInf = zip.folder("META-INF");
metaInf.file("manifest.xml", manifest);
zip.file("styles.xml", styles);
zip.file("settings.xml", settings); 
zip.file("meta.xml", meta);
zip.file("content.xml", content);

//Generate the file
var odsFile = zip.generate({type: "blob", mimeType: "application/ods", compression: "DEFLATE"});

var url = window.URL.createObjectURL(odsFile);
var link = document.getElementById("link"); //I suppose you'll have a link with this id :)
link.download = "testjs.ods";
link.href = url;


```
ce; int device; int subdev; const char*ctlName; ctlRequestT ctlRequest; snd_ctl_elem_id_t *elemId; if ((revents & EPOLLHUP) != 0) { AFB_NOTICE("SndCtl hanghup [car disconnected]"); goto ExitOnSucess; } if ((revents & EPOLLIN) != 0) { // initialise event structure on stack snd_ctl_event_alloca(&eventId); snd_ctl_elem_id_alloca(&elemId); err = snd_ctl_read(evtHandle->ctlDev, eventId); if (err < 0) goto OnErrorExit; // we only process sndctrl element if (snd_ctl_event_get_type(eventId) != SND_CTL_EVENT_ELEM) goto ExitOnSucess; // we only process value changed events mask = snd_ctl_event_elem_get_mask(eventId); if (!(mask & SND_CTL_EVENT_MASK_VALUE)) goto ExitOnSucess; snd_ctl_event_elem_get_id(eventId, elemId); err = alsaGetSingleCtl(evtHandle->ctlDev, elemId, &ctlRequest, evtHandle->mode); if (err) goto OnErrorExit; // If CTL as a value use it as container for response if (ctlRequest.valuesJ) ctlEventJ = ctlRequest.valuesJ; else { ctlEventJ = json_object_new_object(); ctlNumid = snd_ctl_event_elem_get_numid(eventId); json_object_object_add(ctlEventJ, "id", json_object_new_int(ctlNumid)); } if (evtHandle->mode >= QUERY_COMPACT) { ctlName = snd_ctl_event_elem_get_name(eventId); json_object_object_add(ctlEventJ, "name", json_object_new_string(ctlName)); } if (evtHandle->mode >= QUERY_VERBOSE) { iface = snd_ctl_event_elem_get_interface(eventId); device = snd_ctl_event_elem_get_device(eventId); subdev = snd_ctl_event_elem_get_subdevice(eventId); json_object_object_add(ctlEventJ, "ifc", json_object_new_int(iface)); json_object_object_add(ctlEventJ, "dev", json_object_new_int(device)); json_object_object_add(ctlEventJ, "sub", json_object_new_int(subdev)); } AFB_DEBUG("sndCtlEventCB=%s", json_object_get_string(ctlEventJ)); afb_event_push(evtHandle->afbevt, ctlEventJ); } ExitOnSucess: return 0; OnErrorExit: AFB_WARNING("sndCtlEventCB: ignored unsupported event type"); return (0); } // Subscribe to every Alsa CtlEvent send by a given board PUBLIC void alsaEvtSubcribe(afb_req request) { static sndHandleT sndHandles[MAX_SND_CARD]; evtHandleT *evtHandle = NULL; snd_ctl_t *ctlDev = NULL; int err, idx, cardId, idxFree = -1; snd_ctl_card_info_t *cardinfo; queryValuesT queryValues; json_object *queryJ = alsaCheckQuery(request, &queryValues); if (!queryJ) goto OnErrorExit; // open control interface for devid err = snd_ctl_open(&ctlDev, queryValues.devid, SND_CTL_READONLY); if (err < 0) { afb_req_fail_f(request, "devid-unknown", "SndCard devid=%s Not Found err=%s", queryValues.devid, snd_strerror(err)); goto OnErrorExit; } snd_ctl_card_info_alloca(&cardinfo); if ((err = snd_ctl_card_info(ctlDev, cardinfo)) < 0) { afb_req_fail_f(request, "devid-invalid", "SndCard devid=%s Not Found err=%s", queryValues.devid, snd_strerror(err)); goto OnErrorExit; } cardId = snd_ctl_card_info_get_card(cardinfo); // search for an existing subscription and mark 1st free slot for (idx = 0; idx < MAX_SND_CARD; idx++) { if (sndHandles[idx].ucount > 0 && cardId == sndHandles[idx].cardId) { evtHandle = sndHandles[idx].evtHandle; break; } else if (idxFree == -1) idxFree = idx; }; // if not subscription exist for the event let's create one if (idx == MAX_SND_CARD) { // reach MAX_SND_CARD event registration if (idxFree == -1) { afb_req_fail_f(request, "register-toomany", "Cannot register new event Maxcard==%d", idx); goto OnErrorExit; } evtHandle = malloc(sizeof (evtHandleT)); evtHandle->ctlDev = ctlDev; evtHandle->mode = queryValues.mode; sndHandles[idxFree].ucount = 0; sndHandles[idxFree].cardId = cardId; sndHandles[idxFree].evtHandle = evtHandle; // subscribe for sndctl events attached to devid err = snd_ctl_subscribe_events(evtHandle->ctlDev, 1); if (err < 0) { afb_req_fail_f(request, "subscribe-fail", "Cannot subscribe events from devid=%s err=%d", queryValues.devid, err); goto OnErrorExit; } // get pollfd attach to this sound board snd_ctl_poll_descriptors(evtHandle->ctlDev, &evtHandle->pfds, 1); // register sound event to binder main loop err = sd_event_add_io(afb_daemon_get_event_loop(), &evtHandle->src, evtHandle->pfds.fd, EPOLLIN, sndCtlEventCB, evtHandle); if (err < 0) { afb_req_fail_f(request, "register-mainloop", "Cannot hook events to mainloop devid=%s err=%d", queryValues.devid, err); goto OnErrorExit; } // create binder event attached to devid name evtHandle->afbevt = afb_daemon_make_event(queryValues.devid); if (!afb_event_is_valid(evtHandle->afbevt)) { afb_req_fail_f(request, "register-event", "Cannot register new binder event name=%s", queryValues.devid); goto OnErrorExit; } // everything looks OK let's move forward idx = idxFree; } // subscribe to binder event err = afb_req_subscribe(request, evtHandle->afbevt); if (err != 0) { afb_req_fail_f(request, "register-eventname", "Cannot subscribe binder event name=%s [invalid channel]", queryValues.devid); goto OnErrorExit; } // increase usage count and return success sndHandles[idx].ucount++; snd_ctl_close(ctlDev); afb_req_success(request, NULL, NULL); // use OnErrorExit OnErrorExit: if (ctlDev) snd_ctl_close(ctlDev); return; } // Subscribe to every Alsa CtlEvent send by a given board STATIC json_object *alsaProbeCardId(afb_req request) { char devid [10]; const char *ctlName, *shortname, *longname; int card, err, index, idx; json_object *responseJ; snd_ctl_t *ctlDev; snd_ctl_card_info_t *cardinfo; const char *sndname = afb_req_value(request, "sndname"); if (sndname == NULL) { afb_req_fail_f(request, "argument-missing", "sndname=SndCardName missing"); goto OnErrorExit; } // loop on potential card number snd_ctl_card_info_alloca(&cardinfo); for (card = 0; card < MAX_SND_CARD; card++) { // build card devid and probe it snprintf(devid, sizeof (devid), "hw:%i", card); // open control interface for devid err = snd_ctl_open(&ctlDev, devid, SND_CTL_READONLY); if (err < 0) continue; // extract sound card information snd_ctl_card_info(ctlDev, cardinfo); index = snd_ctl_card_info_get_card(cardinfo); ctlName = snd_ctl_card_info_get_id(cardinfo); shortname = snd_ctl_card_info_get_name(cardinfo); longname = snd_ctl_card_info_get_longname(cardinfo); snd_ctl_close(ctlDev); // check if short|long name match if (!strcmp(sndname, ctlName)) break; if (!strcmp(sndname, shortname)) break; if (!strcmp(sndname, longname)) break; } if (card == MAX_SND_CARD) { afb_req_fail_f(request, "ctlDev-notfound", "Fail to find card with name=%s", sndname); goto OnErrorExit; } // proxy ctlevent as a binder event responseJ = json_object_new_object(); json_object_object_add(responseJ, "index", json_object_new_int(index)); json_object_object_add(responseJ, "devid", json_object_new_string(devid)); json_object_object_add(responseJ, "shortname", json_object_new_string(shortname)); json_object_object_add(responseJ, "longname", json_object_new_string(longname)); // search for a HAL binder card mapping name to api prefix for (idx = 0; (idx < MAX_SND_CARD && cardRegistry[idx]); idx++) { if (!strcmp(cardRegistry[idx]->shortname, shortname)) { json_object_object_add(responseJ, "halapi", json_object_new_string(cardRegistry[idx]->apiprefix)); break; } } return responseJ; OnErrorExit: return NULL; } // Make alsaProbeCardId compatible with AFB request PUBLIC void alsaGetCardId(afb_req request) { json_object *responseJ = alsaProbeCardId(request); if (responseJ) afb_req_success(request, responseJ, NULL); } // Return list of active resgistrated HAL with corresponding sndcard PUBLIC void alsaActiveHal(afb_req request) { json_object *responseJ = json_object_new_array(); for (int idx = 0; idx < MAX_SND_CARD; idx++) { if (!cardRegistry[idx]) break; json_object *haldevJ = json_object_new_object(); json_object_object_add(haldevJ, "api", json_object_new_string(cardRegistry[idx]->apiprefix)); if (cardRegistry[idx]->devid) json_object_object_add(haldevJ, "devid", json_object_new_string(cardRegistry[idx]->devid)); if (cardRegistry[idx]->shortname)json_object_object_add(haldevJ, "shortname", json_object_new_string(cardRegistry[idx]->shortname)); if (cardRegistry[idx]->longname) json_object_object_add(haldevJ, "longname", json_object_new_string(cardRegistry[idx]->longname)); json_object_array_add(responseJ, haldevJ); } afb_req_success(request, responseJ, NULL); } // Register loaded HAL with board Name and API prefix PUBLIC void alsaRegisterHal(afb_req request) { static int index = 0; json_object *responseJ; const char *shortname, *apiPrefix; apiPrefix = afb_req_value(request, "prefix"); if (apiPrefix == NULL) { afb_req_fail_f(request, "argument-missing", "prefix=BindingApiPrefix missing"); goto OnErrorExit; } shortname = afb_req_value(request, "sndname"); if (shortname == NULL) { afb_req_fail_f(request, "argument-missing", "sndname=SndCardName missing"); goto OnErrorExit; } if (index == MAX_SND_CARD) { afb_req_fail_f(request, "alsahal-toomany", "Fail to register sndname=[%s]", shortname); goto OnErrorExit; } // alsaGetCardId should be check to register only valid card responseJ = alsaProbeCardId(request); if (responseJ) { json_object *tmpJ; int done; cardRegistry[index] = malloc(sizeof (cardRegistry)); cardRegistry[index]->apiprefix = strdup(apiPrefix); cardRegistry[index]->shortname = strdup(shortname); done = json_object_object_get_ex(responseJ, "devid", &tmpJ); if (done) cardRegistry[index]->devid = strdup(json_object_get_string(tmpJ)); else cardRegistry[index]->devid = NULL; done = json_object_object_get_ex(responseJ, "longname", &tmpJ); if (done) cardRegistry[index]->longname = strdup(json_object_get_string(tmpJ)); else cardRegistry[index]->longname = NULL; // make sure register close with a null value index++; cardRegistry[index] = NULL; afb_req_success(request, responseJ, NULL); } // If OK return sound card Alsa ID+Info return; OnErrorExit: return; }