ドライブファイルURL生成などの修正 (#5671)

* Fix: リモートプロキシ時にサムネイルのContent-Typeがおかしい

* fix drive
This commit is contained in:
MeiMei 2020-01-02 02:45:05 +09:00 committed by syuilo
parent b0bb5d8dfc
commit 78ef0a9929
5 changed files with 8 additions and 58 deletions

View file

@ -39,25 +39,13 @@ export class DriveFileRepository extends Repository<DriveFile> {
const key = thumbnail ? file.thumbnailAccessKey : file.webpublicAccessKey; const key = thumbnail ? file.thumbnailAccessKey : file.webpublicAccessKey;
if (key && !key.match('/')) { // 古いものはここにオブジェクトストレージキーが入ってるので除外 if (key && !key.match('/')) { // 古いものはここにオブジェクトストレージキーが入ってるので除外
let ext = ''; return `/files/${key}`;
if (file.name) {
[ext] = (file.name.match(/\.(\w+)$/) || ['']);
}
if (ext === '') {
if (file.type === 'image/jpeg') ext = '.jpg';
if (file.type === 'image/png') ext = '.png';
if (file.type === 'image/webp') ext = '.webp';
if (file.type === 'image/apng') ext = '.apng';
if (file.type === 'image/vnd.mozilla.apng') ext = '.apng';
}
return `/files/${key}/${key}${ext}`;
} }
} }
return thumbnail ? (file.thumbnailUrl || file.webpublicUrl || null) : (file.webpublicUrl || file.url); const isImage = file.type && ['image/png', 'image/apng', 'image/gif', 'image/jpeg', 'image/webp', 'image/svg+xml'].includes(file.type);
return thumbnail ? (file.thumbnailUrl || (isImage ? (file.webpublicUrl || file.url) : null)) : (file.webpublicUrl || file.url);
} }
public async clacDriveUsageOf(user: User['id'] | User): Promise<number> { public async clacDriveUsageOf(user: User['id'] | User): Promise<number> {

View file

@ -78,7 +78,7 @@ export default async function(ctx: Koa.Context) {
const image = await convertFile(); const image = await convertFile();
ctx.body = image.data; ctx.body = image.data;
ctx.set('Content-Type', file.type); ctx.set('Content-Type', image.type);
ctx.set('Cache-Control', 'max-age=31536000, immutable'); ctx.set('Cache-Control', 'max-age=31536000, immutable');
} catch (e) { } catch (e) {
serverLogger.error(e); serverLogger.error(e);

View file

@ -10,7 +10,7 @@ import { deleteFile } from './delete-file';
import { fetchMeta } from '../../misc/fetch-meta'; import { fetchMeta } from '../../misc/fetch-meta';
import { GenerateVideoThumbnail } from './generate-video-thumbnail'; import { GenerateVideoThumbnail } from './generate-video-thumbnail';
import { driveLogger } from './logger'; import { driveLogger } from './logger';
import { IImage, convertToJpeg, convertToWebp, convertToPng, convertToGif, convertToApng } from './image-processor'; import { IImage, convertToJpeg, convertToWebp, convertToPng } from './image-processor';
import { contentDisposition } from '../../misc/content-disposition'; import { contentDisposition } from '../../misc/content-disposition';
import { detectMine } from '../../misc/detect-mine'; import { detectMine } from '../../misc/detect-mine';
import { DriveFiles, DriveFolders, Users, Instances, UserProfiles } from '../../models'; import { DriveFiles, DriveFolders, Users, Instances, UserProfiles } from '../../models';
@ -159,12 +159,6 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
webpublic = await convertToWebp(path, 2048, 2048); webpublic = await convertToWebp(path, 2048, 2048);
} else if (['image/png'].includes(type)) { } else if (['image/png'].includes(type)) {
webpublic = await convertToPng(path, 2048, 2048); webpublic = await convertToPng(path, 2048, 2048);
} else if (['image/apng', 'image/vnd.mozilla.apng'].includes(type)) {
webpublic = await convertToApng(path);
} else if (['image/gif'].includes(type)) {
webpublic = await convertToGif(path);
} else {
logger.info(`web image not created (not an image)`);
} }
} catch (e) { } catch (e) {
logger.warn(`web image not created (an error occured)`, e); logger.warn(`web image not created (an error occured)`, e);
@ -182,10 +176,6 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
thumbnail = await convertToJpeg(path, 498, 280); thumbnail = await convertToJpeg(path, 498, 280);
} else if (['image/png'].includes(type)) { } else if (['image/png'].includes(type)) {
thumbnail = await convertToPng(path, 498, 280); thumbnail = await convertToPng(path, 498, 280);
} else if (['image/gif'].includes(type)) {
thumbnail = await convertToGif(path);
} else if (['image/apng', 'image/vnd.mozilla.apng'].includes(type)) {
thumbnail = await convertToApng(path);
} else if (type.startsWith('video/')) { } else if (type.startsWith('video/')) {
try { try {
thumbnail = await GenerateVideoThumbnail(path); thumbnail = await GenerateVideoThumbnail(path);
@ -422,8 +412,6 @@ export default async function(
if (isLink) { if (isLink) {
file.url = url; file.url = url;
file.thumbnailUrl = url;
file.webpublicUrl = url;
// ローカルプロキシ用 // ローカルプロキシ用
file.accessKey = uuid(); file.accessKey = uuid();
file.thumbnailAccessKey = 'thumbnail-' + uuid(); file.thumbnailAccessKey = 'thumbnail-' + uuid();

View file

@ -69,8 +69,8 @@ function postProcess(file: DriveFile, isExpired = false) {
DriveFiles.update(file.id, { DriveFiles.update(file.id, {
isLink: true, isLink: true,
url: file.uri, url: file.uri,
thumbnailUrl: file.uri, thumbnailUrl: null,
webpublicUrl: file.uri, webpublicUrl: null,
size: 0, size: 0,
// ローカルプロキシ用 // ローカルプロキシ用
accessKey: uuid(), accessKey: uuid(),

View file

@ -74,29 +74,3 @@ export async function convertToPng(path: string, width: number, height: number):
type: 'image/png' type: 'image/png'
}; };
} }
/**
* Convert to GIF (Actually just NOP)
*/
export async function convertToGif(path: string): Promise<IImage> {
const data = await fs.promises.readFile(path);
return {
data,
ext: 'gif',
type: 'image/gif'
};
}
/**
* Convert to APNG (Actually just NOP)
*/
export async function convertToApng(path: string): Promise<IImage> {
const data = await fs.promises.readFile(path);
return {
data,
ext: 'apng',
type: 'image/apng'
};
}