Search.../

searchFiles( )

Developer Preview

Searches all folders in the Media Manager and returns a list of files that match the terms specified in the optional parameters.

Description

The searchFiles() function returns a Promise that resolves to an array of the specified files' descriptors and cursor information.

If no parameters are specified, the function returns all files in the MEDIA_ROOT folder.

Admin Method

This function requires elevated permissions to run. This function is not universal and runs only on the backend.

Syntax

function searchFiles(options: SearchFilesOptions): Promise<SearchFilesResponse>

searchFiles Parameters

NAME
TYPE
DESCRIPTION
options
Optional
SearchFilesOptions

Options to specify which folders to search.

Returns

Return Type:

Promise<
SearchFilesResponse
>
NAME
TYPE
DESCRIPTION
files
Array<
FileDescriptor
>

Files matching the query.

nextCursor
PagingMetadataV2

The next cursor if it exists.

Was this helpful?

Search files

When no rootFolder is specified in the options parameter, the search returns results from the root folder.

Copy Code
1import { files } from 'wix-media.v2';
2import { elevate } from 'wix-auth';
3
4async function mySearchFilesFunction() {
5 try {
6 const elevatedSearchFiles = elevate(files.searchFiles);
7 const rootFiles = await elevatedSearchFiles();
8
9 console.log('Files in MEDIA_ROOT', rootFiles);
10 return rootFiles;
11 } catch (error) {
12 console.error(error);
13 // Handle the error
14 }
15}
16
17/* Promise resolves to:
18 * {
19 * "files": [
20 * {
21 * "_createdDate": "2023-07-16T08:54:05.000Z",
22 * "_id": "w8ide0_ouulk5w0mdr9omjkwsfyn71g4mb1olcf~mv2.jpg",
23 * "_updatedDate": "2023-07-16T08:54:05.000Z",
24 * "displayName": "image2.jpg",
25 * "hash": "800fae3e0cb7908518a391c25864436c",
26 * "internalTags": [
27 * "_fileOrigin_uploaded"
28 * ],
29 * "labels": [
30 * "label1",
31 * "label2",
32 * "label3",
33 * "label4"
34 * ],
35 * "media": {
36 * "image": {
37 * "colors": {
38 * "palette": [
39 * {
40 * "rgb": {
41 * "b": 47,
42 * "g": 36,
43 * "r": 31
44 * }
45 * },
46 * {
47 * "rgb": {
48 * "b": 155,
49 * "g": 153,
50 * "r": 195
51 * }
52 * },
53 * {
54 * "rgb": {
55 * "b": 141,
56 * "g": 123,
57 * "r": 138
58 * }
59 * },
60 * {
61 * "rgb": {
62 * "b": 177,
63 * "g": 161,
64 * "r": 190
65 * }
66 * },
67 * {
68 * "rgb": {
69 * "b": 179,
70 * "g": 178,
71 * "r": 217
72 * }
73 * },
74 * {
75 * "rgb": {
76 * "b": 160,
77 * "g": 176,
78 * "r": 229
79 * }
80 * },
81 * {
82 * "rgb": {
83 * "b": 176,
84 * "g": 151,
85 * "r": 162
86 * }
87 * },
88 * {
89 * "rgb": {
90 * "b": 93,
91 * "g": 78,
92 * "r": 71
93 * }
94 * },
95 * {
96 * "rgb": {
97 * "b": 45,
98 * "g": 63,
99 * "r": 107
100 * }
101 * },
102 * {
103 * "rgb": {
104 * "b": 104,
105 * "g": 88,
106 * "r": 82
107 * }
108 * }
109 * ],
110 * "prominent": {
111 * "rgb": {
112 * "b": 26,
113 * "g": 36,
114 * "r": 39
115 * }
116 * }
117 * },
118 * "faces": [],
119 * "image": "wix:image://v1/w8ide0_ouulk5w0mdr9omjkwsfyn71g4mb1olcf~mv2.jpg/image2.jpg#originWidth=5833&originHeight=3894"
120 * }
121 * },
122 * "mediaType": "IMAGE",
123 * "operationStatus": "READY",
124 * "parentFolderId": "media-root",
125 * "private": false,
126 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
127 * "sizeInBytes": "3013886",
128 * "state": "OK",
129 * "thumbnailUrl": "https://static.wixstatic.com/media/w8ide0_ouulk5w0mdr9omjkwsfyn71g4mb1olcf~mv2.jpg",
130 * "url": "https://static.wixstatic.com/media/w8ide0_ouulk5w0mdr9omjkwsfyn71g4mb1olcf~mv2.jpg"
131 * },
132 * {
133 * "_createdDate": "2023-07-16T08:56:07.000Z",
134 * "_id": "w8ide0_suuti24j47vesffndm8hkd651u72vp3f~mv2.jpg",
135 * "_updatedDate": "2023-07-16T08:56:07.000Z",
136 * "displayName": "image1.jpg",
137 * "hash": "fef0ylitxcnlyzxf0nahu8rh3k0i5t85",
138 * "internalTags": [
139 * "_fileOrigin_uploaded"
140 * ],
141 * "labels": [
142 * "label1"
143 * ],
144 * "media": {
145 * "image": {
146 * "colors": {
147 * "palette": [
148 * {
149 * "rgb": {
150 * "b": 26,
151 * "g": 36,
152 * "r": 39
153 * }
154 * },
155 * {
156 * "rgb": {
157 * "b": 135,
158 * "g": 125,
159 * "r": 119
160 * }
161 * },
162 * {
163 * "rgb": {
164 * "b": 208,
165 * "g": 209,
166 * "r": 208
167 * }
168 * },
169 * {
170 * "rgb": {
171 * "b": 40,
172 * "g": 108,
173 * "r": 130
174 * }
175 * },
176 * {
177 * "rgb": {
178 * "b": 133,
179 * "g": 101,
180 * "r": 75
181 * }
182 * },
183 * {
184 * "rgb": {
185 * "b": 36,
186 * "g": 78,
187 * "r": 94
188 * }
189 * },
190 * {
191 * "rgb": {
192 * "b": 178,
193 * "g": 161,
194 * "r": 148
195 * }
196 * },
197 * {
198 * "rgb": {
199 * "b": 192,
200 * "g": 177,
201 * "r": 163
202 * }
203 * },
204 * {
205 * "rgb": {
206 * "b": 159,
207 * "g": 184,
208 * "r": 208
209 * }
210 * },
211 * {
212 * "rgb": {
213 * "b": 137,
214 * "g": 159,
215 * "r": 183
216 * }
217 * }
218 * ],
219 * "prominent": {
220 * "rgb": {
221 * "b": 26,
222 * "g": 36,
223 * "r": 39
224 * }
225 * }
226 * },
227 * "faces": [],
228 * "image": "wix:image://v1/w8ide0_suuti24j47vesffndm8hkd651u72vp3f~mv2.jpg/image1.jpg#originWidth=5616&originHeight=3744"
229 * }
230 * },
231 * "mediaType": "IMAGE",
232 * "operationStatus": "READY",
233 * "parentFolderId": "media-root",
234 * "private": false,
235 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
236 * "sizeInBytes": "6312959",
237 * "state": "OK",
238 * "thumbnailUrl": "https://static.wixstatic.com/media/w8ide0_suuti24j47vesffndm8hkd651u72vp3f~mv2.jpg",
239 * "url": "https://static.wixstatic.com/media/w8ide0_suuti24j47vesffndm8hkd651u72vp3f~mv2.jpg"
240 * },
241 * {
242 * "_createdDate": "2023-07-16T08:56:10.000Z",
243 * "_id": "w8ide0_9yvsg5vv40flojo0120h0rz6ki4g0tur.zip",
244 * "_updatedDate": "2023-07-16T08:56:10.000Z",
245 * "displayName": "file1.zip",
246 * "hash": "urm9iak1w39pk381ucb9kqjphya8loro",
247 * "internalTags": [
248 * "_fileOrigin_uploaded"
249 * ],
250 * "labels": [],
251 * "media": {
252 * "archive": {
253 * "_id": "w8ide0_9yvsg5vv40flojo0120h0rz6ki4g0tur.zip",
254 * "filename": "file1.zip",
255 * "sizeInBytes": "49546083",
256 * "url": "https://3ecba886-4267-11ee-be56-0242ac120002.usrfiles.com/archives/w8ide0_9yvsg5vv40flojo0120h0rz6ki4g0tur.zip"
257 * }
258 * },
259 * "mediaType": "ARCHIVE",
260 * "operationStatus": "READY",
261 * "parentFolderId": "media-root",
262 * "private": false,
263 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
264 * "sizeInBytes": "49546083",
265 * "state": "OK",
266 * "thumbnailUrl": "https://static.wixstatic.com/media/48c4fbc47d7298cd4406936294b4c532.png",
267 * "url": "https://3ecba886-4267-11ee-be56-0242ac120002.usrfiles.com/archives/w8ide0_9yvsg5vv40flojo0120h0rz6ki4g0tur.zip"
268 * }
269 * ],
270 * "nextCursor": {
271 * "cursors": {
272 * "next": ""
273 * },
274 * "hasNext": false
275 * }
276 * }
277 */
Search files with options

When no rootFolder is specified in the options parameter, the search returns results from the root folder.

Copy Code
1import { files } from 'wix-media.v2';
2import { elevate } from 'wix-auth';
3
4/* Sample searchOptions value:
5 * {
6 * mediaTypes: ['IMAGE', 'ARCHIVE'],
7 * search: 'river'
8 * }
9 */
10
11async function mySearchFilesFunction(searchOptions) {
12 try {
13 const elevatedSearchFiles = elevate(files.searchFiles);
14 const searchFiles = await elevatedSearchFiles(searchOptions);
15
16 console.log('Search files result:', searchFiles);
17 return searchFiles;
18 } catch (error) {
19 console.error(error);
20 // Handle the error
21 }
22}
23
24/* Promise resolves to:
25 * {
26 * "files": [
27 * {
28 * "_createdDate": "2023-07-16T08:54:05.000Z",
29 * "_id": "w8ide0_j18vyi43l4adwd81tpioxumkojv3dg78~mv2.jpg",
30 * "_updatedDate": "2023-07-19T08:43:58.000Z",
31 * "displayName": "river",
32 * "hash": "xa8k1btltmtn0nimi7a2diwdxd4aujku",
33 * "internalTags": [
34 * "_fileOrigin_uploaded"
35 * ],
36 * "labels": [
37 * "mountain stream",
38 * "fresh water",
39 * "river bank",
40 * "natural scenery"
41 * ],
42 * "media": {
43 * "image": {
44 * "colors": {
45 * "palette": [
46 * {
47 * "rgb": {
48 * "b": 41,
49 * "g": 41,
50 * "r": 24
51 * }
52 * },
53 * {
54 * "rgb": {
55 * "b": 135,
56 * "g": 105,
57 * "r": 62
58 * }
59 * },
60 * {
61 * "rgb": {
62 * "b": 195,
63 * "g": 154,
64 * "r": 105
65 * }
66 * },
67 * {
68 * "rgb": {
69 * "b": 230,
70 * "g": 190,
71 * "r": 141
72 * }
73 * },
74 * {
75 * "rgb": {
76 * "b": 96,
77 * "g": 77,
78 * "r": 44
79 * }
80 * },
81 * {
82 * "rgb": {
83 * "b": 144,
84 * "g": 135,
85 * "r": 99
86 * }
87 * },
88 * {
89 * "rgb": {
90 * "b": 64,
91 * "g": 106,
92 * "r": 64
93 * }
94 * },
95 * {
96 * "rgb": {
97 * "b": 178,
98 * "g": 131,
99 * "r": 80
100 * }
101 * },
102 * {
103 * "rgb": {
104 * "b": 247,
105 * "g": 230,
106 * "r": 185
107 * }
108 * },
109 * {
110 * "rgb": {
111 * "b": 75,
112 * "g": 145,
113 * "r": 97
114 * }
115 * }
116 * ],
117 * "prominent": {
118 * "rgb": {
119 * "b": 41,
120 * "g": 41,
121 * "r": 24
122 * }
123 * }
124 * },
125 * "faces": [],
126 * "image": "wix:image://v1/w8ide0_j18vyi43l4adwd81tpioxumkojv3dg78~mv2.jpg/river#originWidth=5760&originHeight=3840"
127 * }
128 * },
129 * "mediaType": "IMAGE",
130 * "operationStatus": "READY",
131 * "parentFolderId": "media-root",
132 * "private": false,
133 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
134 * "sizeInBytes": "3677628",
135 * "state": "OK",
136 * "thumbnailUrl": "https://static.wixstatic.com/media/w8ide0_j18vyi43l4adwd81tpioxumkojv3dg78~mv2.jpg",
137 * "url": "https://static.wixstatic.com/media/w8ide0_j18vyi43l4adwd81tpioxumkojv3dg78~mv2.jpg"
138 * },
139 * {
140 * "_createdDate": "2023-07-16T08:56:10.000Z",
141 * "_id": "w8ide0_r047t3x8bm46vlx5n1aeha1bdd7g4u1y.zip",
142 * "_updatedDate": "2023-07-20T07:16:55.000Z",
143 * "displayName": "riverpics",
144 * "hash": "3vx3d37vf3j2ttxp61ax7wlt0iox58nh",
145 * "internalTags": [
146 * "_fileOrigin_uploaded"
147 * ],
148 * "labels": [],
149 * "media": {
150 * "archive": {
151 * "_id": "w8ide0_r047t3x8bm46vlx5n1aeha1bdd7g4u1y.zip",
152 * "filename": "riverpics",
153 * "sizeInBytes": "49546083",
154 * "url": "https://3ecba886-4267-11ee-be56-0242ac120002.usrfiles.com/archives/w8ide0_r047t3x8bm46vlx5n1aeha1bdd7g4u1y.zip"
155 * }
156 * },
157 * "mediaType": "ARCHIVE",
158 * "operationStatus": "READY",
159 * "parentFolderId": "media-root",
160 * "private": false,
161 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
162 * "sizeInBytes": "49546083",
163 * "state": "OK",
164 * "thumbnailUrl": "https://static.wixstatic.com/media/48c4fbc47d7298cd4406936294b4c532.png",
165 * "url": "https://3ecba886-4267-11ee-be56-0242ac120002.usrfiles.com/archives/w8ide0_r047t3x8bm46vlx5n1aeha1bdd7g4u1y.zip"
166 * }
167 * ],
168 * "nextCursor": {
169 * "cursors": {
170 * "next": ""
171 * },
172 * "hasNext": false
173 * }
174 * }
175 */
Bulk delete all files found in a chosen folder

This code shows a page where the visitor chooses a folder by typing the folder's name in an input box, and then all the files in the chosen folder are deleted. Only visitors with permission to manage the site's media would have access to this page.

Copy Code
1/****************************************
2 * Backend code - bulk-delete-files.jsw *
3 ***************************************/
4
5import { files } from 'wix-business-tools.v2';
6import { elevate } from 'wix-auth';
7
8export async function deleteFiles (fileIds, options) {
9 try {
10 const elevatedBulkDeleteFiles = elevate(files.bulkDeleteFiles)
11 await elevatedBulkDeleteFiles(fileIds, options);
12
13 console.log(`Permanently deleted files with ids: ${fileIds.toString()}.`);
14 return true;
15 } catch (error) {
16 console.error(error);
17 }
18}
19
20export async function searchFileIds (parentFolderId) {
21 try {
22 const options = { parentFolder: parentFolderId}
23 const elevatedSearchFiles = elevate(files.searchFiles)
24 const returnedFiles = await elevatedSearchFiles(options);
25
26 const fileIds = returnedFiles.map((file) => {
27 return file._id
28 });
29
30 return fileIds;
31 } catch (error) {
32 console.error(error);
33 }
34}
35
36
37/*************
38 * Page code *
39 ************/
40
41import { deleteFiles, searchFileIds } from 'backend/bulk-delete-files';
42
43$w.onReady(() => {
44 $w('#delete').onClick(async () => {
45 const isDeletePermanently = $w('#deletePermanently').checked;
46 const options = {permanent: isDeletePermanently};
47
48 const parentFolder = $w('#parentFolder').value;
49 const fileIds = await searchFileIds(parentFolder);
50
51 await deleteFiles(fileIds, options);
52 $w('#successMessage').show();
53 });
54});