Merge pull request #915 from syuilo/write-file-use-stream

multer がテンポラリディレクトリにファイルを保存するように変更
This commit is contained in:
syuilo 2017-11-14 05:38:33 +09:00 committed by GitHub
commit 431e24fa99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 15 deletions

1
.gitignore vendored
View File

@ -2,7 +2,6 @@
/.vscode /.vscode
/node_modules /node_modules
/built /built
/uploads
/data /data
npm-debug.log npm-debug.log
*.pem *.pem

View File

@ -40,7 +40,7 @@ const addFile = async (
folderId: mongodb.ObjectID = null, folderId: mongodb.ObjectID = null,
force: boolean = false force: boolean = false
) => { ) => {
log(`registering ${name} (user: ${user.username})`); log(`registering ${name} (user: ${user.username}, path: ${path})`);
// Calculate hash, get content type and get file size // Calculate hash, get content type and get file size
const [hash, [mime, ext], size] = await Promise.all([ const [hash, [mime, ext], size] = await Promise.all([
@ -210,18 +210,19 @@ export default (user: any, file: string | stream.Readable, ...args) => new Promi
.catch(rej); .catch(rej);
} }
rej(new Error('un-compatible file.')); rej(new Error('un-compatible file.'));
}).then(([path, remove]): Promise<any> => new Promise((res, rej) => { })
addFile(user, path, ...args) .then(([path, remove]): Promise<any> => new Promise((res, rej) => {
.then(file => { addFile(user, path, ...args)
res(file); .then(file => {
if (remove) { res(file);
fs.unlink(path, (e) => { if (remove) {
if (e) log(e.stack); fs.unlink(path, (e) => {
}); 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);

View File

@ -40,7 +40,7 @@ app.get('/', (req, res) => {
endpoints.forEach(endpoint => endpoints.forEach(endpoint =>
endpoint.withFile ? endpoint.withFile ?
app.post(`/${endpoint.name}`, app.post(`/${endpoint.name}`,
endpoint.withFile ? multer({ dest: 'uploads/' }).single('file') : null, endpoint.withFile ? multer({ storage: multer.diskStorage({}) }).single('file') : null,
require('./api-handler').default.bind(null, endpoint)) : require('./api-handler').default.bind(null, endpoint)) :
app.post(`/${endpoint.name}`, app.post(`/${endpoint.name}`,
require('./api-handler').default.bind(null, endpoint)) require('./api-handler').default.bind(null, endpoint))