1
0
mirror of https://github.com/MisskeyIO/misskey synced 2024-12-29 05:58:15 +09:00
MisskeyIO/src/server/api/endpoints/drive/files/create.ts

56 lines
1.1 KiB
TypeScript
Raw Normal View History

2016-12-29 07:49:51 +09:00
/**
* Module dependencies
*/
2018-05-25 18:41:49 +09:00
import * as fs from 'fs';
2018-04-24 18:13:06 +09:00
import $ from 'cafy'; import ID from '../../../../../cafy-id';
2018-03-29 20:32:18 +09:00
import { validateFileName, pack } from '../../../../../models/drive-file';
2018-04-08 04:02:12 +09:00
import create from '../../../../../services/drive/add-file';
2016-12-29 07:49:51 +09:00
/**
* Create a file
*/
module.exports = async (file, params, user): Promise<any> => {
2017-02-12 01:39:27 +09:00
if (file == null) {
throw 'file is required';
2017-02-12 01:39:27 +09:00
}
2016-12-29 07:49:51 +09:00
// Get 'name' parameter
let name = file.originalname;
if (name !== undefined && name !== null) {
name = name.trim();
if (name.length === 0) {
name = null;
} else if (name === 'blob') {
name = null;
} else if (!validateFileName(name)) {
throw 'invalid name';
2016-12-29 07:49:51 +09:00
}
} else {
name = null;
}
2018-03-29 14:48:47 +09:00
// Get 'folderId' parameter
2018-05-02 18:06:16 +09:00
const [folderId = null, folderIdErr] = $.type(ID).optional().nullable().get(params.folderId);
2018-03-29 14:48:47 +09:00
if (folderIdErr) throw 'invalid folderId param';
2016-12-29 07:49:51 +09:00
2018-05-25 18:41:49 +09:00
function cleanup() {
fs.unlink(file.path, () => {});
}
2017-12-11 13:33:33 +09:00
try {
// Create file
const driveFile = await create(user, file.path, name, null, folderId);
2016-12-29 07:49:51 +09:00
2018-05-25 18:41:49 +09:00
cleanup();
2017-12-11 13:33:33 +09:00
// Serialize
2018-02-02 08:21:30 +09:00
return pack(driveFile);
2017-12-11 13:33:33 +09:00
} catch (e) {
console.error(e);
2018-05-25 18:41:49 +09:00
cleanup();
2017-12-11 13:33:33 +09:00
throw e;
}
};