Skip to content

Commit a35cf9f

Browse files
committed
refactor(overview): remove hardcoded OFFICE_MIME_FILTERS, accept MIME list as parameter
Signed-off-by: James Manuel <moodyjmz@users.noreply.github.com>
1 parent bf0d730 commit a35cf9f

1 file changed

Lines changed: 24 additions & 50 deletions

File tree

src/services/officeFiles.js

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,18 @@
55

66
import { getClient, getDavNameSpaces, getDavProperties, getRootPath, resultToNode } from '@nextcloud/files/dav'
77

8-
export const OFFICE_MIME_FILTERS = {
9-
documents: [
10-
'application/vnd.oasis.opendocument.text',
11-
'application/vnd.oasis.opendocument.text-template',
12-
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
13-
'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
14-
'application/msword',
15-
],
16-
presentations: [
17-
'application/vnd.oasis.opendocument.presentation',
18-
'application/vnd.oasis.opendocument.presentation-template',
19-
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
20-
'application/vnd.openxmlformats-officedocument.presentationml.template',
21-
'application/vnd.ms-powerpoint',
22-
],
23-
spreadsheets: [
24-
'application/vnd.oasis.opendocument.spreadsheet',
25-
'application/vnd.oasis.opendocument.spreadsheet-template',
26-
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
27-
'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
28-
'application/vnd.ms-excel',
29-
'text/csv',
30-
],
31-
diagrams: [
32-
'application/vnd.oasis.opendocument.graphics',
33-
'application/vnd.oasis.opendocument.graphics-template',
34-
'application/vnd.visio',
35-
'application/vnd.ms-visio.drawing',
36-
],
37-
}
38-
39-
const ALL_OFFICE_MIMES = Object.values(OFFICE_MIME_FILTERS).flat()
40-
41-
// Pre-computed at module load time — ALL_OFFICE_MIMES is static so this never changes.
42-
const OFFICE_MIME_CONDITIONS = ALL_OFFICE_MIMES
43-
.map(mime => `\t\t\t\t<d:eq><d:prop><d:getcontenttype/></d:prop><d:literal>${mime}</d:literal></d:eq>`)
44-
.join('\n')
45-
468
/**
47-
* Build a DAV SEARCH request body that matches files of any office MIME type
9+
* Build a DAV SEARCH request body that matches files of any of the given MIME types
4810
* across all subdirectories (depth: infinity).
4911
*
12+
* @param {string[]} mimes List of MIME type strings to search for
5013
* @return {string} XML string for the SEARCH request body
5114
*/
52-
function getOfficeMimeSearch() {
15+
function buildOfficeMimeSearch(mimes) {
16+
const conditions = mimes
17+
.map(mime => `\t\t\t\t<d:eq><d:prop><d:getcontenttype/></d:prop><d:literal>${mime}</d:literal></d:eq>`)
18+
.join('\n')
19+
5320
return `<?xml version="1.0" encoding="UTF-8"?>
5421
<d:searchrequest ${getDavNameSpaces()}>
5522
<d:basicsearch>
@@ -66,7 +33,7 @@ function getOfficeMimeSearch() {
6633
</d:from>
6734
<d:where>
6835
<d:or>
69-
${OFFICE_MIME_CONDITIONS}
36+
${conditions}
7037
</d:or>
7138
</d:where>
7239
</d:basicsearch>
@@ -77,13 +44,14 @@ ${OFFICE_MIME_CONDITIONS}
7744
let cachedNodes = null
7845

7946
/**
80-
* Fetch all office files once and cache the result.
81-
* Subsequent calls return the cached array.
82-
* Uses DAV SEARCH to recursively find office files across all subdirectories.
47+
* Fetch all office files matching the given MIME types and cache the result.
48+
* Subsequent calls with the same set of MIMEs return the cached array.
49+
* Pass an empty array to invalidate and re-fetch.
8350
*
51+
* @param {string[]} mimes MIME types to search for, derived from template creators
8452
* @return {Promise<import('@nextcloud/files').Node[]>}
8553
*/
86-
export async function getAllOfficeFiles() {
54+
export async function getAllOfficeFiles(mimes) {
8755
if (cachedNodes) {
8856
return cachedNodes
8957
}
@@ -92,7 +60,7 @@ export async function getAllOfficeFiles() {
9260

9361
const response = await client.search('/', {
9462
details: true,
95-
data: getOfficeMimeSearch(),
63+
data: buildOfficeMimeSearch(mimes),
9664
})
9765

9866
cachedNodes = response.data.results
@@ -103,13 +71,19 @@ export async function getAllOfficeFiles() {
10371
}
10472

10573
/**
106-
* Filter a list of file nodes by an office category.
74+
* Discard the cached file list so the next getAllOfficeFiles() call re-fetches.
75+
*/
76+
export function invalidateOfficeFilesCache() {
77+
cachedNodes = null
78+
}
79+
80+
/**
81+
* Filter a list of file nodes to those whose MIME type is in the given set.
10782
*
10883
* @param {import('@nextcloud/files').Node[]} files
109-
* @param {string} category - One of 'documents', 'presentations', 'spreadsheets', 'diagrams'
84+
* @param {string[]} mimes MIME types for the active category
11085
* @return {import('@nextcloud/files').Node[]}
11186
*/
112-
export function filterByCategory(files, category) {
113-
const mimes = OFFICE_MIME_FILTERS[category]
87+
export function filterByMimes(files, mimes) {
11488
return files.filter(file => mimes.includes(file.mime))
11589
}

0 commit comments

Comments
 (0)