aboutsummaryrefslogtreecommitdiff
path: root/webAO/utils/getAnimLength.js
blob: ac673c27191dc6fb91a9e5f11a5462320d3a98f4 (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
/* eslint no-await-in-loop: "warn" */
/* eslint no-restricted-syntax: "off" */
/* TODO: use promises for this */

import calculatorHandler from "./calculatorHandler";
import fileExists from "./fileExists";
import { requestBuffer } from "../services/request";
/**
 * Gets animation length. If the animation cannot be found, it will
 * silently fail and return 0 instead.
 * @param {string} filename the animation file name
 */

const getAnimLength = async (url) => {
  const extensions = [".gif", ".webp", ".apng"];
  for (const extension of extensions) {
    const urlWithExtension = url + extension;
    const exists = await fileExists(urlWithExtension);
    if (exists) {
      const fileBuffer = await requestBuffer(urlWithExtension);
      const length = calculatorHandler[extension](fileBuffer);
      return length;
    }
  }
  return 0;
};
export default getAnimLength;