forked from mirror/misskey
parent
0f6c5f506a
commit
598500223a
@ -1,18 +1,20 @@
|
|||||||
|
import { Buffer } from 'buffer';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import * as tmp from 'tmp';
|
||||||
|
import * as stream from 'stream';
|
||||||
|
|
||||||
import * as mongodb from 'mongodb';
|
import * as mongodb from 'mongodb';
|
||||||
import * as crypto from 'crypto';
|
import * as crypto from 'crypto';
|
||||||
import * as gm from 'gm';
|
import * as gm from 'gm';
|
||||||
import * as debug from 'debug';
|
import * as debug from 'debug';
|
||||||
import fileType = require('file-type');
|
import fileType = require('file-type');
|
||||||
import prominence = require('prominence');
|
import prominence = require('prominence');
|
||||||
|
|
||||||
import DriveFile, { getGridFSBucket } from '../models/drive-file';
|
import DriveFile, { getGridFSBucket } from '../models/drive-file';
|
||||||
import DriveFolder from '../models/drive-folder';
|
import DriveFolder from '../models/drive-folder';
|
||||||
import serialize from '../serializers/drive-file';
|
import serialize from '../serializers/drive-file';
|
||||||
import event from '../event';
|
import event from '../event';
|
||||||
import config from '../../conf';
|
import config from '../../conf';
|
||||||
import { Buffer } from 'buffer';
|
|
||||||
import * as fs from 'fs';
|
|
||||||
import * as tmp from 'tmp';
|
|
||||||
import * as stream from 'stream';
|
|
||||||
|
|
||||||
const log = debug('misskey:register-drive-file');
|
const log = debug('misskey:register-drive-file');
|
||||||
|
|
||||||
@ -67,10 +69,12 @@ const addFile = async (
|
|||||||
.once('data', (buffer: Buffer) => {
|
.once('data', (buffer: Buffer) => {
|
||||||
readable.destroy();
|
readable.destroy();
|
||||||
const type = fileType(buffer);
|
const type = fileType(buffer);
|
||||||
if (!type) {
|
if (type) {
|
||||||
|
return res([type.mime, type.ext]);
|
||||||
|
} else {
|
||||||
|
// 種類が同定できなかったら application/octet-stream にする
|
||||||
return res(['application/octet-stream', null]);
|
return res(['application/octet-stream', null]);
|
||||||
}
|
}
|
||||||
return res([type.mime, type.ext]);
|
|
||||||
});
|
});
|
||||||
}))(),
|
}))(),
|
||||||
// size
|
// size
|
||||||
@ -105,9 +109,18 @@ const addFile = async (
|
|||||||
const [properties, folder] = await Promise.all([
|
const [properties, folder] = await Promise.all([
|
||||||
// properties
|
// properties
|
||||||
(async () => {
|
(async () => {
|
||||||
|
// 画像かどうか
|
||||||
if (!/^image\/.*$/.test(mime)) {
|
if (!/^image\/.*$/.test(mime)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const imageType = mime.split('/')[1];
|
||||||
|
|
||||||
|
// 画像でもPNGかJPEGでないならスキップ
|
||||||
|
if (imageType != 'png' && imageType != 'jpeg') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// If the file is an image, calculate width and height to save in property
|
// If the file is an image, calculate width and height to save in property
|
||||||
const g = gm(fs.createReadStream(path), name);
|
const g = gm(fs.createReadStream(path), name);
|
||||||
const size = await prominence(g).size();
|
const size = await prominence(g).size();
|
||||||
@ -115,7 +128,9 @@ const addFile = async (
|
|||||||
width: size.width,
|
width: size.width,
|
||||||
height: size.height
|
height: size.height
|
||||||
};
|
};
|
||||||
|
|
||||||
log('image width and height is calculated');
|
log('image width and height is calculated');
|
||||||
|
|
||||||
return properties;
|
return properties;
|
||||||
})(),
|
})(),
|
||||||
// folder
|
// folder
|
||||||
@ -136,20 +151,18 @@ const addFile = async (
|
|||||||
(async () => {
|
(async () => {
|
||||||
// Calculate drive usage
|
// Calculate drive usage
|
||||||
const usage = await DriveFile
|
const usage = await DriveFile
|
||||||
.aggregate([
|
.aggregate([{
|
||||||
{ $match: { 'metadata.user_id': user._id } },
|
$match: { 'metadata.user_id': user._id }
|
||||||
{
|
}, {
|
||||||
$project: {
|
$project: {
|
||||||
length: true
|
length: true
|
||||||
}
|
}
|
||||||
},
|
}, {
|
||||||
{
|
|
||||||
$group: {
|
$group: {
|
||||||
_id: null,
|
_id: null,
|
||||||
usage: { $sum: '$length' }
|
usage: { $sum: '$length' }
|
||||||
}
|
}
|
||||||
}
|
}])
|
||||||
])
|
|
||||||
.then((aggregates: any[]) => {
|
.then((aggregates: any[]) => {
|
||||||
if (aggregates.length > 0) {
|
if (aggregates.length > 0) {
|
||||||
return aggregates[0].usage;
|
return aggregates[0].usage;
|
||||||
@ -227,8 +240,7 @@ export default (user: any, file: string | stream.Readable, ...args) => new Promi
|
|||||||
log(`drive file has been created ${file._id}`);
|
log(`drive file has been created ${file._id}`);
|
||||||
resolve(file);
|
resolve(file);
|
||||||
|
|
||||||
serialize(file)
|
serialize(file).then(serializedFile => {
|
||||||
.then(serializedFile => {
|
|
||||||
// Publish drive_file_created event
|
// Publish drive_file_created event
|
||||||
event(user._id, 'drive_file_created', serializedFile);
|
event(user._id, 'drive_file_created', serializedFile);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user