This commit is contained in:
syuilo 2017-03-03 19:33:14 +09:00
parent 73ac13a274
commit e2461a9314
14 changed files with 122 additions and 242 deletions

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import DriveFile from '../../models/drive-file'; import DriveFile from '../../models/drive-file';
import serialize from '../../serializers/drive-file'; import serialize from '../../serializers/drive-file';
@ -19,33 +19,25 @@ module.exports = (params, user, app) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'limit' parameter // Get 'limit' parameter
let limit = params.limit; const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
if (limit !== undefined && limit !== null) { if (limitErr) return rej('invalid limit param');
limit = parseInt(limit, 10);
// From 1 to 100 // Get 'since_id' parameter
if (!(1 <= limit && limit <= 100)) { const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed();
return rej('invalid limit range'); if (sinceIdErr) return rej('invalid since_id param');
}
} else {
limit = 10;
}
const since = params.since_id || null; // Get 'max_id' parameter
const max = params.max_id || null; const [maxId, maxIdErr] = it(params.max_id).expect.id().qed();
if (maxIdErr) return rej('invalid max_id param');
// Check if both of since_id and max_id is specified // Check if both of since_id and max_id is specified
if (since !== null && max !== null) { if (sinceId !== null && maxId !== null) {
return rej('cannot set since_id and max_id'); return rej('cannot set since_id and max_id');
} }
// Get 'folder_id' parameter // Get 'folder_id' parameter
let folder = params.folder_id; const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed();
if (folder === undefined || folder === null) { if (folderIdErr) return rej('invalid folder_id param');
folder = null;
} else {
folder = new mongo.ObjectID(folder);
}
// Construct query // Construct query
const sort = { const sort = {
@ -53,16 +45,16 @@ module.exports = (params, user, app) =>
}; };
const query = { const query = {
user_id: user._id, user_id: user._id,
folder_id: folder folder_id: folderId
}; } as any;
if (since !== null) { if (sinceId) {
sort._id = 1; sort._id = 1;
query._id = { query._id = {
$gt: new mongo.ObjectID(since) $gt: sinceId
}; };
} else if (max !== null) { } else if (maxId) {
query._id = { query._id = {
$lt: new mongo.ObjectID(max) $lt: maxId
}; };
} }

View File

@ -4,10 +4,8 @@
* Module dependencies * Module dependencies
*/ */
import * as fs from 'fs'; import * as fs from 'fs';
import * as mongo from 'mongodb'; import it from '../../../it';
import File from '../../../models/drive-file';
import { validateFileName } from '../../../models/drive-file'; import { validateFileName } from '../../../models/drive-file';
import User from '../../../models/user';
import serialize from '../../../serializers/drive-file'; import serialize from '../../../serializers/drive-file';
import create from '../../../common/add-file-to-drive'; import create from '../../../common/add-file-to-drive';
@ -45,15 +43,11 @@ module.exports = (file, params, user) =>
} }
// Get 'folder_id' parameter // Get 'folder_id' parameter
let folder = params.folder_id; const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed();
if (folder === undefined || folder === null) { if (folderIdErr) return rej('invalid folder_id param');
folder = null;
} else {
folder = new mongo.ObjectID(folder);
}
// Create file // Create file
const driveFile = await create(user, buffer, name, null, folder); const driveFile = await create(user, buffer, name, null, folderId);
// Serialize // Serialize
const fileObj = await serialize(driveFile); const fileObj = await serialize(driveFile);

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../../it';
import DriveFile from '../../../models/drive-file'; import DriveFile from '../../../models/drive-file';
import serialize from '../../../serializers/drive-file'; import serialize from '../../../serializers/drive-file';
@ -18,25 +18,19 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'name' parameter // Get 'name' parameter
const name = params.name; const [name, nameErr] = it(params.name).expect.string().required().qed();
if (name === undefined || name === null) { if (nameErr) return rej('invalid name param');
return rej('name is required');
}
// Get 'folder_id' parameter // Get 'folder_id' parameter
let folder = params.folder_id; const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed();
if (folder === undefined || folder === null) { if (folderIdErr) return rej('invalid folder_id param');
folder = null;
} else {
folder = new mongo.ObjectID(folder);
}
// Issue query // Issue query
const files = await DriveFile const files = await DriveFile
.find({ .find({
name: name, name: name,
user_id: user._id, user_id: user._id,
folder_id: folder folder_id: folderId
}, { }, {
fields: { fields: {
data: false data: false

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../../it';
import DriveFile from '../../../models/drive-file'; import DriveFile from '../../../models/drive-file';
import serialize from '../../../serializers/drive-file'; import serialize from '../../../serializers/drive-file';
@ -18,14 +18,13 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'file_id' parameter // Get 'file_id' parameter
const fileId = params.file_id; const [fileId, fileIdErr] = it(params.file_id).expect.id().required().qed();
if (fileId === undefined || fileId === null) { if (fileIdErr) return rej('invalid file_id param');
return rej('file_id is required');
}
// Fetch file
const file = await DriveFile const file = await DriveFile
.findOne({ .findOne({
_id: new mongo.ObjectID(fileId), _id: fileId,
user_id: user._id user_id: user._id
}, { }, {
fields: { fields: {

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../../it';
import DriveFolder from '../../../models/drive-folder'; import DriveFolder from '../../../models/drive-folder';
import DriveFile from '../../../models/drive-file'; import DriveFile from '../../../models/drive-file';
import { validateFileName } from '../../../models/drive-file'; import { validateFileName } from '../../../models/drive-file';
@ -21,19 +21,13 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'file_id' parameter // Get 'file_id' parameter
const fileId = params.file_id; const [fileId, fileIdErr] = it(params.file_id).expect.id().required().qed();
if (fileId === undefined || fileId === null) { if (fileIdErr) return rej('invalid file_id param');
return rej('file_id is required');
}
// Validate id
if (!mongo.ObjectID.isValid(fileId)) {
return rej('incorrect file_id');
}
// Fetch file
const file = await DriveFile const file = await DriveFile
.findOne({ .findOne({
_id: new mongo.ObjectID(fileId), _id: fileId,
user_id: user._id user_id: user._id
}, { }, {
fields: { fields: {
@ -46,29 +40,19 @@ module.exports = (params, user) =>
} }
// Get 'name' parameter // Get 'name' parameter
let name = params.name; const [name, nameErr] = it(params.name).expect.string().validate(validateFileName).qed();
if (name) { if (nameErr) return rej('invalid name param');
name = name.trim(); if (name) file.name = name;
if (validateFileName(name)) {
file.name = name;
} else {
return rej('invalid file name');
}
}
// Get 'folder_id' parameter // Get 'folder_id' parameter
let folderId = params.folder_id; const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().qed();
if (folderIdErr) return rej('invalid folder_id param');
if (folderId !== undefined) { if (folderId !== undefined) {
if (folderId === null) { if (folderId === null) {
file.folder_id = null; file.folder_id = null;
} else { } else {
// Validate id // Fetch folder
if (!mongo.ObjectID.isValid(folderId)) {
return rej('incorrect folder_id');
}
folderId = new mongo.ObjectID(folderId);
const folder = await DriveFolder const folder = await DriveFolder
.findOne({ .findOne({
_id: folderId, _id: folderId,

View File

@ -5,10 +5,8 @@
*/ */
import * as URL from 'url'; import * as URL from 'url';
const download = require('download'); const download = require('download');
import * as mongo from 'mongodb'; import it from '../../../it';
import File from '../../../models/drive-file';
import { validateFileName } from '../../../models/drive-file'; import { validateFileName } from '../../../models/drive-file';
import User from '../../../models/user';
import serialize from '../../../serializers/drive-file'; import serialize from '../../../serializers/drive-file';
import create from '../../../common/add-file-to-drive'; import create from '../../../common/add-file-to-drive';
@ -24,10 +22,8 @@ module.exports = (params, user) =>
{ {
// Get 'url' parameter // Get 'url' parameter
// TODO: Validate this url // TODO: Validate this url
const url = params.url; const [url, urlErr] = it(params.url).expect.string().required().qed();
if (url == null) { if (urlErr) return rej('invalid url param');
return rej('url is required');
}
let name = URL.parse(url).pathname.split('/').pop(); let name = URL.parse(url).pathname.split('/').pop();
if (!validateFileName(name)) { if (!validateFileName(name)) {
@ -35,18 +31,14 @@ module.exports = (params, user) =>
} }
// Get 'folder_id' parameter // Get 'folder_id' parameter
let folder = params.folder_id; const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed();
if (folder === undefined || folder === null) { if (folderIdErr) return rej('invalid folder_id param');
folder = null;
} else {
folder = new mongo.ObjectID(folder);
}
// Download file // Download file
const data = await download(url); const data = await download(url);
// Create file // Create file
const driveFile = await create(user, data, name, null, folder); const driveFile = await create(user, data, name, null, folderId);
// Serialize // Serialize
const fileObj = await serialize(driveFile); const fileObj = await serialize(driveFile);

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import DriveFolder from '../../models/drive-folder'; import DriveFolder from '../../models/drive-folder';
import serialize from '../../serializers/drive-folder'; import serialize from '../../serializers/drive-folder';
@ -19,33 +19,25 @@ module.exports = (params, user, app) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'limit' parameter // Get 'limit' parameter
let limit = params.limit; const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
if (limit !== undefined && limit !== null) { if (limitErr) return rej('invalid limit param');
limit = parseInt(limit, 10);
// From 1 to 100 // Get 'since_id' parameter
if (!(1 <= limit && limit <= 100)) { const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed();
return rej('invalid limit range'); if (sinceIdErr) return rej('invalid since_id param');
}
} else {
limit = 10;
}
const since = params.since_id || null; // Get 'max_id' parameter
const max = params.max_id || null; const [maxId, maxIdErr] = it(params.max_id).expect.id().qed();
if (maxIdErr) return rej('invalid max_id param');
// Check if both of since_id and max_id is specified // Check if both of since_id and max_id is specified
if (since !== null && max !== null) { if (sinceId !== null && maxId !== null) {
return rej('cannot set since_id and max_id'); return rej('cannot set since_id and max_id');
} }
// Get 'folder_id' parameter // Get 'folder_id' parameter
let folder = params.folder_id; const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed();
if (folder === undefined || folder === null) { if (folderIdErr) return rej('invalid folder_id param');
folder = null;
} else {
folder = new mongo.ObjectID(folder);
}
// Construct query // Construct query
const sort = { const sort = {
@ -53,16 +45,16 @@ module.exports = (params, user, app) =>
}; };
const query = { const query = {
user_id: user._id, user_id: user._id,
parent_id: folder parent_id: folderId
}; } as any;
if (since !== null) { if (sinceId) {
sort._id = 1; sort._id = 1;
query._id = { query._id = {
$gt: new mongo.ObjectID(since) $gt: sinceId
}; };
} else if (max !== null) { } else if (maxId) {
query._id = { query._id = {
$lt: new mongo.ObjectID(max) $lt: maxId
}; };
} }

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../../it';
import DriveFolder from '../../../models/drive-folder'; import DriveFolder from '../../../models/drive-folder';
import { isValidFolderName } from '../../../models/drive-folder'; import { isValidFolderName } from '../../../models/drive-folder';
import serialize from '../../../serializers/drive-folder'; import serialize from '../../../serializers/drive-folder';
@ -20,33 +20,17 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'name' parameter // Get 'name' parameter
let name = params.name; const [name, nameErr] = it(params.name).expect.string().validate(isValidFolderName).default('無題のフォルダー').qed();
if (name !== undefined && name !== null) { if (nameErr) return rej('invalid name param');
name = name.trim();
if (name.length === 0) {
name = null;
} else if (!isValidFolderName(name)) {
return rej('invalid name');
}
} else {
name = null;
}
if (name == null) { // Get 'parent_id' parameter
name = '無題のフォルダー'; const [parentId, parentIdErr] = it(params.parent_id).expect.nullable.id().default(null).qed();
} if (parentIdErr) return rej('invalid parent_id param');
// Get 'folder_id' parameter
let parentId = params.folder_id;
if (parentId === undefined || parentId === null) {
parentId = null;
} else {
parentId = new mongo.ObjectID(parentId);
}
// If the parent folder is specified // If the parent folder is specified
let parent = null; let parent = null;
if (parentId !== null) { if (parentId) {
// Fetch parent folder
parent = await DriveFolder parent = await DriveFolder
.findOne({ .findOne({
_id: parentId, _id: parentId,
@ -54,7 +38,7 @@ module.exports = (params, user) =>
}); });
if (parent === null) { if (parent === null) {
return reject('parent-not-found'); return rej('parent-not-found');
} }
} }

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../../it';
import DriveFolder from '../../../models/drive-folder'; import DriveFolder from '../../../models/drive-folder';
import serialize from '../../../serializers/drive-folder'; import serialize from '../../../serializers/drive-folder';
@ -18,18 +18,12 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'name' parameter // Get 'name' parameter
const name = params.name; const [name, nameErr] = it(params.name).expect.string().required().qed();
if (name === undefined || name === null) { if (nameErr) return rej('invalid name param');
return rej('name is required');
}
// Get 'parent_id' parameter // Get 'parent_id' parameter
let parentId = params.parent_id; const [parentId, parentIdErr] = it(params.parent_id).expect.id().qed();
if (parentId === undefined || parentId === null) { if (parentIdErr) return rej('invalid parent_id param');
parentId = null;
} else {
parentId = new mongo.ObjectID(parentId);
}
// Issue query // Issue query
const folders = await DriveFolder const folders = await DriveFolder

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../../it';
import DriveFolder from '../../../models/drive-folder'; import DriveFolder from '../../../models/drive-folder';
import serialize from '../../../serializers/drive-folder'; import serialize from '../../../serializers/drive-folder';
@ -18,15 +18,13 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'folder_id' parameter // Get 'folder_id' parameter
const folderId = params.folder_id; const [folderId, folderIdErr] = it(params.folder_id).expect.id().required().qed();
if (folderId === undefined || folderId === null) { if (folderIdErr) return rej('invalid folder_id param');
return rej('folder_id is required');
}
// Get folder // Get folder
const folder = await DriveFolder const folder = await DriveFolder
.findOne({ .findOne({
_id: new mongo.ObjectID(folderId), _id: folderId,
user_id: user._id user_id: user._id
}); });

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../../it';
import DriveFolder from '../../../models/drive-folder'; import DriveFolder from '../../../models/drive-folder';
import { isValidFolderName } from '../../../models/drive-folder'; import { isValidFolderName } from '../../../models/drive-folder';
import serialize from '../../../serializers/drive-file'; import serialize from '../../../serializers/drive-file';
@ -20,20 +20,13 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'folder_id' parameter // Get 'folder_id' parameter
const folderId = params.folder_id; const [folderId, folderIdErr] = it(params.folder_id).expect.id().required().qed();
if (folderId === undefined || folderId === null) { if (folderIdErr) return rej('invalid folder_id param');
return rej('folder_id is required');
}
// Validate id
if (!mongo.ObjectID.isValid(folderId)) {
return rej('incorrect folder_id');
}
// Fetch folder // Fetch folder
const folder = await DriveFolder const folder = await DriveFolder
.findOne({ .findOne({
_id: new mongo.ObjectID(folderId), _id: folderId,
user_id: user._id user_id: user._id
}); });
@ -42,29 +35,17 @@ module.exports = (params, user) =>
} }
// Get 'name' parameter // Get 'name' parameter
let name = params.name; const [name, nameErr] = it(params.name).expect.string().validate(isValidFolderName).qed();
if (name) { if (nameErr) return rej('invalid name param');
name = name.trim(); if (name) folder.name = name;
if (isValidFolderName(name)) {
folder.name = name;
} else {
return rej('invalid folder name');
}
}
// Get 'parent_id' parameter // Get 'parent_id' parameter
let parentId = params.parent_id; const [parentId, parentIdErr] = it(params.parent_id).expect.nullable.id().qed();
if (parentIdErr) return rej('invalid parent_id param');
if (parentId !== undefined) { if (parentId !== undefined) {
if (parentId === null) { if (parentId === null) {
folder.parent_id = null; folder.parent_id = null;
} else { } else {
// Validate id
if (!mongo.ObjectID.isValid(parentId)) {
return rej('incorrect parent_id');
}
parentId = new mongo.ObjectID(parentId);
// Get parent folder // Get parent folder
const parent = await DriveFolder const parent = await DriveFolder
.findOne({ .findOne({

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import DriveFile from '../../models/drive-file'; import DriveFile from '../../models/drive-file';
import serialize from '../../serializers/drive-file'; import serialize from '../../serializers/drive-file';
@ -18,35 +18,25 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'limit' parameter // Get 'limit' parameter
let limit = params.limit; const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
if (limit !== undefined && limit !== null) { if (limitErr) return rej('invalid limit param');
limit = parseInt(limit, 10);
// From 1 to 100 // Get 'since_id' parameter
if (!(1 <= limit && limit <= 100)) { const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed();
return rej('invalid limit range'); if (sinceIdErr) return rej('invalid since_id param');
}
} else {
limit = 10;
}
const since = params.since_id || null; // Get 'max_id' parameter
const max = params.max_id || null; const [maxId, maxIdErr] = it(params.max_id).expect.id().qed();
if (maxIdErr) return rej('invalid max_id param');
// Check if both of since_id and max_id is specified // Check if both of since_id and max_id is specified
if (since !== null && max !== null) { if (sinceId !== null && maxId !== null) {
return rej('cannot set since_id and max_id'); return rej('cannot set since_id and max_id');
} }
// Get 'type' parameter // Get 'type' parameter
let type = params.type; const [type, typeErr] = it(params.type).expect.string().match(/^[a-zA-Z\/\-\*]+$/).qed();
if (type === undefined || type === null) { if (typeErr) return rej('invalid type param');
type = null;
} else if (!/^[a-zA-Z\/\-\*]+$/.test(type)) {
return rej('invalid type format');
} else {
type = new RegExp(`^${type.replace(/\*/g, '.+?')}$`);
}
// Construct query // Construct query
const sort = { const sort = {
@ -54,19 +44,19 @@ module.exports = (params, user) =>
}; };
const query = { const query = {
user_id: user._id user_id: user._id
}; } as any;
if (since !== null) { if (sinceId) {
sort._id = 1; sort._id = 1;
query._id = { query._id = {
$gt: new mongo.ObjectID(since) $gt: sinceId
}; };
} else if (max !== null) { } else if (maxId) {
query._id = { query._id = {
$lt: new mongo.ObjectID(max) $lt: maxId
}; };
} }
if (type !== null) { if (type !== null) {
query.type = type; query.type = new RegExp(`^${type.replace(/\*/g, '.+?')}$`);
} }
// Issue query // Issue query

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import User from '../../models/user'; import User from '../../models/user';
import Following from '../../models/following'; import Following from '../../models/following';
import notify from '../../common/notify'; import notify from '../../common/notify';
@ -23,15 +23,8 @@ module.exports = (params, user) =>
const follower = user; const follower = user;
// Get 'user_id' parameter // Get 'user_id' parameter
let userId = params.user_id; const [userId, userIdErr] = it(params.user_id, 'id', true);
if (userId === undefined || userId === null) { if (userIdErr) return rej('invalid user_id param');
return rej('user_id is required');
}
// Validate id
if (!mongo.ObjectID.isValid(userId)) {
return rej('incorrect user_id');
}
// 自分自身 // 自分自身
if (user._id.equals(userId)) { if (user._id.equals(userId)) {
@ -40,7 +33,7 @@ module.exports = (params, user) =>
// Get followee // Get followee
const followee = await User.findOne({ const followee = await User.findOne({
_id: new mongo.ObjectID(userId) _id: userId
}, { }, {
fields: { fields: {
data: false, data: false,

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import User from '../../models/user'; import User from '../../models/user';
import Following from '../../models/following'; import Following from '../../models/following';
import event from '../../event'; import event from '../../event';
@ -22,15 +22,8 @@ module.exports = (params, user) =>
const follower = user; const follower = user;
// Get 'user_id' parameter // Get 'user_id' parameter
let userId = params.user_id; const [userId, userIdErr] = it(params.user_id, 'id', true);
if (userId === undefined || userId === null) { if (userIdErr) return rej('invalid user_id param');
return rej('user_id is required');
}
// Validate id
if (!mongo.ObjectID.isValid(userId)) {
return rej('incorrect user_id');
}
// Check if the followee is yourself // Check if the followee is yourself
if (user._id.equals(userId)) { if (user._id.equals(userId)) {
@ -39,7 +32,7 @@ module.exports = (params, user) =>
// Get followee // Get followee
const followee = await User.findOne({ const followee = await User.findOne({
_id: new mongo.ObjectID(userId) _id: userId
}, { }, {
fields: { fields: {
data: false, data: false,