From ba0fe83b54a44493447208dfb3cc304a60df6fdd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Acid=20Chicken=20=28=E7=A1=AB=E9=85=B8=E9=B6=8F=29?=
 <root@acid-chicken.com>
Date: Sat, 12 Jan 2019 19:15:19 +0900
Subject: [PATCH] Update add-file.ts

---
 src/services/drive/add-file.ts | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts
index 2ea8cdc3bd..00d2c3346c 100644
--- a/src/services/drive/add-file.ts
+++ b/src/services/drive/add-file.ts
@@ -80,6 +80,11 @@ async function save(path: string, name: string, type: string, hash: string, size
 
 			webpublicExt = 'png';
 			webpublicType = 'image/png';
+		} else if (['image/svg', 'image/svg+xml'].includes(type)) {
+			webpublic = await fs.readFile(path);
+
+			webpublicExt = 'svg';
+			webpublicType = 'image/svg+xml';
 		} else {
 			log(`web image not created (not an image)`);
 		}
@@ -115,6 +120,18 @@ async function save(path: string, name: string, type: string, hash: string, size
 			.png()
 			.toBuffer();
 
+		thumbnailExt = 'png';
+		thumbnailType = 'image/png';
+	} else if (['image/svg', 'image/svg+xml'].includes(type)) {
+		thumbnail = await fs.readFile(path).then(x => sharp()
+			.resize(498, 280, {
+				fit: 'inside',
+				withoutEnlargement: true
+			})
+			.overlayWith(x, { cutout: true })
+			.png()
+			.toBuffer());
+
 		thumbnailExt = 'png';
 		thumbnailType = 'image/png';
 	}
@@ -122,11 +139,13 @@ async function save(path: string, name: string, type: string, hash: string, size
 
 	if (config.drive && config.drive.storage == 'minio') {
 		let [ext] = (name.match(/\.([a-zA-Z0-9_-]+)$/) || ['']);
+		const [primaryType] = type.split('+');
 
 		if (ext === '') {
-			if (type === 'image/jpeg') ext = '.jpg';
-			if (type === 'image/png') ext = '.png';
-			if (type === 'image/webp') ext = '.webp';
+			if (primaryType === 'image/jpeg') ext = '.jpg';
+			if (primaryType === 'image/png') ext = '.png';
+			if (primaryType === 'image/webp') ext = '.webp';
+			if (primaryType === 'image/svg') ext = '.svg';
 		}
 
 		const key = `${config.drive.prefix}/${uuid.v4()}${ext}`;