add-file-to-drive - 見通しを良くする
This commit is contained in:
		| @@ -32,29 +32,18 @@ const addToGridFS = (name: string, readable: stream.Readable, type: string, meta | |||||||
| 			readable.pipe(writeStream); | 			readable.pipe(writeStream); | ||||||
| 		})); | 		})); | ||||||
|  |  | ||||||
| /** | const addFile = async ( | ||||||
|  * Add file to drive |  | ||||||
|  * |  | ||||||
|  * @param user User who wish to add file |  | ||||||
|  * @param file File path or readableStream |  | ||||||
|  * @param comment Comment |  | ||||||
|  * @param type File type |  | ||||||
|  * @param folderId Folder ID |  | ||||||
|  * @param force If set to true, forcibly upload the file even if there is a file with the same hash. |  | ||||||
|  * @return Object that represents added file |  | ||||||
|  */ |  | ||||||
| export default ( |  | ||||||
| 	user: any, | 	user: any, | ||||||
| 	file: string | stream.Readable, | 	file: string | stream.Readable, | ||||||
| 	name: string = null, | 	name: string = null, | ||||||
| 	comment: string = null, | 	comment: string = null, | ||||||
| 	folderId: mongodb.ObjectID = null, | 	folderId: mongodb.ObjectID = null, | ||||||
| 	force: boolean = false | 	force: boolean = false | ||||||
| ) => new Promise<any>((resolve, reject) => { | ) => { | ||||||
| 	log(`registering ${name} (user: ${user.username})`); | 	log(`registering ${name} (user: ${user.username})`); | ||||||
|  |  | ||||||
| 	// Get file path | 	// Get file path | ||||||
| 	new Promise((res: (v: string) => void, rej) => { | 	const path = await new Promise((res: (v: string) => void, rej) => { | ||||||
| 		if (typeof file === 'string') { | 		if (typeof file === 'string') { | ||||||
| 			res(file); | 			res(file); | ||||||
| 			return; | 			return; | ||||||
| @@ -75,10 +64,10 @@ export default ( | |||||||
| 				.catch(rej); | 				.catch(rej); | ||||||
| 		} | 		} | ||||||
| 		rej(new Error('un-compatible file.')); | 		rej(new Error('un-compatible file.')); | ||||||
| 	}) | 	}); | ||||||
|  |  | ||||||
| 	// Calculate hash, get content type and get file size | 	// Calculate hash, get content type and get file size | ||||||
| 		.then(path => Promise.all([ | 	const [hash, [mime, ext], size] = await Promise.all([ | ||||||
| 			path, |  | ||||||
| 		// hash | 		// hash | ||||||
| 		((): Promise<string> => new Promise((res, rej) => { | 		((): Promise<string> => new Promise((res, rej) => { | ||||||
| 			const readable = fs.createReadStream(path); | 			const readable = fs.createReadStream(path); | ||||||
| @@ -115,8 +104,8 @@ export default ( | |||||||
| 				res(stats.size); | 				res(stats.size); | ||||||
| 			}); | 			}); | ||||||
| 		}))() | 		}))() | ||||||
| 		])) | 	]); | ||||||
| 		.then(async ([path, hash, [mime, ext], size]) => { |  | ||||||
| 	log(`hash: ${hash}, mime: ${mime}, ext: ${ext}, size: ${size}`); | 	log(`hash: ${hash}, mime: ${mime}, ext: ${ext}, size: ${size}`); | ||||||
|  |  | ||||||
| 	// detect name | 	// detect name | ||||||
| @@ -131,7 +120,7 @@ export default ( | |||||||
|  |  | ||||||
| 		if (much !== null) { | 		if (much !== null) { | ||||||
| 			log('file with same hash is found'); | 			log('file with same hash is found'); | ||||||
| 					return resolve(much); | 			return much; | ||||||
| 		} else { | 		} else { | ||||||
| 			log('file with same hash is not found'); | 			log('file with same hash is not found'); | ||||||
| 		} | 		} | ||||||
| @@ -209,7 +198,21 @@ export default ( | |||||||
| 		comment: comment, | 		comment: comment, | ||||||
| 		properties: properties | 		properties: properties | ||||||
| 	}); | 	}); | ||||||
| 		}) | }; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Add file to drive | ||||||
|  |  * | ||||||
|  |  * @param user User who wish to add file | ||||||
|  |  * @param file File path or readableStream | ||||||
|  |  * @param comment Comment | ||||||
|  |  * @param type File type | ||||||
|  |  * @param folderId Folder ID | ||||||
|  |  * @param force If set to true, forcibly upload the file even if there is a file with the same hash. | ||||||
|  |  * @return Object that represents added file | ||||||
|  |  */ | ||||||
|  | export default (user: any, file: string | stream.Readable, ...args) => new Promise<any>((resolve, reject) => { | ||||||
|  | 	addFile(user, file, ...args) | ||||||
| 		.then(file => { | 		.then(file => { | ||||||
| 			log(`drive file has been created ${file._id}`); | 			log(`drive file has been created ${file._id}`); | ||||||
| 			resolve(file); | 			resolve(file); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 otofune
					otofune