Refactor and some fixes
This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Module dependencies
 | 
			
		||||
 */
 | 
			
		||||
import * as fs from 'fs';
 | 
			
		||||
import $ from 'cafy'; import ID from '../../../../../cafy-id';
 | 
			
		||||
import { validateFileName, pack } from '../../../../../models/drive-file';
 | 
			
		||||
import create from '../../../../../services/drive/add-file';
 | 
			
		||||
@@ -32,15 +33,23 @@ module.exports = async (file, params, user): Promise<any> => {
 | 
			
		||||
	const [folderId = null, folderIdErr] = $.type(ID).optional().nullable().get(params.folderId);
 | 
			
		||||
	if (folderIdErr) throw 'invalid folderId param';
 | 
			
		||||
 | 
			
		||||
	function cleanup() {
 | 
			
		||||
		fs.unlink(file.path, () => {});
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	try {
 | 
			
		||||
		// Create file
 | 
			
		||||
		const driveFile = await create(user, file.path, name, null, folderId);
 | 
			
		||||
 | 
			
		||||
		cleanup();
 | 
			
		||||
 | 
			
		||||
		// Serialize
 | 
			
		||||
		return pack(driveFile);
 | 
			
		||||
	} catch (e) {
 | 
			
		||||
		console.error(e);
 | 
			
		||||
 | 
			
		||||
		cleanup();
 | 
			
		||||
 | 
			
		||||
		throw e;
 | 
			
		||||
	}
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user