From 2a5c19cd0133451b8279843024e2316053ca55c7 Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Tue, 6 Nov 2018 07:52:13 +0900
Subject: [PATCH] =?UTF-8?q?=E3=83=AA=E3=83=A2=E3=83=BC=E3=83=88=E3=81=AE?=
 =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=82=92=E3=82=AD=E3=83=A3?=
 =?UTF-8?q?=E3=83=83=E3=82=B7=E3=83=A5=E3=81=99=E3=82=8B=E3=81=8B=E3=81=A9?=
 =?UTF-8?q?=E3=81=86=E3=81=8B=E3=81=AE=E8=A8=AD=E5=AE=9A=E3=82=92DB?=
 =?UTF-8?q?=E3=81=AB=E4=BF=9D=E5=AD=98=E3=81=99=E3=82=8B=E3=82=88=E3=81=86?=
 =?UTF-8?q?=E3=81=AB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .config/example.yml                           |  9 ---------
 locales/ja-JP.yml                             |  2 ++
 src/client/app/admin/views/instance.vue       |  4 ++++
 src/config/types.ts                           |  2 --
 src/misc/fetch-meta.ts                        |  1 +
 src/models/meta.ts                            | 13 +++++++++++++
 src/server/api/endpoints/admin/update-meta.ts | 11 +++++++++++
 src/server/api/endpoints/meta.ts              |  1 +
 src/services/drive/upload-from-url.ts         |  5 ++++-
 9 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/.config/example.yml b/.config/example.yml
index abbbea50ad..63142e86e3 100644
--- a/.config/example.yml
+++ b/.config/example.yml
@@ -57,15 +57,6 @@ mongodb:
   user: example-misskey-user
   pass: example-misskey-pass
 
-# If enabled:
-#  Server will not cache remote files (Using direct link instead).
-#  You can save your storage.
-#
-#  NOTE:
-#  * Users cannot see remote images when they turn off "Show media from a remote server" setting.
-#  * Since thumbnails are not provided, traffic increases.
-preventCacheRemoteFiles: false
-
 drive:
   storage: 'db'
 
diff --git a/locales/ja-JP.yml b/locales/ja-JP.yml
index 0528a511ce..7794bebc24 100644
--- a/locales/ja-JP.yml
+++ b/locales/ja-JP.yml
@@ -1079,6 +1079,8 @@ admin/views/instance.vue:
   instance-description: "インスタンスの紹介"
   banner-url: "バナー画像URL"
   drive-config: "ドライブの設定"
+  cache-remote-files: "リモートのファイルをキャッシュする"
+  cache-remote-files-desc: "この設定を無効にすると、リモートファイルをキャッシュせず直リンクするようになります。そのためサーバーのストレージを節約できますが、プライバシー設定で直リンクを無効にしているユーザーにはファイルが見えなくなったり、サムネイルが生成されないので通信量が増加します。通常はこの設定をオンにしておくことをおすすめします。"
   local-drive-capacity-mb: "ローカルユーザーひとりあたりのドライブ容量"
   remote-drive-capacity-mb: "リモートユーザーひとりあたりのドライブ容量"
   mb: "メガバイト単位"
diff --git a/src/client/app/admin/views/instance.vue b/src/client/app/admin/views/instance.vue
index 9a7f8d556b..4d7965e703 100644
--- a/src/client/app/admin/views/instance.vue
+++ b/src/client/app/admin/views/instance.vue
@@ -12,6 +12,7 @@
 		</section>
 		<section class="fit-bottom">
 			<header><fa icon="cloud"/> %i18n:@drive-config%</header>
+			<ui-switch v-model="cacheRemoteFiles">%i18n:@cache-remote-files%<span slot="desc">%i18n:@cache-remote-files-desc%</span></ui-switch>
 			<ui-input v-model="localDriveCapacityMb">%i18n:@local-drive-capacity-mb%<span slot="text">%i18n:@mb%</span><span slot="suffix">MB</span></ui-input>
 			<ui-input v-model="remoteDriveCapacityMb">%i18n:@remote-drive-capacity-mb%<span slot="text">%i18n:@mb%</span><span slot="suffix">MB</span></ui-input>
 		</section>
@@ -49,6 +50,7 @@ export default Vue.extend({
 			bannerUrl: null,
 			name: null,
 			description: null,
+			cacheRemoteFiles: false,
 			localDriveCapacityMb: null,
 			remoteDriveCapacityMb: null,
 			maxNoteTextLength: null,
@@ -61,6 +63,7 @@ export default Vue.extend({
 			this.bannerUrl = meta.bannerUrl;
 			this.name = meta.name;
 			this.description = meta.description;
+			this.cacheRemoteFiles = meta.cacheRemoteFiles;
 			this.localDriveCapacityMb = meta.driveCapacityPerLocalUserMb;
 			this.remoteDriveCapacityMb = meta.driveCapacityPerRemoteUserMb;
 			this.maxNoteTextLength = meta.maxNoteTextLength;
@@ -86,6 +89,7 @@ export default Vue.extend({
 				bannerUrl: this.bannerUrl,
 				name: this.name,
 				description: this.description,
+				cacheRemoteFiles: this.cacheRemoteFiles,
 				localDriveCapacityMb: parseInt(this.localDriveCapacityMb, 10),
 				remoteDriveCapacityMb: parseInt(this.remoteDriveCapacityMb, 10),
 				maxNoteTextLength: parseInt(this.maxNoteTextLength, 10)
diff --git a/src/config/types.ts b/src/config/types.ts
index f1cbb84c8a..e6fe6a67c8 100644
--- a/src/config/types.ts
+++ b/src/config/types.ts
@@ -46,8 +46,6 @@ export type Source = {
 		secret_key: string;
 	};
 
-	preventCacheRemoteFiles: boolean;
-
 	drive?: {
 		storage: string;
 		bucket?: string;
diff --git a/src/misc/fetch-meta.ts b/src/misc/fetch-meta.ts
index 778aa029c1..fde244d1cc 100644
--- a/src/misc/fetch-meta.ts
+++ b/src/misc/fetch-meta.ts
@@ -2,6 +2,7 @@ import Meta, { IMeta } from '../models/meta';
 
 const defaultMeta: any = {
 	name: 'Misskey',
+	cacheRemoteFiles: true,
 	localDriveCapacityMb: 256,
 	remoteDriveCapacityMb: 8,
 	hidedTags: [],
diff --git a/src/models/meta.ts b/src/models/meta.ts
index 7caf41f19c..073be7de82 100644
--- a/src/models/meta.ts
+++ b/src/models/meta.ts
@@ -50,6 +50,17 @@ if ((config as any).remoteDriveCapacityMb) {
 		}
 	});
 }
+if ((config as any).preventCacheRemoteFiles) {
+	Meta.findOne({}).then(m => {
+		if (m != null && m.cacheRemoteFiles == null) {
+			Meta.update({}, {
+				$set: {
+					cacheRemoteFiles: !(config as any).preventCacheRemoteFiles
+				}
+			});
+		}
+	});
+}
 
 export type IMeta = {
 	name?: string;
@@ -66,6 +77,8 @@ export type IMeta = {
 	hidedTags?: string[];
 	bannerUrl?: string;
 
+	cacheRemoteFiles?: boolean;
+
 	/**
 	 * Drive capacity of a local user (MB)
 	 */
diff --git a/src/server/api/endpoints/admin/update-meta.ts b/src/server/api/endpoints/admin/update-meta.ts
index 4c4a3ac85c..b4b2b231ab 100644
--- a/src/server/api/endpoints/admin/update-meta.ts
+++ b/src/server/api/endpoints/admin/update-meta.ts
@@ -82,6 +82,13 @@ export const meta = {
 				'en-US': 'Drive capacity of a remote user (MB)'
 			}
 		},
+
+		cacheRemoteFiles: {
+			validator: $.bool.optional,
+			desc: {
+				'ja-JP': 'リモートのファイルをキャッシュするか否か'
+			}
+		}
 	}
 };
 
@@ -128,6 +135,10 @@ export default define(meta, (ps) => new Promise(async (res, rej) => {
 		set.remoteDriveCapacityMb = ps.remoteDriveCapacityMb;
 	}
 
+	if (ps.cacheRemoteFiles !== undefined) {
+		set.cacheRemoteFiles = ps.cacheRemoteFiles;
+	}
+
 	await Meta.update({}, {
 		$set: set
 	}, { upsert: true });
diff --git a/src/server/api/endpoints/meta.ts b/src/server/api/endpoints/meta.ts
index f7a5ed4f16..e3d3ad520f 100644
--- a/src/server/api/endpoints/meta.ts
+++ b/src/server/api/endpoints/meta.ts
@@ -59,6 +59,7 @@ export default define(meta, (ps, me) => new Promise(async (res, rej) => {
 		disableLocalTimeline: instance.disableLocalTimeline,
 		driveCapacityPerLocalUserMb: instance.localDriveCapacityMb,
 		driveCapacityPerRemoteUserMb: instance.remoteDriveCapacityMb,
+		cacheRemoteFiles: instance.cacheRemoteFiles,
 		recaptchaSitekey: config.recaptcha ? config.recaptcha.site_key : null,
 		swPublickey: config.sw ? config.sw.public_key : null,
 		hidedTags: (me && me.isAdmin) ? instance.hidedTags : undefined,
diff --git a/src/services/drive/upload-from-url.ts b/src/services/drive/upload-from-url.ts
index 2184edf005..7d4785be03 100644
--- a/src/services/drive/upload-from-url.ts
+++ b/src/services/drive/upload-from-url.ts
@@ -10,6 +10,7 @@ import create from './add-file';
 import config from '../../config';
 import { IUser } from '../../models/user';
 import * as mongodb from 'mongodb';
+import fetchMeta from '../../misc/fetch-meta';
 
 const log = debug('misskey:drive:upload-from-url');
 
@@ -51,11 +52,13 @@ export default async (url: string, user: IUser, folderId: mongodb.ObjectID = nul
 			.on('error', rej);
 	});
 
+	const instance = await fetchMeta();
+
 	let driveFile: IDriveFile;
 	let error;
 
 	try {
-		driveFile = await create(user, path, name, null, folderId, false, config.preventCacheRemoteFiles, url, uri, sensitive);
+		driveFile = await create(user, path, name, null, folderId, false, !instance.cacheRemoteFiles, url, uri, sensitive);
 		log(`got: ${driveFile._id}`);
 	} catch (e) {
 		error = e;