期限切れ/未保存リモートファイルのローカルプロキシ (#5655)
* Media Proxy を実装 * サンプルを追加 * https://github.com/syuilo/misskey/pull/5649#discussion_r359967471 の修正 * https://github.com/syuilo/misskey/pull/5649#discussion_r359967966 の修正 * https://github.com/syuilo/misskey/pull/5649#discussion_r359968219 の修正 * 期限切れ/未保存リモートファイルのローカルプロキシ * 設定 * 説明 * comment out * fix Co-authored-by: 和風ドレッシング <37681609+CookieRamen@users.noreply.github.com>
This commit is contained in:
		| @@ -151,6 +151,13 @@ export const meta = { | ||||
| 			} | ||||
| 		}, | ||||
|  | ||||
| 		proxyRemoteFiles: { | ||||
| 			validator: $.optional.bool, | ||||
| 			desc: { | ||||
| 				'ja-JP': 'ローカルにないリモートのファイルをプロキシするか否か' | ||||
| 			} | ||||
| 		}, | ||||
|  | ||||
| 		enableRecaptcha: { | ||||
| 			validator: $.optional.bool, | ||||
| 			desc: { | ||||
| @@ -478,6 +485,10 @@ export default define(meta, async (ps, me) => { | ||||
| 		set.cacheRemoteFiles = ps.cacheRemoteFiles; | ||||
| 	} | ||||
|  | ||||
| 	if (ps.proxyRemoteFiles !== undefined) { | ||||
| 		set.proxyRemoteFiles = ps.proxyRemoteFiles; | ||||
| 	} | ||||
|  | ||||
| 	if (ps.enableRecaptcha !== undefined) { | ||||
| 		set.enableRecaptcha = ps.enableRecaptcha; | ||||
| 	} | ||||
|   | ||||
| @@ -143,6 +143,7 @@ export default define(meta, async (ps, me) => { | ||||
| 		driveCapacityPerLocalUserMb: instance.localDriveCapacityMb, | ||||
| 		driveCapacityPerRemoteUserMb: instance.remoteDriveCapacityMb, | ||||
| 		cacheRemoteFiles: instance.cacheRemoteFiles, | ||||
| 		proxyRemoteFiles: instance.proxyRemoteFiles, | ||||
| 		enableRecaptcha: instance.enableRecaptcha, | ||||
| 		recaptchaSiteKey: instance.recaptchaSiteKey, | ||||
| 		swPublickey: instance.swPublicKey, | ||||
|   | ||||
| @@ -1,10 +1,16 @@ | ||||
| import * as Koa from 'koa'; | ||||
| import * as send from 'koa-send'; | ||||
| import * as rename from 'rename'; | ||||
| import * as tmp from 'tmp'; | ||||
| import * as fs from 'fs'; | ||||
| import { serverLogger } from '..'; | ||||
| import { contentDisposition } from '../../misc/content-disposition'; | ||||
| import { DriveFiles } from '../../models'; | ||||
| import { InternalStorage } from '../../services/drive/internal-storage'; | ||||
| import { downloadUrl } from '../../misc/donwload-url'; | ||||
| import { detectMine } from '../../misc/detect-mine'; | ||||
| import { convertToJpeg, convertToPng, convertToGif, convertToApng } from '../../services/drive/image-processor'; | ||||
| import { GenerateVideoThumbnail } from '../../services/drive/generate-video-thumbnail'; | ||||
|  | ||||
| const assets = `${__dirname}/../../server/file/assets/`; | ||||
|  | ||||
| @@ -31,15 +37,70 @@ export default async function(ctx: Koa.Context) { | ||||
| 		return; | ||||
| 	} | ||||
|  | ||||
| 	const isThumbnail = file.thumbnailAccessKey === key; | ||||
| 	const isWebpublic = file.webpublicAccessKey === key; | ||||
|  | ||||
| 	if (!file.storedInternal) { | ||||
| 		if (file.isLink && file.uri) {	// 期限切れリモートファイル | ||||
| 			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 downloadUrl(file.uri, path); | ||||
|  | ||||
| 				const [type, ext] = await detectMine(path); | ||||
|  | ||||
| 				const convertFile = async () => { | ||||
| 					if (isThumbnail) { | ||||
| 						if (['image/jpeg', 'image/webp'].includes(type)) { | ||||
| 							return await convertToJpeg(path, 498, 280); | ||||
| 						} else if (['image/png'].includes(type)) { | ||||
| 							return await convertToPng(path, 498, 280); | ||||
| 						} else if (['image/gif'].includes(type)) { | ||||
| 							return await convertToGif(path); | ||||
| 						} else if (['image/apng', 'image/vnd.mozilla.apng'].includes(type)) { | ||||
| 							return await convertToApng(path); | ||||
| 						} else if (type.startsWith('video/')) { | ||||
| 							return await GenerateVideoThumbnail(path); | ||||
| 						} | ||||
| 					} | ||||
|  | ||||
| 					return { | ||||
| 						data: fs.readFileSync(path), | ||||
| 						ext, | ||||
| 						type, | ||||
| 					}; | ||||
| 				}; | ||||
|  | ||||
| 				const image = await convertFile(); | ||||
| 				ctx.body = image.data; | ||||
| 				ctx.set('Content-Type', file.type); | ||||
| 				ctx.set('Cache-Control', 'max-age=31536000, immutable'); | ||||
| 			} catch (e) { | ||||
| 				serverLogger.error(e); | ||||
|  | ||||
| 				if (typeof e == 'number' && e >= 400 && e < 500) { | ||||
| 					ctx.status = e; | ||||
| 					ctx.set('Cache-Control', 'max-age=86400'); | ||||
| 				} else { | ||||
| 					ctx.status = 500; | ||||
| 					ctx.set('Cache-Control', 'max-age=300'); | ||||
| 				} | ||||
| 			} finally { | ||||
| 				cleanup(); | ||||
| 			} | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| 		ctx.status = 204; | ||||
| 		ctx.set('Cache-Control', 'max-age=86400'); | ||||
| 		return; | ||||
| 	} | ||||
|  | ||||
| 	const isThumbnail = file.thumbnailAccessKey === key; | ||||
| 	const isWebpublic = file.webpublicAccessKey === key; | ||||
|  | ||||
| 	if (isThumbnail) { | ||||
| 		ctx.body = InternalStorage.read(key); | ||||
| 		ctx.set('Content-Type', 'image/jpeg'); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 MeiMei
					MeiMei