aboutsummaryrefslogtreecommitdiff
path: root/webAO/utils
diff options
context:
space:
mode:
Diffstat (limited to 'webAO/utils')
-rw-r--r--webAO/utils/calculateApngLength.js26
-rw-r--r--webAO/utils/calculatorHandler.js2
-rw-r--r--webAO/utils/getAnimLength.js4
3 files changed, 30 insertions, 2 deletions
diff --git a/webAO/utils/calculateApngLength.js b/webAO/utils/calculateApngLength.js
new file mode 100644
index 0000000..bc6b3fa
--- /dev/null
+++ b/webAO/utils/calculateApngLength.js
@@ -0,0 +1,26 @@
+ /**
+ * 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
+ && 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)
+
+ // minimum is 100ms
+ duration += delay < 100 ? 100 : delay;
+ }
+ }
+ console.debug(duration);
+ return duration * 10;
+};
+export default calculateApngLength;
diff --git a/webAO/utils/calculatorHandler.js b/webAO/utils/calculatorHandler.js
index 7686573..9072195 100644
--- a/webAO/utils/calculatorHandler.js
+++ b/webAO/utils/calculatorHandler.js
@@ -1,7 +1,9 @@
import calculateGifLength from './calculateGifLength';
import calculateWebpLength from './calculateWebpLength';
+import calculateApngLength from './calculateApngLength';
export default {
'.gif': calculateGifLength,
'.webp': calculateWebpLength,
+ '.apng': calculateApngLength,
};
diff --git a/webAO/utils/getAnimLength.js b/webAO/utils/getAnimLength.js
index 5b24e84..1441548 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.
@@ -8,7 +8,7 @@ import requestBuffer from '../services/request.js';
*/
const getAnimLength = async (url) => {
- const extensions = ['.gif', '.webp'];
+ const extensions = ['.gif', '.webp', '.apng'];
for (const extension of extensions) {
const urlWithExtension = url + extension;
const exists = await fileExists(urlWithExtension);