migration to GridFS's DriveFile

This commit is contained in:
otofune 2017-11-06 15:18:45 +09:00
parent 7e81e0db6a
commit 18b1ef29ad
11 changed files with 66 additions and 70 deletions

View file

@ -154,6 +154,7 @@ export default (
comment: comment, comment: comment,
properties: properties properties: properties
}); });
console.dir(file)
log(`drive file has been created ${file._id}`); log(`drive file has been created ${file._id}`);

View file

@ -14,16 +14,16 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Calculate drive usage // Calculate drive usage
const usage = ((await DriveFile const usage = ((await DriveFile
.aggregate([ .aggregate([
{ $match: { user_id: user._id } }, { $match: { metadata: { user_id: user._id } } },
{ {
$project: { $project: {
datasize: true length: true
} }
}, },
{ {
$group: { $group: {
_id: null, _id: null,
usage: { $sum: '$datasize' } usage: { $sum: '$length' }
} }
} }
]))[0] || { ]))[0] || {

View file

@ -40,8 +40,10 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
_id: -1 _id: -1
}; };
const query = { const query = {
user_id: user._id, metadata: {
folder_id: folderId user_id: user._id,
folder_id: folderId
}
} as any; } as any;
if (sinceId) { if (sinceId) {
sort._id = 1; sort._id = 1;
@ -57,9 +59,6 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
// Issue query // Issue query
const files = await DriveFile const files = await DriveFile
.find(query, { .find(query, {
fields: {
data: false
},
limit: limit, limit: limit,
sort: sort sort: sort
}); });

View file

@ -24,12 +24,10 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Issue query // Issue query
const files = await DriveFile const files = await DriveFile
.find({ .find({
name: name, metadata: {
user_id: user._id, name: name,
folder_id: folderId user_id: user._id,
}, { folder_id: folderId
fields: {
data: false
} }
}); });

View file

@ -21,10 +21,8 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
const file = await DriveFile const file = await DriveFile
.findOne({ .findOne({
_id: fileId, _id: fileId,
user_id: user._id metadata: {
}, { user_id: user._id
fields: {
data: false
} }
}); });

View file

@ -20,25 +20,29 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
const [fileId, fileIdErr] = $(params.file_id).id().$; const [fileId, fileIdErr] = $(params.file_id).id().$;
if (fileIdErr) return rej('invalid file_id param'); if (fileIdErr) return rej('invalid file_id param');
console.dir(user)
// Fetch file // Fetch file
const file = await DriveFile const file = await DriveFile
.findOne({ .findOne({
_id: fileId, _id: fileId,
user_id: user._id metadata: {
}, { user_id: user._id
fields: {
data: false
} }
}); });
console.dir(file)
if (file === null) { if (file === null) {
return rej('file-not-found'); return rej('file-not-found');
} }
const updateQuery: any = {}
// Get 'name' parameter // Get 'name' parameter
const [name, nameErr] = $(params.name).optional.string().pipe(validateFileName).$; const [name, nameErr] = $(params.name).optional.string().pipe(validateFileName).$;
if (nameErr) return rej('invalid name param'); if (nameErr) return rej('invalid name param');
if (name) file.name = name; if (name) updateQuery.name = name;
// Get 'folder_id' parameter // Get 'folder_id' parameter
const [folderId, folderIdErr] = $(params.folder_id).optional.nullable.id().$; const [folderId, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
@ -46,7 +50,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
if (folderId !== undefined) { if (folderId !== undefined) {
if (folderId === null) { if (folderId === null) {
file.folder_id = null; updateQuery.folder_id = null;
} else { } else {
// Fetch folder // Fetch folder
const folder = await DriveFolder const folder = await DriveFolder
@ -59,19 +63,20 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
return rej('folder-not-found'); return rej('folder-not-found');
} }
file.folder_id = folder._id; updateQuery.folder_id = folder._id;
} }
} }
DriveFile.update(file._id, { const updated = await DriveFile.update(file._id, {
$set: { $set: { metadata: updateQuery }
name: file.name,
folder_id: file.folder_id
}
}); });
console.dir(updated)
// Serialize // Serialize
const fileObj = await serialize(file); const fileObj = await serialize(updated);
console.dir(fileObj)
// Response // Response
res(fileObj); res(fileObj);

View file

@ -54,9 +54,9 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
if (fileId !== undefined) { if (fileId !== undefined) {
file = await DriveFile.findOne({ file = await DriveFile.findOne({
_id: fileId, _id: fileId,
user_id: user._id metadata: {
}, { user_id: user._id
data: false }
}); });
if (file === null) { if (file === null) {

View file

@ -44,9 +44,9 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
// SELECT _id // SELECT _id
const entity = await DriveFile.findOne({ const entity = await DriveFile.findOne({
_id: mediaId, _id: mediaId,
user_id: user._id metadata: {
}, { user_id: user._id
_id: true }
}); });
if (entity === null) { if (entity === null) {

View file

@ -2,6 +2,7 @@
* Module dependencies * Module dependencies
*/ */
import $ from 'cafy'; import $ from 'cafy';
import rap from '@prezzemolo/rap';
import Post from '../../models/post'; import Post from '../../models/post';
import ChannelWatching from '../../models/channel-watching'; import ChannelWatching from '../../models/channel-watching';
import getFriends from '../../common/get-friends'; import getFriends from '../../common/get-friends';
@ -33,14 +34,15 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
return rej('cannot set since_id and max_id'); return rej('cannot set since_id and max_id');
} }
// ID list of the user itself and other users who the user follows const { followingIds, watchChannelIds } = await rap({
const followingIds = await getFriends(user._id); // ID list of the user itself and other users who the user follows
followingIds: getFriends(user._id),
// Watchしているチャンネルを取得 // Watchしているチャンネルを取得
const watches = await ChannelWatching.find({ watchChannelIds: ChannelWatching.find({
user_id: user._id, user_id: user._id,
// 削除されたドキュメントは除く // 削除されたドキュメントは除く
deleted_at: { $exists: false } deleted_at: { $exists: false }
}).then(watches => watches.map(w => w.channel_id))
}); });
//#region Construct query //#region Construct query
@ -65,7 +67,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
}, { }, {
// Watchしているチャンネルへの投稿 // Watchしているチャンネルへの投稿
channel_id: { channel_id: {
$in: watches.map(w => w.channel_id) $in: watchChannelIds
} }
}] }]
} as any; } as any;
@ -90,7 +92,5 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
}); });
// Serialize // Serialize
res(await Promise.all(timeline.map(async post => res(Promise.all(timeline.map(post => serialize(post, user))));
await serialize(post, user)
)));
}); });

View file

@ -31,44 +31,37 @@ export default (
if (mongo.ObjectID.prototype.isPrototypeOf(file)) { if (mongo.ObjectID.prototype.isPrototypeOf(file)) {
_file = await DriveFile.findOne({ _file = await DriveFile.findOne({
_id: file _id: file
}, { });
fields: {
data: false
}
});
} else if (typeof file === 'string') { } else if (typeof file === 'string') {
_file = await DriveFile.findOne({ _file = await DriveFile.findOne({
_id: new mongo.ObjectID(file) _id: new mongo.ObjectID(file)
}, { });
fields: {
data: false
}
});
} else { } else {
_file = deepcopy(file); _file = deepcopy(file);
} }
// Rename _id to id // rendered target
_file.id = _file._id; let _target: any = {};
delete _file._id;
delete _file.data; _target.id = _file._id;
_file.url = `${config.drive_url}/${_file.id}/${encodeURIComponent(_file.name)}`; _target = Object.assign(_target, _file.metadata);
if (opts.detail && _file.folder_id) { _target.url = `${config.drive_url}/${_target.id}/${encodeURIComponent(_target.name)}`;
if (opts.detail && _target.folder_id) {
// Populate folder // Populate folder
_file.folder = await serializeDriveFolder(_file.folder_id, { _target.folder = await serializeDriveFolder(_target.folder_id, {
detail: true detail: true
}); });
} }
if (opts.detail && _file.tags) { if (opts.detail && _target.tags) {
// Populate tags // Populate tags
_file.tags = await _file.tags.map(async (tag: any) => _target.tags = await _target.tags.map(async (tag: any) =>
await serializeDriveTag(tag) await serializeDriveTag(tag)
); );
} }
resolve(_file); resolve(_target);
}); });

View file

@ -44,7 +44,9 @@ const self = (
}); });
const childFilesCount = await DriveFile.count({ const childFilesCount = await DriveFile.count({
folder_id: _folder.id metadata: {
folder_id: _folder.id
}
}); });
_folder.folders_count = childFoldersCount; _folder.folders_count = childFoldersCount;