<commit_before>import { Buffer } from 'buffer';
import * as fs from 'fs';
import * as stream from 'stream';

import * as mongodb from 'mongodb';
import * as crypto from 'crypto';
import * as debug from 'debug';
import fileType = require('file-type');
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';
import { pack } from '../../models/drive-file';
import event, { publishDriveStream } from '../../stream';
import { isLocalUser, IUser, IRemoteUser } from '../../models/user';
import delFile from './delete-file';
import config from '../../config';

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> {
	if (config.drive && config.drive.storage == 'minio') {
		const minio = new Minio.Client(config.drive.config);
		const id = uuid.v4();
		const obj = `${config.drive.prefix}/${id}`;
		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 }`;
		await minio.putObject(config.drive.bucket, obj, readable, size, { 'Content-Type': type, 'Cache-Control': 'max-age=31536000, immutable' });

		Object.assign(metadata, {
			withoutChunks: true,
			storage: 'minio',
			storageProps: {
				id: id
			},
			url: `${ baseUrl }/${ obj }`
		});

		const file = await DriveFile.insert({
			length: size,
			uploadDate: new Date(),
			md5: hash,
			filename: name,
			metadata: metadata,
			contentType: type
		});

		return file;
	} else {
		// Get MongoDB GridFS bucket<commit_msg>Improve readability<commit_after>import { Buffer } from 'buffer';
import * as fs from 'fs';
import * as stream from 'stream';

import * as mongodb from 'mongodb';
import * as crypto from 'crypto';
import * as debug from 'debug';
import fileType = require('file-type');
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';
import { pack } from '../../models/drive-file';
import event, { publishDriveStream } from '../../stream';
import { isLocalUser, IUser, IRemoteUser } from '../../models/user';
import delFile from './delete-file';
import config from '../../config';

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> {
	if (config.drive && config.drive.storage == 'minio') {
		const minio = new Minio.Client(config.drive.config);
		const id = uuid.v4();
		const obj = `${config.drive.prefix}/${id}`;

		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 }`;

		await minio.putObject(config.drive.bucket, obj, readable, size, {
			'Content-Type': type,
			'Cache-Control': 'max-age=31536000, immutable'
		});

		Object.assign(metadata, {
			withoutChunks: true,
			storage: 'minio',
			storageProps: {
				id: id
			},
			url: `${ baseUrl }/${ obj }`
		});

		const file = await DriveFile.insert({
			length: size,
			uploadDate: new Date(),
			md5: hash,
			filename: name,
			metadata: metadata,
			contentType: type
		});

		return file;
	} else {
		// Get MongoDB GridFS bucket
{'fraction_non_alphanumeric': 0.1726349545697488, 'fraction_numerical': 0.005344735435595938, 'mean_word_length': 3.823453608247423, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2738324', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1185, 'n_words': 336}