wip
This commit is contained in:
parent
286e15b967
commit
1c7a194950
@ -17,30 +17,52 @@ import { publishUserStream, publishDriveStream } from '../../stream';
|
|||||||
import { isLocalUser, IUser, IRemoteUser } from '../../models/user';
|
import { isLocalUser, IUser, IRemoteUser } from '../../models/user';
|
||||||
import delFile from './delete-file';
|
import delFile from './delete-file';
|
||||||
import config from '../../config';
|
import config from '../../config';
|
||||||
|
import { getDriveFileThumbnailBucket } from '../../models/drive-file-thumbnail';
|
||||||
|
|
||||||
const log = debug('misskey:drive:add-file');
|
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> {
|
async function save(path: string, name: string, type: string, hash: string, size: number, metadata: any): Promise<IDriveFile> {
|
||||||
|
let thumbnail: Buffer;
|
||||||
|
|
||||||
|
if (['image/jpeg', 'image/png', 'image/webp'].includes(type)) {
|
||||||
|
thumbnail = await sharp(path)
|
||||||
|
.resize(500)
|
||||||
|
.jpeg({
|
||||||
|
quality: 70,
|
||||||
|
progressive: true
|
||||||
|
})
|
||||||
|
.toBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
if (config.drive && config.drive.storage == 'minio') {
|
if (config.drive && config.drive.storage == 'minio') {
|
||||||
const minio = new Minio.Client(config.drive.config);
|
const minio = new Minio.Client(config.drive.config);
|
||||||
const id = uuid.v4();
|
const id = uuid.v4();
|
||||||
const obj = `${config.drive.prefix}/${id}`;
|
const obj = `${config.drive.prefix}/${id}`;
|
||||||
|
const thumbnailObj = `${obj}-thumbnail`;
|
||||||
|
|
||||||
const baseUrl = config.drive.baseUrl
|
const baseUrl = config.drive.baseUrl
|
||||||
|| `${ config.drive.config.secure ? 'https' : 'http' }://${ config.drive.config.endPoint }${ config.drive.config.port ? ':' + config.drive.config.port : '' }/${ config.drive.bucket }`;
|
|| `${ config.drive.config.secure ? 'https' : 'http' }://${ config.drive.config.endPoint }${ config.drive.config.port ? ':' + config.drive.config.port : '' }/${ config.drive.bucket }`;
|
||||||
|
|
||||||
await minio.putObject(config.drive.bucket, obj, readable, size, {
|
await minio.putObject(config.drive.bucket, obj, fs.createReadStream(path), size, {
|
||||||
'Content-Type': type,
|
'Content-Type': type,
|
||||||
'Cache-Control': 'max-age=31536000, immutable'
|
'Cache-Control': 'max-age=31536000, immutable'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (thumbnail) {
|
||||||
|
await minio.putObject(config.drive.bucket, thumbnailObj, fs.createReadStream(path), size, {
|
||||||
|
'Content-Type': 'image/jpeg',
|
||||||
|
'Cache-Control': 'max-age=31536000, immutable'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Object.assign(metadata, {
|
Object.assign(metadata, {
|
||||||
withoutChunks: true,
|
withoutChunks: true,
|
||||||
storage: 'minio',
|
storage: 'minio',
|
||||||
storageProps: {
|
storageProps: {
|
||||||
id: id
|
id: id
|
||||||
},
|
},
|
||||||
url: `${ baseUrl }/${ obj }`
|
url: `${ baseUrl }/${ obj }`,
|
||||||
|
thumbnailUrl: thumbnail ? `${ baseUrl }/${ thumbnailObj }` : null
|
||||||
});
|
});
|
||||||
|
|
||||||
const file = await DriveFile.insert({
|
const file = await DriveFile.insert({
|
||||||
@ -57,12 +79,37 @@ async function save(readable: stream.Readable, name: string, type: string, hash:
|
|||||||
// Get MongoDB GridFS bucket
|
// Get MongoDB GridFS bucket
|
||||||
const bucket = await getDriveFileBucket();
|
const bucket = await getDriveFileBucket();
|
||||||
|
|
||||||
return new Promise<IDriveFile>((resolve, reject) => {
|
const file = await new Promise<IDriveFile>((resolve, reject) => {
|
||||||
const writeStream = bucket.openUploadStream(name, { contentType: type, metadata });
|
const writeStream = bucket.openUploadStream(name, {
|
||||||
|
contentType: type,
|
||||||
|
metadata
|
||||||
|
});
|
||||||
|
|
||||||
writeStream.once('finish', resolve);
|
writeStream.once('finish', resolve);
|
||||||
writeStream.on('error', reject);
|
writeStream.on('error', reject);
|
||||||
readable.pipe(writeStream);
|
|
||||||
|
fs.createReadStream(path).pipe(writeStream);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (thumbnail) {
|
||||||
|
const thumbnailBucket = await getDriveFileThumbnailBucket();
|
||||||
|
|
||||||
|
await new Promise<IDriveFile>((resolve, reject) => {
|
||||||
|
const writeStream = thumbnailBucket.openUploadStream(name, {
|
||||||
|
contentType: 'image/jpeg',
|
||||||
|
metadata: {
|
||||||
|
originalId: file._id
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
writeStream.once('finish', resolve);
|
||||||
|
writeStream.on('error', reject);
|
||||||
|
|
||||||
|
fs.createReadStream(path).pipe(writeStream);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return file;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -321,7 +368,7 @@ export default async function(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
driveFile = await (save(fs.createReadStream(path), detectedName, mime, hash, size, metadata));
|
driveFile = await (save(path, detectedName, mime, hash, size, metadata));
|
||||||
}
|
}
|
||||||
|
|
||||||
log(`drive file has been created ${driveFile._id}`);
|
log(`drive file has been created ${driveFile._id}`);
|
||||||
|
Loading…
Reference in New Issue
Block a user