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,
properties: properties
});
console.dir(file)
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
const usage = ((await DriveFile
.aggregate([
{ $match: { user_id: user._id } },
{ $match: { metadata: { user_id: user._id } } },
{
$project: {
datasize: true
length: true
}
},
{
$group: {
_id: null,
usage: { $sum: '$datasize' }
usage: { $sum: '$length' }
}
}
]))[0] || {

View File

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

View File

@ -24,12 +24,10 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Issue query
const files = await DriveFile
.find({
metadata: {
name: name,
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
.findOne({
_id: fileId,
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().$;
if (fileIdErr) return rej('invalid file_id param');
console.dir(user)
// Fetch file
const file = await DriveFile
.findOne({
_id: fileId,
metadata: {
user_id: user._id
}, {
fields: {
data: false
}
});
console.dir(file)
if (file === null) {
return rej('file-not-found');
}
const updateQuery: any = {}
// Get 'name' parameter
const [name, nameErr] = $(params.name).optional.string().pipe(validateFileName).$;
if (nameErr) return rej('invalid name param');
if (name) file.name = name;
if (name) updateQuery.name = name;
// Get 'folder_id' parameter
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 === null) {
file.folder_id = null;
updateQuery.folder_id = null;
} else {
// Fetch folder
const folder = await DriveFolder
@ -59,19 +63,20 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
return rej('folder-not-found');
}
file.folder_id = folder._id;
updateQuery.folder_id = folder._id;
}
}
DriveFile.update(file._id, {
$set: {
name: file.name,
folder_id: file.folder_id
}
const updated = await DriveFile.update(file._id, {
$set: { metadata: updateQuery }
});
console.dir(updated)
// Serialize
const fileObj = await serialize(file);
const fileObj = await serialize(updated);
console.dir(fileObj)
// Response
res(fileObj);

View File

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

View File

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

View File

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

View File

@ -31,44 +31,37 @@ export default (
if (mongo.ObjectID.prototype.isPrototypeOf(file)) {
_file = await DriveFile.findOne({
_id: file
}, {
fields: {
data: false
}
});
} else if (typeof file === 'string') {
_file = await DriveFile.findOne({
_id: new mongo.ObjectID(file)
}, {
fields: {
data: false
}
});
} else {
_file = deepcopy(file);
}
// Rename _id to id
_file.id = _file._id;
delete _file._id;
// rendered target
let _target: any = {};
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
_file.folder = await serializeDriveFolder(_file.folder_id, {
_target.folder = await serializeDriveFolder(_target.folder_id, {
detail: true
});
}
if (opts.detail && _file.tags) {
if (opts.detail && _target.tags) {
// Populate tags
_file.tags = await _file.tags.map(async (tag: any) =>
_target.tags = await _target.tags.map(async (tag: any) =>
await serializeDriveTag(tag)
);
}
resolve(_file);
resolve(_target);
});

View File

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