Skip to content

Commit e794816

Browse files
committed
Added Base64 and HTML Data URI Outputs.
1 parent c400fff commit e794816

2 files changed

Lines changed: 166 additions & 6 deletions

File tree

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ What is done
2020
* Python Packer
2121
* JPAK 1.0 Specification file
2222
* Load progress event
23+
* Get file as Base64
24+
* Get file as HTML Data URI (for hide from Network Requests)
2325

2426
TODO
2527
========
@@ -105,6 +107,65 @@ Arguments:
105107
* `path` -> The path you want to list
106108
* `returns` -> Returns an object `{ "dirs" : [ arrayofdirs ], "files" : [ arrayoffiles ], "error" : "An error message, if happens" }`
107109

110+
```javascript
111+
jpakloader.GetFile(path, type)
112+
```
113+
114+
Return a blob of the file
115+
116+
Arguments:
117+
* `path` -> The file path to get
118+
* `type` -> The mime type. Defaults to **application/octet-binary**
119+
* `returns` -> Returns an blob
120+
121+
```javascript
122+
jpakloader.GetFileURL(path, type)
123+
```
124+
125+
Return a Blob URL to embed as resource.
126+
127+
Arguments:
128+
* `path` -> The file path to get
129+
* `type` -> The mime type. Defaults to **application/octet-binary**
130+
* `returns` -> Returns an string containing the Blob Data URL
131+
132+
```javascript
133+
jpakloader.GetFileArrayBuffer(path, type)
134+
```
135+
136+
Return an ArrayBuffer with contents of the file
137+
138+
Arguments:
139+
* `path` -> The file path to get
140+
* `type` -> The mime type. Not used directly for this function, but for cache. Defaults to **application/octet-binary**
141+
* `returns` -> Returns an ArrayBuffer with contents of the file
142+
143+
144+
145+
```javascript
146+
jpakloader.GetBase64File(path, type)
147+
```
148+
149+
Return a Base64 Encoded Data from the File
150+
151+
Arguments:
152+
* `path` -> The file path to get
153+
* `type` -> The mime type. Not used directly for this function, but for cache. Defaults to **application/octet-binary**
154+
* `returns` -> Returns an string of Base64 Encoded data.
155+
156+
```javascript
157+
jpakloader.GetHTMLDataURIFile(path, type)
158+
```
159+
160+
Return a HTML Data URI to embed as resource.
161+
This is useful if you want to hide the image load process inside the script, as the browser doesn`t make network requests.
162+
163+
Arguments:
164+
* `path` -> The file path to get
165+
* `type` -> The mime type. Defaults to **application/octet-binary**
166+
* `encoding` -> The Encoding of the file. *Optional*
167+
* `returns` -> Returns an string of HTML Data URI in format: `data:[<MIME-type>][;charset=<encoding>][;base64],<data>`
168+
108169
Events:
109170
========
110171

js/jpak.js

Lines changed: 105 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,84 @@ Array.prototype.clean = function(deleteValue) {
3737
return this;
3838
};
3939

40-
/* Convert Unsigned Int 8 Array Buffer to String */
41-
function u8as(d) {
40+
/* Start of JPAK Class Stuff */
41+
var JPAK = function() {};
42+
43+
// Auxiliary functions
44+
// Convert Unsigned Int 8 Array Buffer to String
45+
JPAK.Uint8ArrayToString = function(uintArray) {
4246
var o = "";
43-
for(var i=0;i<d.byteLength;i++)
44-
o += String.fromCharCode(d[i]);
47+
for(var i=0;i<uintArray.byteLength;i++)
48+
o += String.fromCharCode(uintArray[i]);
4549
return o;
4650
}
4751

48-
/* Start of JPAK Class Stuff */
49-
var JPAK = function() {};
52+
var u8as = JPAK.Uint8ArrayToString; // Provided for compatibility
53+
54+
JPAK.Base64_Encoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
55+
56+
// Modified version from https://gist.github.com/jonleighton/958841
57+
JPAK.ArrayBufferToBase64 = function(arrayBuffer) {
58+
var base64 = ''
59+
60+
var bytes = new Uint8Array(arrayBuffer)
61+
var byteLength = bytes.byteLength
62+
var byteRemainder = byteLength % 3
63+
var mainLength = byteLength - byteRemainder
64+
65+
var a, b, c, d
66+
var chunk
67+
68+
// Main loop deals with bytes in chunks of 3
69+
for (var i = 0; i < mainLength; i = i + 3) {
70+
// Combine the three bytes into a single integer
71+
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]
72+
73+
// Use bitmasks to extract 6-bit segments from the triplet
74+
a = (chunk & 16515072) >> 18 // 16515072 = (2^6 - 1) << 18
75+
b = (chunk & 258048) >> 12 // 258048 = (2^6 - 1) << 12
76+
c = (chunk & 4032) >> 6 // 4032 = (2^6 - 1) << 6
77+
d = chunk & 63 // 63 = 2^6 - 1
78+
79+
// Convert the raw binary segments to the appropriate ASCII encoding
80+
base64 += JPAK.Base64_Encoding[a] + JPAK.Base64_Encoding[b] + JPAK.Base64_Encoding[c] + JPAK.Base64_Encoding[d]
81+
}
82+
83+
// Deal with the remaining bytes and padding
84+
if (byteRemainder == 1) {
85+
chunk = bytes[mainLength]
86+
87+
a = (chunk & 252) >> 2 // 252 = (2^6 - 1) << 2
88+
89+
// Set the 4 least significant bits to zero
90+
b = (chunk & 3) << 4 // 3 = 2^2 - 1
91+
92+
base64 += JPAK.Base64_Encoding[a] + JPAK.Base64_Encoding[b] + '=='
93+
} else if (byteRemainder == 2) {
94+
chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]
95+
96+
a = (chunk & 64512) >> 10 // 64512 = (2^6 - 1) << 10
97+
b = (chunk & 1008) >> 4 // 1008 = (2^6 - 1) << 4
98+
99+
// Set the 2 least significant bits to zero
100+
c = (chunk & 15) << 2 // 15 = 2^4 - 1
101+
102+
base64 += JPAK.Base64_Encoding[a] + JPAK.Base64_Encoding[b] + JPAK.Base64_Encoding[c] + '='
103+
}
104+
105+
return base64
106+
};
50107

108+
109+
// JPAKLoader
51110
JPAK.jpakloader = function(parameters) {
52111
if(parameters !== undefined) {
53112
this.jpakfile = parameters.file;
54113
this.loadall = parameters.loadall || false; // TODO: Implement the fetch-on-need feature
55114
}
56115
};
57116

117+
// Variables
58118
JPAK.jpakloader.prototype.dataloaded = false; // Set to true, when file is loaded
59119
JPAK.jpakloader.prototype.filecache = []; // The cached files that we loaded
60120

@@ -207,6 +267,23 @@ JPAK.jpakloader.prototype.GetFileURL = function(path, type) {
207267
return cache.url;
208268
};
209269

270+
// Returns an arraybuffer with file content. It looks in the cache for already loaded files.
271+
JPAK.jpakloader.prototype.GetFileArrayBuffer = function(path, type) {
272+
var file = this.FindFileEntry(path);
273+
type = type || 'application/octet-binary';
274+
var cache = this.CacheLoad(path);
275+
276+
if(file != undefined && cache == undefined) {
277+
// Add it to file cache
278+
var blob = new Blob([new Uint8Array(this.jpakdata.slice(file.offset,file.offset+file.size)).buffer], { "type":type});
279+
var arraybuffer = this.jpakdata.slice(file.offset,file.offset+file.size);
280+
this.filecache.push({"path":path,"type":type,"blob":blob,"url":URL.createObjectURL(blob), "arraybuffer" : arraybuffer});
281+
return arraybuffer;
282+
}else if(cache != undefined)
283+
return cache.arraybuffer;
284+
285+
return undefined;
286+
};
210287
// Returns an arraybuffer with file content. It looks in the cache for already loaded files.
211288
JPAK.jpakloader.prototype.GetFileArrayBuffer = function(path, type) {
212289
var file = this.FindFileEntry(path);
@@ -225,4 +302,26 @@ JPAK.jpakloader.prototype.GetFileArrayBuffer = function(path, type) {
225302
return undefined;
226303
};
227304

305+
// Returns an Base64 Encoded File Content. It looks in the cache for already loaded files.
306+
JPAK.jpakloader.prototype.GetBase64File = function(path, type) {
307+
var filedata = this.GetFileArrayBuffer(path, type);
308+
if(filedata == undefined)
309+
return undefined;
310+
311+
return JPAK.ArrayBufferToBase64(filedata);
312+
};
228313

314+
// Returns an HTML Data URI with File Content. It looks in the cache for already loaded files.
315+
// Using HTML Data URI for Images, you can hide the load process from chrome Network Inspector
316+
// I didnt find any place that you can find DataURI File
317+
JPAK.jpakloader.prototype.GetHTMLDataURIFile = function(path, type, encoding) {
318+
var b64 = this.GetBase64File(path, type);
319+
// HTML Data URI Format: data:[<MIME-type>][;charset=<encoding>][;base64],<data>
320+
if(b64 === undefined)
321+
return undefined
322+
323+
if(encoding !== undefined)
324+
return "data:"+type+";charset="+encoding+";base64,"+b64;
325+
else
326+
return "data:"+type+";base64,"+b64;
327+
};

0 commit comments

Comments
 (0)