|
|
|
@ -10,8 +10,10 @@ import { promises, existsSync, lstatSync, readFileSync, readdirSync } from 'fs';
|
|
|
|
|
import { lookup as _getMimeType } from 'mime-types';
|
|
|
|
|
import { extname, join, parse } from 'path';
|
|
|
|
|
import { escapeRegExp } from 'lodash';
|
|
|
|
|
import { get } from '@vizality/http';
|
|
|
|
|
import imageSize from 'image-size';
|
|
|
|
|
import { promisify } from 'util';
|
|
|
|
|
|
|
|
|
|
import { log, warn, error } from './Logger';
|
|
|
|
|
import { Directories } from '../constants';
|
|
|
|
|
import { isString } from './String';
|
|
|
|
@ -25,52 +27,64 @@ const _log = (labels, ...message) => log({ labels, message });
|
|
|
|
|
const _warn = (labels, ...message) => warn({ labels, message });
|
|
|
|
|
const _error = (labels, ...message) => error({ labels, message });
|
|
|
|
|
|
|
|
|
|
export const getCaller = () => {
|
|
|
|
|
try {
|
|
|
|
|
const stackTrace = (new Error()).stack;
|
|
|
|
|
const plugin = stackTrace.match(new RegExp(`${escapeRegExp(Directories.PLUGINS)}.([-\\w]+)`));
|
|
|
|
|
if (plugin) {
|
|
|
|
|
return plugin[1];
|
|
|
|
|
}
|
|
|
|
|
const builtin = stackTrace.match(new RegExp(`${escapeRegExp(Directories.BUILTINS)}.([-\\w]+)`));
|
|
|
|
|
if (builtin) {
|
|
|
|
|
return builtin[1];
|
|
|
|
|
}
|
|
|
|
|
return 'vizality';
|
|
|
|
|
} catch (err) {
|
|
|
|
|
_error(_labels.concat('getCaller'), err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getAddonByPath = path => {
|
|
|
|
|
export const getCaller = path => {
|
|
|
|
|
try {
|
|
|
|
|
const plugin = path.match(new RegExp(`${escapeRegExp(Directories.PLUGINS)}.([-\\w]+)`));
|
|
|
|
|
if (plugin) {
|
|
|
|
|
return {
|
|
|
|
|
type: 'plugin',
|
|
|
|
|
id: plugin[1]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
const builtin = path.match(new RegExp(`${escapeRegExp(Directories.BUILTINS)}.([-\\w]+)`));
|
|
|
|
|
if (builtin) {
|
|
|
|
|
if (path) {
|
|
|
|
|
const plugin = path.match(new RegExp(`${escapeRegExp(Directories.PLUGINS)}.([-\\w]+)`));
|
|
|
|
|
if (plugin) {
|
|
|
|
|
return {
|
|
|
|
|
type: 'plugin',
|
|
|
|
|
id: plugin[1]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
const builtin = path.match(new RegExp(`${escapeRegExp(Directories.BUILTINS)}.([-\\w]+)`));
|
|
|
|
|
if (builtin) {
|
|
|
|
|
return {
|
|
|
|
|
type: 'builtin',
|
|
|
|
|
id: builtin[1]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
const theme = path.match(new RegExp(`${escapeRegExp(Directories.THEMES)}.([-\\w]+)`));
|
|
|
|
|
if (theme) {
|
|
|
|
|
return {
|
|
|
|
|
type: 'theme',
|
|
|
|
|
id: theme[1]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
type: 'builtin',
|
|
|
|
|
id: builtin[1]
|
|
|
|
|
type: 'core',
|
|
|
|
|
id: 'vizality'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
const theme = path.match(new RegExp(`${escapeRegExp(Directories.THEMES)}.([-\\w]+)`));
|
|
|
|
|
if (theme) {
|
|
|
|
|
} else {
|
|
|
|
|
const stackTrace = (new Error()).stack;
|
|
|
|
|
const plugin = stackTrace.match(new RegExp(`${escapeRegExp(Directories.PLUGINS)}.([-\\w]+)`));
|
|
|
|
|
if (plugin) {
|
|
|
|
|
return {
|
|
|
|
|
type: 'plugin',
|
|
|
|
|
id: plugin[1]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
const builtin = stackTrace.match(new RegExp(`${escapeRegExp(Directories.BUILTINS)}.([-\\w]+)`));
|
|
|
|
|
if (builtin) {
|
|
|
|
|
return {
|
|
|
|
|
type: 'builtin',
|
|
|
|
|
id: builtin[1]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
const theme = stackTrace.match(new RegExp(`${escapeRegExp(Directories.THEMES)}.([-\\w]+)`));
|
|
|
|
|
if (theme) {
|
|
|
|
|
return {
|
|
|
|
|
type: 'theme',
|
|
|
|
|
id: theme[1]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
type: 'theme',
|
|
|
|
|
id: theme[1]
|
|
|
|
|
type: 'core',
|
|
|
|
|
id: 'vizality'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
type: 'core',
|
|
|
|
|
id: 'vizality'
|
|
|
|
|
};
|
|
|
|
|
} catch (err) {
|
|
|
|
|
_error(_labels.concat('getAddonByPath'), err);
|
|
|
|
|
_error(_labels.concat('getCaller'), err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -80,7 +94,7 @@ export const getMimeType = async input => {
|
|
|
|
|
type = _getMimeType(input);
|
|
|
|
|
|
|
|
|
|
if (!type) {
|
|
|
|
|
type = await fetch(input).then(res => res.blob().then(blob => blob.type));
|
|
|
|
|
type = await get(input).then(res => res.blob().then(blob => blob.type));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!type) {
|
|
|
|
@ -148,7 +162,7 @@ export const getMediaDimensions = async (url, mimeType) => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const convertUrlToFile = (url, fileName) => {
|
|
|
|
|
return fetch(url)
|
|
|
|
|
return get(url)
|
|
|
|
|
.then(res => res.arrayBuffer())
|
|
|
|
|
.then(async buffer => new File([ buffer ], fileName, { type: await this.getMimeType(url) }));
|
|
|
|
|
};
|
|
|
|
|