This commit is contained in:
syuilo
2018-07-25 00:29:18 +09:00
parent 7432de3d33
commit c3b3b9b9a6
8 changed files with 21 additions and 56 deletions

View File

@ -1,25 +0,0 @@
import * as stream from 'stream';
import * as Gm from 'gm';
import { IDriveFile, getDriveFileBucket } from '../models/drive-file';
const gm = Gm.subClass({
imageMagick: true
});
export default async function(file: IDriveFile): Promise<stream.Readable> {
if (!/^image\/.*$/.test(file.contentType)) return null;
const bucket = await getDriveFileBucket();
const readable = bucket.openDownloadStream(file._id);
const g = gm(readable);
const stream = g
.resize(256, 256)
.compress('jpeg')
.quality(70)
.interlace('line')
.stream();
return stream;
}

View File

@ -11,7 +11,6 @@ export default class {
public showAll(): void {
this.show('MongoDB', 'mongo --version', x => x.match(/^MongoDB shell version:? v(.*)\r?\n/));
this.show('Redis', 'redis-server --version', x => x.match(/v=([0-9\.]*)/));
this.show('ImageMagick', 'magick -version', x => x.match(/^Version: ImageMagick ([^ ]*)/));
}
public show(serviceName: string, command: string, transform: (x: string) => RegExpMatchArray): void {

View File

@ -37,6 +37,7 @@ export type IMetadata = {
storage?: string;
storageProps?: any;
isSensitive?: boolean;
isRemote?: boolean;
};
export type IDriveFile = {
@ -160,7 +161,7 @@ export const pack = (
_target.url = _file.metadata.url ? _file.metadata.url : `${config.drive_url}/${_target.id}/${encodeURIComponent(_target.name)}`;
_target.src = _file.metadata.url;
_target.isRemote = _file.metadata.withoutChunks;
_target.isRemote = _file.metadata.isRemote;
if (_target.properties == null) _target.properties = {};

View File

@ -4,12 +4,11 @@ import * as stream from 'stream';
import * as mongodb from 'mongodb';
import * as crypto from 'crypto';
import * as _gm from 'gm';
import * as debug from 'debug';
import fileType = require('file-type');
const prominence = require('prominence');
import * as Minio from 'minio';
import * as uuid from 'uuid';
import * as sharp from 'sharp';
import DriveFile, { IMetadata, getDriveFileBucket, IDriveFile } from '../../models/drive-file';
import DriveFolder from '../../models/drive-folder';
@ -19,10 +18,6 @@ import { isLocalUser, IUser, IRemoteUser } from '../../models/user';
import delFile from './delete-file';
import config from '../../config';
const gm = _gm.subClass({
imageMagick: true
});
const log = debug('misskey:drive:add-file');
async function save(readable: stream.Readable, name: string, type: string, hash: string, size: number, metadata: any): Promise<IDriveFile> {
@ -53,6 +48,8 @@ async function save(readable: stream.Readable, name: string, type: string, hash:
});
return file;
} else {
throw 'unknown storage type';
}
} else {
// Get MongoDB GridFS bucket
@ -228,42 +225,37 @@ export default async function(
let propPromises: Array<Promise<void>> = [];
const isImage = ['image/jpeg', 'image/gif', 'image/png'].includes(mime);
const isImage = ['image/jpeg', 'image/gif', 'image/png', 'image/webp'].includes(mime);
if (isImage) {
const img = sharp(path);
// Calc width and height
const calcWh = async () => {
log('calculate image width and height...');
// Calculate width and height
const g = gm(fs.createReadStream(path), name);
const size = await prominence(g).size();
const meta = await img.metadata();
log(`image width and height is calculated: ${size.width}, ${size.height}`);
log(`image width and height is calculated: ${meta.width}, ${meta.height}`);
properties['width'] = size.width;
properties['height'] = size.height;
properties['width'] = meta.width;
properties['height'] = meta.height;
};
// Calc average color
const calcAvg = async () => {
log('calculate average color...');
const info = await prominence(gm(fs.createReadStream(path), name)).identify();
const isTransparent = info ? info['Channel depth'].Alpha != null : false;
const info = await (img as any).stats();
const buffer = await prominence(gm(fs.createReadStream(path), name)
.setFormat('ppm')
.resize(1, 1)) // 1pxのサイズに縮小して平均色を取得するというハック
.toBuffer();
const r = buffer.readUInt8(buffer.length - 3);
const g = buffer.readUInt8(buffer.length - 2);
const b = buffer.readUInt8(buffer.length - 1);
const r = Math.round(info.channels[0].mean);
const g = Math.round(info.channels[1].mean);
const b = Math.round(info.channels[2].mean);
log(`average color is calculated: ${r}, ${g}, ${b}`);
const value = isTransparent ? [r, g, b, 255] : [r, g, b];
const value = info.isOpaque ? [r, g, b] : [r, g, b, 255];
properties['avgColor'] = value;
};
@ -282,6 +274,7 @@ export default async function(
comment: comment,
properties: properties,
withoutChunks: isLink,
isRemote: isLink,
isSensitive: sensitive
} as IMetadata;