summaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
authorMeiMei <30769358+mei23@users.noreply.github.com>2019-02-05 03:01:36 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2019-02-05 03:01:36 +0900
commitf014b7ae0ece886ef0cff2366b9925e23b34ba6f (patch)
treec35760467278da408348fcd459415af7a63e08e9 /src/server
parentRefactor: Better type definition (diff)
downloadsharkey-f014b7ae0ece886ef0cff2366b9925e23b34ba6f.tar.gz
sharkey-f014b7ae0ece886ef0cff2366b9925e23b34ba6f.tar.bz2
sharkey-f014b7ae0ece886ef0cff2366b9925e23b34ba6f.zip
アニメーションを自動再生しないオプション (#4131)
* Refactor * settings * Media Proxy * Replace API response
Diffstat (limited to 'src/server')
-rw-r--r--src/server/api/endpoints/drive/stream.ts2
-rw-r--r--src/server/index.ts1
-rw-r--r--src/server/proxy/index.ts22
-rw-r--r--src/server/proxy/proxy-media.ts113
4 files changed, 137 insertions, 1 deletions
diff --git a/src/server/api/endpoints/drive/stream.ts b/src/server/api/endpoints/drive/stream.ts
index c8342c66b5..d364f62778 100644
--- a/src/server/api/endpoints/drive/stream.ts
+++ b/src/server/api/endpoints/drive/stream.ts
@@ -65,5 +65,5 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => {
sort: sort
});
- res(await packMany(files, { self: true }));
+ res(await packMany(files, { self: true, me: user }));
}));
diff --git a/src/server/index.ts b/src/server/index.ts
index 26fa06d111..720a191d55 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -61,6 +61,7 @@ if (config.url.startsWith('https') && !config.disableHsts) {
app.use(mount('/api', apiServer));
app.use(mount('/files', require('./file')));
+app.use(mount('/proxy', require('./proxy')));
// Init router
const router = new Router();
diff --git a/src/server/proxy/index.ts b/src/server/proxy/index.ts
new file mode 100644
index 0000000000..8d33af85da
--- /dev/null
+++ b/src/server/proxy/index.ts
@@ -0,0 +1,22 @@
+/**
+ * Media Proxy
+ */
+
+import * as Koa from 'koa';
+import * as cors from '@koa/cors';
+import * as Router from 'koa-router';
+import { proxyMedia } from './proxy-media';
+
+// Init app
+const app = new Koa();
+app.use(cors());
+
+// Init router
+const router = new Router();
+
+router.get('/:url*', proxyMedia);
+
+// Register router
+app.use(router.routes());
+
+module.exports = app;
diff --git a/src/server/proxy/proxy-media.ts b/src/server/proxy/proxy-media.ts
new file mode 100644
index 0000000000..a0b65fbcfc
--- /dev/null
+++ b/src/server/proxy/proxy-media.ts
@@ -0,0 +1,113 @@
+import * as fs from 'fs';
+import * as URL from 'url';
+import * as tmp from 'tmp';
+import * as Koa from 'koa';
+import * as request from 'request';
+import * as fileType from 'file-type';
+import * as isSvg from 'is-svg';
+import { serverLogger } from '..';
+import config from '../../config';
+import { IImage, ConvertToPng } from '../../services/drive/image-processor';
+
+export async function proxyMedia(ctx: Koa.BaseContext) {
+ const url = 'url' in ctx.query ? ctx.query.url : 'https://' + ctx.params.url;
+ console.log(url);
+
+ // 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]);
+ });
+ });
+
+ try {
+ await fetch(url, path);
+
+ const [type, ext] = await detectMine(path);
+
+ let image: IImage;
+
+ if ('static' in ctx.query && ['image/png', 'image/gif'].includes(type)) {
+ image = await ConvertToPng(path, 498, 280);
+ } else {
+ image = {
+ data: fs.readFileSync(path),
+ ext,
+ type,
+ };
+ }
+
+ ctx.set('Content-Type', type);
+ ctx.set('Cache-Control', 'max-age=31536000, immutable');
+ ctx.body = image.data;
+ } catch (e) {
+ serverLogger.error(e);
+ ctx.status = 500;
+ } finally {
+ cleanup();
+ }
+}
+
+async function fetch(url: string, path: string) {
+ await new Promise((res, rej) => {
+ const writable = fs.createWriteStream(path);
+
+ writable.on('finish', () => {
+ res();
+ });
+
+ writable.on('error', 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.user_agent
+ }
+ });
+
+ req.pipe(writable);
+
+ req.on('response', response => {
+ if (response.statusCode !== 200) {
+ writable.close();
+ rej(response.statusCode);
+ }
+ });
+
+ req.on('error', error => {
+ writable.close();
+ rej(error);
+ });
+ });
+}
+
+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) {
+ res([type.mime, type.ext]);
+ } else if (isSvg(buffer)) {
+ res(['image/svg+xml', 'svg']);
+ } else {
+ // 種類が同定できなかったら application/octet-stream にする
+ res(['application/octet-stream', null]);
+ }
+ })
+ .on('end', () => {
+ // maybe 0 bytes
+ res(['application/octet-stream', null]);
+ });
+ });
+}