aboutsummaryrefslogtreecommitdiff
path: root/webAO/utils
diff options
context:
space:
mode:
Diffstat (limited to 'webAO/utils')
-rw-r--r--webAO/utils/calculateApngLength.js29
-rw-r--r--webAO/utils/fileExists.js22
-rw-r--r--webAO/utils/getAnimLength.js2
3 files changed, 31 insertions, 22 deletions
diff --git a/webAO/utils/calculateApngLength.js b/webAO/utils/calculateApngLength.js
index bc6b3fa..932f581 100644
--- a/webAO/utils/calculateApngLength.js
+++ b/webAO/utils/calculateApngLength.js
@@ -1,26 +1,25 @@
- /**
+/**
* Adds up the chunk delays to find out how long a APNG is
* @param {data} apngFile the APNG data
*/
const calculateApngLength = (apngFile) => {
const d = new Uint8Array(apngFile);
- // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chunk
- let duration = 0;
- for (var i = 0; i < d.length; i++) {
- // Find fcTL header (66 63 54 4C)
- if (d[i] === 0x66
+ // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chunk
+ let duration = 0;
+ for (let i = 0; i < d.length; i++) {
+ // Find fcTL header (66 63 54 4C)
+ if (d[i] === 0x66
&& d[i + 1] === 0x63
&& d[i + 2] === 0x54
&& d[i + 3] === 0x4C) {
- console.log("found apng header");
- // numerator and denominator
- let delay = ((d[i + 21] / d[i + 23]) * 1000)
+ // numerator and denominator
+ const delay = ((d[i + 21] / d[i + 23]) * 1000);
- // minimum is 100ms
- duration += delay < 100 ? 100 : delay;
- }
- }
- console.debug(duration);
- return duration * 10;
+ // minimum is 100ms
+ duration += delay < 100 ? 100 : delay;
+ }
+ }
+ console.debug(duration);
+ return duration * 10;
};
export default calculateApngLength;
diff --git a/webAO/utils/fileExists.js b/webAO/utils/fileExists.js
index 6d32a1e..7978cbc 100644
--- a/webAO/utils/fileExists.js
+++ b/webAO/utils/fileExists.js
@@ -1,8 +1,18 @@
-const fileExists = async (url) => {
+const fileExists = async (url) => new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
- xhr.open('HEAD', url, false);
- xhr.send();
-
- return xhr.status === 200;
-};
+ xhr.open('HEAD', url);
+ xhr.onload = function (e) {
+ if (xhr.readyState === 4) {
+ if (xhr.status === 200) {
+ resolve(true);
+ } else {
+ resolve(false);
+ }
+ }
+ };
+ xhr.onerror = function (e) {
+ resolve(false);
+ };
+ xhr.send(null);
+});
export default fileExists;
diff --git a/webAO/utils/getAnimLength.js b/webAO/utils/getAnimLength.js
index 1441548..aa303cf 100644
--- a/webAO/utils/getAnimLength.js
+++ b/webAO/utils/getAnimLength.js
@@ -1,6 +1,6 @@
import calculatorHandler from './calculatorHandler';
import fileExists from './fileExists.js';
-import {requestBuffer} from '../services/request.js';
+import { requestBuffer } from '../services/request.js';
/**
* Gets animation length. If the animation cannot be found, it will
* silently fail and return 0 instead.