mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-10-30 21:14:12 +00:00 
			
		
		
		
	
							parent
							
								
									0f6c5f506a
								
							
						
					
					
						commit
						598500223a
					
				
					 1 changed files with 64 additions and 52 deletions
				
			
		|  | @ -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: { |  | ||||||
| 							_id: null, |  | ||||||
| 							usage: { $sum: '$length' } |  | ||||||
| 						} |  | ||||||
| 					} | 					} | ||||||
| 				]) | 				}, { | ||||||
|  | 					$group: { | ||||||
|  | 						_id: null, | ||||||
|  | 						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; | ||||||
|  | @ -211,41 +224,40 @@ export default (user: any, file: string | stream.Readable, ...args) => new Promi | ||||||
| 		} | 		} | ||||||
| 		rej(new Error('un-compatible file.')); | 		rej(new Error('un-compatible file.')); | ||||||
| 	}) | 	}) | ||||||
| 		.then(([path, remove]): Promise<any> => new Promise((res, rej) => { | 	.then(([path, remove]): Promise<any> => new Promise((res, rej) => { | ||||||
| 			addFile(user, path, ...args) | 		addFile(user, path, ...args) | ||||||
| 				.then(file => { | 			.then(file => { | ||||||
| 					res(file); | 				res(file); | ||||||
| 					if (remove) { | 				if (remove) { | ||||||
| 						fs.unlink(path, (e) => { | 					fs.unlink(path, (e) => { | ||||||
| 							if (e) log(e.stack); | 						if (e) log(e.stack); | ||||||
| 						}); | 					}); | ||||||
| 					} | 				} | ||||||
| 				}) | 			}) | ||||||
| 				.catch(rej); | 			.catch(rej); | ||||||
| 		})) | 	})) | ||||||
| 		.then(file => { | 	.then(file => { | ||||||
| 			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); |  | ||||||
| 
 | 
 | ||||||
| 					// Register to search database
 | 			// Register to search database
 | ||||||
| 					if (config.elasticsearch.enable) { | 			if (config.elasticsearch.enable) { | ||||||
| 						const es = require('../../db/elasticsearch'); | 				const es = require('../../db/elasticsearch'); | ||||||
| 						es.index({ | 				es.index({ | ||||||
| 							index: 'misskey', | 					index: 'misskey', | ||||||
| 							type: 'drive_file', | 					type: 'drive_file', | ||||||
| 							id: file._id.toString(), | 					id: file._id.toString(), | ||||||
| 							body: { | 					body: { | ||||||
| 								name: file.name, | 						name: file.name, | ||||||
| 								user_id: user._id.toString() | 						user_id: user._id.toString() | ||||||
| 							} |  | ||||||
| 						}); |  | ||||||
| 					} | 					} | ||||||
| 				}); | 				}); | ||||||
| 		}) | 			} | ||||||
| 		.catch(reject); | 		}); | ||||||
|  | 	}) | ||||||
|  | 	.catch(reject); | ||||||
| }); | }); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue