summaryrefslogtreecommitdiff
path: root/src/misc
diff options
context:
space:
mode:
Diffstat (limited to 'src/misc')
-rw-r--r--src/misc/create-temp.ts10
-rw-r--r--src/misc/detect-mine.ts31
-rw-r--r--src/misc/detect-url-mine.ts15
-rw-r--r--src/misc/donwload-url.ts61
-rw-r--r--src/misc/download-text-file.ts76
5 files changed, 128 insertions, 65 deletions
diff --git a/src/misc/create-temp.ts b/src/misc/create-temp.ts
new file mode 100644
index 0000000000..04604cf7d0
--- /dev/null
+++ b/src/misc/create-temp.ts
@@ -0,0 +1,10 @@
+import * as tmp from 'tmp';
+
+export function createTemp(): Promise<[string, any]> {
+ return new Promise<[string, any]>((res, rej) => {
+ tmp.file((e, path, fd, cleanup) => {
+ if (e) return rej(e);
+ res([path, cleanup]);
+ });
+ });
+}
diff --git a/src/misc/detect-mine.ts b/src/misc/detect-mine.ts
new file mode 100644
index 0000000000..bbf49efc10
--- /dev/null
+++ b/src/misc/detect-mine.ts
@@ -0,0 +1,31 @@
+import * as fs from 'fs';
+import fileType from 'file-type';
+import checkSvg from '../misc/check-svg';
+
+export async function detectMine(path: string) {
+ return new Promise<[string, string]>((res, rej) => {
+ const readable = fs.createReadStream(path);
+ readable
+ .on('error', rej)
+ .once('data', (buffer: Buffer) => {
+ readable.destroy();
+ const type = fileType(buffer);
+ if (type) {
+ if (type.mime == 'application/xml' && checkSvg(path)) {
+ res(['image/svg+xml', 'svg']);
+ } else {
+ res([type.mime, type.ext]);
+ }
+ } else if (checkSvg(path)) {
+ res(['image/svg+xml', 'svg']);
+ } else {
+ // 種類が同定できなかったら application/octet-stream にする
+ res(['application/octet-stream', null]);
+ }
+ })
+ .on('end', () => {
+ // maybe 0 bytes
+ res(['application/octet-stream', null]);
+ });
+ });
+}
diff --git a/src/misc/detect-url-mine.ts b/src/misc/detect-url-mine.ts
new file mode 100644
index 0000000000..eef64cfc56
--- /dev/null
+++ b/src/misc/detect-url-mine.ts
@@ -0,0 +1,15 @@
+import { createTemp } from './create-temp';
+import { downloadUrl } from './donwload-url';
+import { detectMine } from './detect-mine';
+
+export async function detectUrlMine(url: string) {
+ const [path, cleanup] = await createTemp();
+
+ try {
+ await downloadUrl(url, path);
+ const [type] = await detectMine(path);
+ return type;
+ } finally {
+ cleanup();
+ }
+}
diff --git a/src/misc/donwload-url.ts b/src/misc/donwload-url.ts
new file mode 100644
index 0000000000..0dd4e4ef5d
--- /dev/null
+++ b/src/misc/donwload-url.ts
@@ -0,0 +1,61 @@
+import * as fs from 'fs';
+import * as URL from 'url';
+import * as request from 'request';
+import config from '../config';
+import chalk from 'chalk';
+import Logger from '../services/logger';
+
+export async function downloadUrl(url: string, path: string) {
+ const logger = new Logger('download-url');
+
+ await new Promise((res, rej) => {
+ logger.info(`Downloading ${chalk.cyan(url)} ...`);
+
+ const writable = fs.createWriteStream(path);
+
+ writable.on('finish', () => {
+ logger.succ(`Download finished: ${chalk.cyan(url)}`);
+ res();
+ });
+
+ writable.on('error', error => {
+ logger.error(`Download failed: ${chalk.cyan(url)}: ${error}`, {
+ url: url,
+ e: error
+ });
+ rej(error);
+ });
+
+ const requestUrl = URL.parse(url).pathname.match(/[^\u0021-\u00ff]/) ? encodeURI(url) : url;
+
+ const req = request({
+ url: requestUrl,
+ proxy: config.proxy,
+ timeout: 10 * 1000,
+ headers: {
+ 'User-Agent': config.userAgent
+ }
+ });
+
+ req.pipe(writable);
+
+ req.on('response', response => {
+ if (response.statusCode !== 200) {
+ logger.error(`Got ${response.statusCode} (${url})`);
+ writable.close();
+ rej(response.statusCode);
+ }
+ });
+
+ req.on('error', error => {
+ logger.error(`Failed to start download: ${chalk.cyan(url)}: ${error}`, {
+ url: url,
+ e: error
+ });
+ writable.close();
+ rej(error);
+ });
+
+ logger.succ(`Downloaded to: ${path}`);
+ });
+}
diff --git a/src/misc/download-text-file.ts b/src/misc/download-text-file.ts
index 60c9d83da9..f73286e9bb 100644
--- a/src/misc/download-text-file.ts
+++ b/src/misc/download-text-file.ts
@@ -1,79 +1,25 @@
-import * as tmp from 'tmp';
import * as fs from 'fs';
import * as util from 'util';
-import chalk from 'chalk';
-import * as request from 'request';
import Logger from '../services/logger';
-import config from '../config';
+import { createTemp } from './create-temp';
+import { downloadUrl } from './donwload-url';
const logger = new Logger('download-text-file');
export async function downloadTextFile(url: string): Promise<string> {
// Create temp file
- const [path, cleanup] = await new Promise<[string, any]>((res, rej) => {
- tmp.file((e, path, fd, cleanup) => {
- if (e) return rej(e);
- res([path, cleanup]);
- });
- });
+ const [path, cleanup] = await createTemp();
logger.info(`Temp file is ${path}`);
- // write content at URL to temp file
- await new Promise((res, rej) => {
- logger.info(`Downloading ${chalk.cyan(url)} ...`);
+ try {
+ // write content at URL to temp file
+ await downloadUrl(url, path);
- const writable = fs.createWriteStream(path);
+ const text = await util.promisify(fs.readFile)(path, 'utf8');
- writable.on('finish', () => {
- logger.succ(`Download finished: ${chalk.cyan(url)}`);
- res();
- });
-
- writable.on('error', error => {
- logger.error(`Download failed: ${chalk.cyan(url)}: ${error}`, {
- url: url,
- e: error
- });
- rej(error);
- });
-
- const requestUrl = new URL(url).pathname.match(/[^\u0021-\u00ff]/) ? encodeURI(url) : url;
-
- const req = request({
- url: requestUrl,
- proxy: config.proxy,
- timeout: 10 * 1000,
- headers: {
- 'User-Agent': config.userAgent
- }
- });
-
- req.pipe(writable);
-
- req.on('response', response => {
- if (response.statusCode !== 200) {
- logger.error(`Got ${response.statusCode} (${url})`);
- writable.close();
- rej(response.statusCode);
- }
- });
-
- req.on('error', error => {
- logger.error(`Failed to start download: ${chalk.cyan(url)}: ${error}`, {
- url: url,
- e: error
- });
- writable.close();
- rej(error);
- });
- });
-
- logger.succ(`Downloaded to: ${path}`);
-
- const text = await util.promisify(fs.readFile)(path, 'utf8');
-
- cleanup();
-
- return text;
+ return text;
+ } finally {
+ cleanup();
+ }
}