sharkey/src/server/api/endpoints/admin/drive/files.ts

82 lines
1.4 KiB
TypeScript
Raw Normal View History

2018-12-14 19:09:11 +09:00
import $ from 'cafy';
import File, { packMany } from '../../../../../models/drive-file';
import define from '../../../define';
export const meta = {
requireCredential: false,
requireModerator: true,
params: {
limit: {
2019-02-13 16:33:07 +09:00
validator: $.optional.num.range(1, 100),
2018-12-14 19:09:11 +09:00
default: 10
},
offset: {
2019-02-13 16:33:07 +09:00
validator: $.optional.num.min(0),
2018-12-14 19:09:11 +09:00
default: 0
},
sort: {
2019-02-13 16:33:07 +09:00
validator: $.optional.str.or([
2018-12-14 19:09:11 +09:00
'+createdAt',
'-createdAt',
'+size',
'-size',
]),
},
origin: {
2019-02-13 16:33:07 +09:00
validator: $.optional.str.or([
2018-12-14 19:09:11 +09:00
'combined',
'local',
'remote',
]),
default: 'local'
}
}
};
export default define(meta, (ps, me) => new Promise(async (res, rej) => {
let _sort;
if (ps.sort) {
if (ps.sort == '+createdAt') {
_sort = {
uploadDate: -1
};
} else if (ps.sort == '-createdAt') {
_sort = {
uploadDate: 1
};
} else if (ps.sort == '+size') {
_sort = {
length: -1
};
2018-12-24 05:31:20 +09:00
} else if (ps.sort == '-size') {
2018-12-14 19:09:11 +09:00
_sort = {
2018-12-24 05:31:20 +09:00
length: 1
2018-12-14 19:09:11 +09:00
};
}
} else {
_sort = {
_id: -1
};
}
2018-12-15 00:09:04 +09:00
const q = {
'metadata.deletedAt': { $exists: false },
} as any;
if (ps.origin == 'local') q['metadata._user.host'] = null;
if (ps.origin == 'remote') q['metadata._user.host'] = { $ne: null };
2018-12-14 19:09:11 +09:00
const files = await File
.find(q, {
limit: ps.limit,
sort: _sort,
skip: ps.offset
});
res(await packMany(files, { detail: true, withUser: true, self: true }));
2018-12-14 19:09:11 +09:00
}));