create - バッファを使用しないように

This commit is contained in:
otofune 2017-11-14 04:35:25 +09:00
parent 47f98fbab7
commit 342345cc2f

View file

@ -1,7 +1,6 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as fs from 'fs';
import $ from 'cafy'; import $ from 'cafy';
import { validateFileName } from '../../../models/drive-file'; import { validateFileName } from '../../../models/drive-file';
import serialize from '../../../serializers/drive-file'; import serialize from '../../../serializers/drive-file';
@ -15,15 +14,11 @@ import create from '../../../common/add-file-to-drive';
* @param {any} user * @param {any} user
* @return {Promise<any>} * @return {Promise<any>}
*/ */
module.exports = (file, params, user) => new Promise(async (res, rej) => { module.exports = async (file, params, user): Promise<any> => {
if (file == null) { if (file == null) {
return rej('file is required'); throw 'file is required';
} }
// TODO: 非同期にしたい。Promise対応してないんだろうか...
const buffer = fs.readFileSync(file.path);
fs.unlink(file.path, (err) => { if (err) console.log(err); });
// Get 'name' parameter // Get 'name' parameter
let name = file.originalname; let name = file.originalname;
if (name !== undefined && name !== null) { if (name !== undefined && name !== null) {
@ -33,7 +28,7 @@ module.exports = (file, params, user) => new Promise(async (res, rej) => {
} else if (name === 'blob') { } else if (name === 'blob') {
name = null; name = null;
} else if (!validateFileName(name)) { } else if (!validateFileName(name)) {
return rej('invalid name'); throw 'invalid name';
} }
} else { } else {
name = null; name = null;
@ -41,14 +36,11 @@ module.exports = (file, params, user) => new Promise(async (res, rej) => {
// Get 'folder_id' parameter // Get 'folder_id' parameter
const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$; const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
if (folderIdErr) return rej('invalid folder_id param'); if (folderIdErr) throw 'invalid folder_id param';
// Create file // Create file
const driveFile = await create(user, buffer, name, null, folderId); const driveFile = await create(user, file.path, name, null, folderId);
// Serialize // Serialize
const fileObj = await serialize(driveFile); return serialize(driveFile);
};
// Response
res(fileObj);
});