|
|
|
@ -11,7 +11,7 @@ class Misc extends Controller {
|
|
|
|
|
#default_token_grants = ['database']
|
|
|
|
|
|
|
|
|
|
static get services() {
|
|
|
|
|
return [...super.services, 'models']
|
|
|
|
|
return [...super.services, 'models', 'injector']
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async get_token(req, res, next) {
|
|
|
|
@ -36,6 +36,7 @@ class Misc extends Controller {
|
|
|
|
|
const Database = this.models.get('api:db:Database')
|
|
|
|
|
const FileGroup = this.models.get('api:FileGroup')
|
|
|
|
|
const File = this.models.get('upload::File')
|
|
|
|
|
const FileBox = this.models.get('api:files:FileBox')
|
|
|
|
|
|
|
|
|
|
const all_user_page_ids = (await req.user.get_accessible_pages()).map(x => x.UUID)
|
|
|
|
|
const results = []
|
|
|
|
@ -125,12 +126,41 @@ class Misc extends Controller {
|
|
|
|
|
return self.indexOf(value) === index
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const matching_boxes = await FileBox.find({
|
|
|
|
|
active: true,
|
|
|
|
|
pageId: { $in: all_user_page_ids },
|
|
|
|
|
$text: { $search: query },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
for ( const box of matching_boxes ) {
|
|
|
|
|
const page = await box.page()
|
|
|
|
|
results.push({
|
|
|
|
|
title: box.name,
|
|
|
|
|
short_title: `${box.name.replace(/(<([^>]+)>)/ig, '').slice(0, snip_length)}${box.name.length > snip_length ? '...' : ''}`,
|
|
|
|
|
type: 'file_box',
|
|
|
|
|
id: box.nodeId,
|
|
|
|
|
boxId: box.UUID,
|
|
|
|
|
associated: {
|
|
|
|
|
title: page.Name,
|
|
|
|
|
short_title: `${page.Name.slice(0, snip_length)}${page.Name.length > snip_length ? '...' : ''}`,
|
|
|
|
|
type: 'page',
|
|
|
|
|
id: page.UUID,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const all_boxes = await FileBox.find({
|
|
|
|
|
active: true,
|
|
|
|
|
pageId: { $in: all_user_page_ids },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const all_file_groups = await FileGroup.find({
|
|
|
|
|
PageId: { $in: all_user_page_ids },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
let all_file_ids = []
|
|
|
|
|
const file_id_x_group = {}
|
|
|
|
|
const file_id_x_box = {}
|
|
|
|
|
for ( const group of all_file_groups ) {
|
|
|
|
|
group.FileIds.forEach(id => {
|
|
|
|
|
all_file_ids.push(id)
|
|
|
|
@ -138,6 +168,13 @@ class Misc extends Controller {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ( const box of all_boxes ) {
|
|
|
|
|
box.fileIds.forEach(id => {
|
|
|
|
|
all_file_ids.push(id)
|
|
|
|
|
file_id_x_box[id] = box
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const unique_file_ids = all_file_ids.filter(unique).map(x => File.ObjectId(x))
|
|
|
|
|
|
|
|
|
|
const matching_files = await File.find({
|
|
|
|
@ -147,7 +184,24 @@ class Misc extends Controller {
|
|
|
|
|
|
|
|
|
|
for ( const file of matching_files ) {
|
|
|
|
|
const group = file_id_x_group[file.id]
|
|
|
|
|
if ( group ) {
|
|
|
|
|
const box = file_id_x_box[file.id]
|
|
|
|
|
|
|
|
|
|
if ( box ) {
|
|
|
|
|
const page = await box.page()
|
|
|
|
|
results.push({
|
|
|
|
|
title: file.original_name,
|
|
|
|
|
short_title: `${file.original_name.slice(0, snip_length)}${file.original_name.length > snip_length ? '...' : ''}`,
|
|
|
|
|
type: `file_box-${this.injector.get('controllers').get('api:v1:FileBox.get_file_category')(file.mime_type)}`,
|
|
|
|
|
id: box.nodeId,
|
|
|
|
|
boxId: box.UUID,
|
|
|
|
|
associated: {
|
|
|
|
|
title: page.Name,
|
|
|
|
|
short_title: `${page.Name.slice(0, snip_length)}${page.Name.length > snip_length ? '...' : ''}`,
|
|
|
|
|
type: 'page',
|
|
|
|
|
id: page.UUID,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
} else if ( group ) {
|
|
|
|
|
const page = await group.page
|
|
|
|
|
results.push({
|
|
|
|
|
title: file.original_name,
|
|
|
|
@ -159,7 +213,7 @@ class Misc extends Controller {
|
|
|
|
|
short_title: `${page.Name.slice(0, snip_length)}${page.Name.length > snip_length ? '...' : ''}`,
|
|
|
|
|
type: 'page',
|
|
|
|
|
id: page.UUID,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|