Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor magicFileType to reduce cyclic dependencies #480

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
450 changes: 450 additions & 0 deletions src/core/lib/DetectFileType.mjs

Large diffs are not rendered by default.

447 changes: 2 additions & 445 deletions src/core/lib/Magic.mjs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/core/operations/DetectFileType.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import Operation from "../Operation";
import Magic from "../lib/Magic";
import detectFileType from "../lib/DetectFileType";

/**
* Detect File Type operation
Expand Down Expand Up @@ -34,7 +34,7 @@ class DetectFileType extends Operation {
*/
run(input, args) {
const data = new Uint8Array(input),
type = Magic.magicFileType(data);
type = detectFileType(data);

if (!type) {
return "Unknown file type. Have you tried checking the entropy of this data to determine whether it might be encrypted or compressed?";
Expand Down
6 changes: 3 additions & 3 deletions src/core/operations/GenerateQRCode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* @license Apache-2.0
*/

import detectFileType from "../lib/DetectFileType";
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
import qr from "qr-image";
import { toBase64 } from "../lib/Base64";
import Magic from "../lib/Magic";
import Utils from "../Utils";
import { toBase64 } from "../lib/Base64";

/**
* Generate QR Code operation
Expand Down Expand Up @@ -100,7 +100,7 @@ class GenerateQRCode extends Operation {

if (format === "PNG") {
let dataURI = "data:";
const type = Magic.magicFileType(data);
const type = detectFileType(data);
if (type && type.mime.indexOf("image") === 0){
dataURI += type.mime + ";";
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/core/operations/ParseQRCode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* @license Apache-2.0
*/

import detectFileType from "../lib/DetectFileType";
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
import Magic from "../lib/Magic";
import jsqr from "jsqr";
import jimp from "jimp";

Expand Down Expand Up @@ -42,7 +42,7 @@ class ParseQRCode extends Operation {
* @returns {string}
*/
async run(input, args) {
const type = Magic.magicFileType(input);
const type = detectFileType(input);
const [normalise] = args;

// Make sure that the input is an image
Expand Down
6 changes: 3 additions & 3 deletions src/core/operations/PlayMedia.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

import { fromBase64, toBase64 } from "../lib/Base64";
import { fromHex } from "../lib/Hex";
import detectFileType from "../lib/DetectFileType";
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
import Utils from "../Utils";
import Magic from "../lib/Magic";

/**
* PlayMedia operation
Expand Down Expand Up @@ -66,7 +66,7 @@ class PlayMedia extends Operation {


// Determine file type
const type = Magic.magicFileType(input);
const type = detectFileType(input);
if (!(type && /^audio|video/.test(type.mime))) {
throw new OperationError("Invalid or unrecognised file type");
}
Expand All @@ -84,7 +84,7 @@ class PlayMedia extends Operation {
async present(data) {
if (!data.length) return "";

const type = Magic.magicFileType(data);
const type = detectFileType(data);
const matches = /^audio|video/.exec(type.mime);
if (!matches) {
throw new OperationError("Invalid file type");
Expand Down
6 changes: 3 additions & 3 deletions src/core/operations/RenderImage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { fromHex } from "../lib/Hex";
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
import Utils from "../Utils";
import Magic from "../lib/Magic";
import detectFileType from "../lib/DetectFileType";

/**
* Render Image operation
Expand Down Expand Up @@ -72,7 +72,7 @@ class RenderImage extends Operation {
}

// Determine file type
const type = Magic.magicFileType(input);
const type = detectFileType(input);
if (!(type && type.mime.indexOf("image") === 0)) {
throw new OperationError("Invalid file type");
}
Expand All @@ -92,7 +92,7 @@ class RenderImage extends Operation {
let dataURI = "data:";

// Determine file type
const type = Magic.magicFileType(data);
const type = detectFileType(data);
if (type && type.mime.indexOf("image") === 0) {
dataURI += type.mime + ";";
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/core/operations/ScanForEmbeddedFiles.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Operation from "../Operation";
import Utils from "../Utils";
import Magic from "../lib/Magic";
import detectFileType from "../lib/DetectFileType";

/**
* Scan for Embedded Files operation
Expand Down Expand Up @@ -49,7 +49,7 @@ class ScanForEmbeddedFiles extends Operation {
data = new Uint8Array(input);

for (let i = 0; i < data.length; i++) {
type = Magic.magicFileType(data.slice(i));
type = detectFileType(data.slice(i));
if (type) {
if (ignoreCommon && commonExts.indexOf(type.ext) > -1) {
numCommonFound++;
Expand Down
4 changes: 2 additions & 2 deletions src/core/operations/SplitColourChannels.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
import Utils from "../Utils";
import Magic from "../lib/Magic";
import detectFileType from "../lib/DetectFileType";

import jimp from "jimp";

Expand Down Expand Up @@ -38,7 +38,7 @@ class SplitColourChannels extends Operation {
* @returns {List<File>}
*/
async run(input, args) {
const type = Magic.magicFileType(input);
const type = detectFileType(input);
// Make sure that the input is an image
if (type && type.mime.indexOf("image") === 0) {
const parsedImage = await jimp.read(Buffer.from(input));
Expand Down