searchFiles( )
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.
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 to specify which folders to search.
Returns
Return Type:
NAME
TYPE
DESCRIPTION
Files matching the query.
The next cursor if it exists.
Was this helpful?
1import { files } from 'wix-media.v2';23async function mySearchFilesFunction() {4 try {5 const rootFiles = await files.searchFiles();67 console.log('Files in MEDIA_ROOT', rootFiles);8 return rootFiles;9 } catch (error) {10 console.error(error);11 // Handle the error12 }13}1415/* Promise resolves to:16 * {17 * "files": [18 * {19 * "_createdDate": "2023-07-16T08:54:05.000Z",20 * "_id": "w8ide0_ouulk5w0mdr9omjkwsfyn71g4mb1olcf~mv2.jpg",21 * "_updatedDate": "2023-07-16T08:54:05.000Z",22 * "displayName": "image2.jpg",23 * "hash": "800fae3e0cb7908518a391c25864436c",24 * "internalTags": [25 * "_fileOrigin_uploaded"26 * ],27 * "labels": [28 * "label1",29 * "label2",30 * "label3",31 * "label4"32 * ],33 * "media": {34 * "image": {35 * "colors": {36 * "palette": [37 * {38 * "rgb": {39 * "b": 47,40 * "g": 36,41 * "r": 3142 * }43 * },44 * {45 * "rgb": {46 * "b": 155,47 * "g": 153,48 * "r": 19549 * }50 * },51 * {52 * "rgb": {53 * "b": 141,54 * "g": 123,55 * "r": 13856 * }57 * },58 * {59 * "rgb": {60 * "b": 177,61 * "g": 161,62 * "r": 19063 * }64 * },65 * {66 * "rgb": {67 * "b": 179,68 * "g": 178,69 * "r": 21770 * }71 * },72 * {73 * "rgb": {74 * "b": 160,75 * "g": 176,76 * "r": 22977 * }78 * },79 * {80 * "rgb": {81 * "b": 176,82 * "g": 151,83 * "r": 16284 * }85 * },86 * {87 * "rgb": {88 * "b": 93,89 * "g": 78,90 * "r": 7191 * }92 * },93 * {94 * "rgb": {95 * "b": 45,96 * "g": 63,97 * "r": 10798 * }99 * },100 * {101 * "rgb": {102 * "b": 104,103 * "g": 88,104 * "r": 82105 * }106 * }107 * ],108 * "prominent": {109 * "rgb": {110 * "b": 26,111 * "g": 36,112 * "r": 39113 * }114 * }115 * },116 * "faces": [],117 * "image": "wix:image://v1/w8ide0_ouulk5w0mdr9omjkwsfyn71g4mb1olcf~mv2.jpg/image2.jpg#originWidth=5833&originHeight=3894"118 * }119 * },120 * "mediaType": "IMAGE",121 * "operationStatus": "READY",122 * "parentFolderId": "media-root",123 * "private": false,124 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",125 * "sizeInBytes": "3013886",126 * "state": "OK",127 * "thumbnailUrl": "https://static.wixstatic.com/media/w8ide0_ouulk5w0mdr9omjkwsfyn71g4mb1olcf~mv2.jpg",128 * "url": "https://static.wixstatic.com/media/w8ide0_ouulk5w0mdr9omjkwsfyn71g4mb1olcf~mv2.jpg"129 * },130 * {131 * "_createdDate": "2023-07-16T08:56:07.000Z",132 * "_id": "w8ide0_suuti24j47vesffndm8hkd651u72vp3f~mv2.jpg",133 * "_updatedDate": "2023-07-16T08:56:07.000Z",134 * "displayName": "image1.jpg",135 * "hash": "fef0ylitxcnlyzxf0nahu8rh3k0i5t85",136 * "internalTags": [137 * "_fileOrigin_uploaded"138 * ],139 * "labels": [140 * "label1"141 * ],142 * "media": {143 * "image": {144 * "colors": {145 * "palette": [146 * {147 * "rgb": {148 * "b": 26,149 * "g": 36,150 * "r": 39151 * }152 * },153 * {154 * "rgb": {155 * "b": 135,156 * "g": 125,157 * "r": 119158 * }159 * },160 * {161 * "rgb": {162 * "b": 208,163 * "g": 209,164 * "r": 208165 * }166 * },167 * {168 * "rgb": {169 * "b": 40,170 * "g": 108,171 * "r": 130172 * }173 * },174 * {175 * "rgb": {176 * "b": 133,177 * "g": 101,178 * "r": 75179 * }180 * },181 * {182 * "rgb": {183 * "b": 36,184 * "g": 78,185 * "r": 94186 * }187 * },188 * {189 * "rgb": {190 * "b": 178,191 * "g": 161,192 * "r": 148193 * }194 * },195 * {196 * "rgb": {197 * "b": 192,198 * "g": 177,199 * "r": 163200 * }201 * },202 * {203 * "rgb": {204 * "b": 159,205 * "g": 184,206 * "r": 208207 * }208 * },209 * {210 * "rgb": {211 * "b": 137,212 * "g": 159,213 * "r": 183214 * }215 * }216 * ],217 * "prominent": {218 * "rgb": {219 * "b": 26,220 * "g": 36,221 * "r": 39222 * }223 * }224 * },225 * "faces": [],226 * "image": "wix:image://v1/w8ide0_suuti24j47vesffndm8hkd651u72vp3f~mv2.jpg/image1.jpg#originWidth=5616&originHeight=3744"227 * }228 * },229 * "mediaType": "IMAGE",230 * "operationStatus": "READY",231 * "parentFolderId": "media-root",232 * "private": false,233 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",234 * "sizeInBytes": "6312959",235 * "state": "OK",236 * "thumbnailUrl": "https://static.wixstatic.com/media/w8ide0_suuti24j47vesffndm8hkd651u72vp3f~mv2.jpg",237 * "url": "https://static.wixstatic.com/media/w8ide0_suuti24j47vesffndm8hkd651u72vp3f~mv2.jpg"238 * },239 * {240 * "_createdDate": "2023-07-16T08:56:10.000Z",241 * "_id": "w8ide0_9yvsg5vv40flojo0120h0rz6ki4g0tur.zip",242 * "_updatedDate": "2023-07-16T08:56:10.000Z",243 * "displayName": "file1.zip",244 * "hash": "urm9iak1w39pk381ucb9kqjphya8loro",245 * "internalTags": [246 * "_fileOrigin_uploaded"247 * ],248 * "labels": [],249 * "media": {250 * "archive": {251 * "_id": "w8ide0_9yvsg5vv40flojo0120h0rz6ki4g0tur.zip",252 * "filename": "file1.zip",253 * "sizeInBytes": "49546083",254 * "url": "https://3ecba886-4267-11ee-be56-0242ac120002.usrfiles.com/archives/w8ide0_9yvsg5vv40flojo0120h0rz6ki4g0tur.zip"255 * }256 * },257 * "mediaType": "ARCHIVE",258 * "operationStatus": "READY",259 * "parentFolderId": "media-root",260 * "private": false,261 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",262 * "sizeInBytes": "49546083",263 * "state": "OK",264 * "thumbnailUrl": "https://static.wixstatic.com/media/48c4fbc47d7298cd4406936294b4c532.png",265 * "url": "https://3ecba886-4267-11ee-be56-0242ac120002.usrfiles.com/archives/w8ide0_9yvsg5vv40flojo0120h0rz6ki4g0tur.zip"266 * }267 * ],268 * "nextCursor": {269 * "cursors": {270 * "next": ""271 * },272 * "hasNext": false273 * }274 * }275 */
1import { Permissions, webMethod } from 'wix-web-module';2import { files } from 'wix-media.v2';3import { elevate } from 'wix-auth';45export const mySearchFilesFunction = webMethod(Permissions.Anyone, async () => {6 try {7 const elevatedSearchFiles = elevate(files.searchFiles);8 const rootFiles = await elevatedSearchFiles();910 console.log('Files in MEDIA_ROOT', rootFiles);11 return rootFiles;12 } catch (error) {13 console.error(error);14 // Handle the error15 }16});1718/* Promise resolves to:19 * {20 * "files": [21 * {22 * "_createdDate": "2023-07-16T08:54:05.000Z",23 * "_id": "w8ide0_ouulk5w0mdr9omjkwsfyn71g4mb1olcf~mv2.jpg",24 * "_updatedDate": "2023-07-16T08:54:05.000Z",25 * "displayName": "image2.jpg",26 * "hash": "800fae3e0cb7908518a391c25864436c",27 * "internalTags": [28 * "_fileOrigin_uploaded"29 * ],30 * "labels": [31 * "label1",32 * "label2",33 * "label3",34 * "label4"35 * ],36 * "media": {37 * "image": {38 * "colors": {39 * "palette": [40 * {41 * "rgb": {42 * "b": 47,43 * "g": 36,44 * "r": 3145 * }46 * },47 * {48 * "rgb": {49 * "b": 155,50 * "g": 153,51 * "r": 19552 * }53 * },54 * {55 * "rgb": {56 * "b": 141,57 * "g": 123,58 * "r": 13859 * }60 * },61 * {62 * "rgb": {63 * "b": 177,64 * "g": 161,65 * "r": 19066 * }67 * },68 * {69 * "rgb": {70 * "b": 179,71 * "g": 178,72 * "r": 21773 * }74 * },75 * {76 * "rgb": {77 * "b": 160,78 * "g": 176,79 * "r": 22980 * }81 * },82 * {83 * "rgb": {84 * "b": 176,85 * "g": 151,86 * "r": 16287 * }88 * },89 * {90 * "rgb": {91 * "b": 93,92 * "g": 78,93 * "r": 7194 * }95 * },96 * {97 * "rgb": {98 * "b": 45,99 * "g": 63,100 * "r": 107101 * }102 * },103 * {104 * "rgb": {105 * "b": 104,106 * "g": 88,107 * "r": 82108 * }109 * }110 * ],111 * "prominent": {112 * "rgb": {113 * "b": 26,114 * "g": 36,115 * "r": 39116 * }117 * }118 * },119 * "faces": [],120 * "image": "wix:image://v1/w8ide0_ouulk5w0mdr9omjkwsfyn71g4mb1olcf~mv2.jpg/image2.jpg#originWidth=5833&originHeight=3894"121 * }122 * },123 * "mediaType": "IMAGE",124 * "operationStatus": "READY",125 * "parentFolderId": "media-root",126 * "private": false,127 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",128 * "sizeInBytes": "3013886",129 * "state": "OK",130 * "thumbnailUrl": "https://static.wixstatic.com/media/w8ide0_ouulk5w0mdr9omjkwsfyn71g4mb1olcf~mv2.jpg",131 * "url": "https://static.wixstatic.com/media/w8ide0_ouulk5w0mdr9omjkwsfyn71g4mb1olcf~mv2.jpg"132 * },133 * {134 * "_createdDate": "2023-07-16T08:56:07.000Z",135 * "_id": "w8ide0_suuti24j47vesffndm8hkd651u72vp3f~mv2.jpg",136 * "_updatedDate": "2023-07-16T08:56:07.000Z",137 * "displayName": "image1.jpg",138 * "hash": "fef0ylitxcnlyzxf0nahu8rh3k0i5t85",139 * "internalTags": [140 * "_fileOrigin_uploaded"141 * ],142 * "labels": [143 * "label1"144 * ],145 * "media": {146 * "image": {147 * "colors": {148 * "palette": [149 * {150 * "rgb": {151 * "b": 26,152 * "g": 36,153 * "r": 39154 * }155 * },156 * {157 * "rgb": {158 * "b": 135,159 * "g": 125,160 * "r": 119161 * }162 * },163 * {164 * "rgb": {165 * "b": 208,166 * "g": 209,167 * "r": 208168 * }169 * },170 * {171 * "rgb": {172 * "b": 40,173 * "g": 108,174 * "r": 130175 * }176 * },177 * {178 * "rgb": {179 * "b": 133,180 * "g": 101,181 * "r": 75182 * }183 * },184 * {185 * "rgb": {186 * "b": 36,187 * "g": 78,188 * "r": 94189 * }190 * },191 * {192 * "rgb": {193 * "b": 178,194 * "g": 161,195 * "r": 148196 * }197 * },198 * {199 * "rgb": {200 * "b": 192,201 * "g": 177,202 * "r": 163203 * }204 * },205 * {206 * "rgb": {207 * "b": 159,208 * "g": 184,209 * "r": 208210 * }211 * },212 * {213 * "rgb": {214 * "b": 137,215 * "g": 159,216 * "r": 183217 * }218 * }219 * ],220 * "prominent": {221 * "rgb": {222 * "b": 26,223 * "g": 36,224 * "r": 39225 * }226 * }227 * },228 * "faces": [],229 * "image": "wix:image://v1/w8ide0_suuti24j47vesffndm8hkd651u72vp3f~mv2.jpg/image1.jpg#originWidth=5616&originHeight=3744"230 * }231 * },232 * "mediaType": "IMAGE",233 * "operationStatus": "READY",234 * "parentFolderId": "media-root",235 * "private": false,236 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",237 * "sizeInBytes": "6312959",238 * "state": "OK",239 * "thumbnailUrl": "https://static.wixstatic.com/media/w8ide0_suuti24j47vesffndm8hkd651u72vp3f~mv2.jpg",240 * "url": "https://static.wixstatic.com/media/w8ide0_suuti24j47vesffndm8hkd651u72vp3f~mv2.jpg"241 * },242 * {243 * "_createdDate": "2023-07-16T08:56:10.000Z",244 * "_id": "w8ide0_9yvsg5vv40flojo0120h0rz6ki4g0tur.zip",245 * "_updatedDate": "2023-07-16T08:56:10.000Z",246 * "displayName": "file1.zip",247 * "hash": "urm9iak1w39pk381ucb9kqjphya8loro",248 * "internalTags": [249 * "_fileOrigin_uploaded"250 * ],251 * "labels": [],252 * "media": {253 * "archive": {254 * "_id": "w8ide0_9yvsg5vv40flojo0120h0rz6ki4g0tur.zip",255 * "filename": "file1.zip",256 * "sizeInBytes": "49546083",257 * "url": "https://3ecba886-4267-11ee-be56-0242ac120002.usrfiles.com/archives/w8ide0_9yvsg5vv40flojo0120h0rz6ki4g0tur.zip"258 * }259 * },260 * "mediaType": "ARCHIVE",261 * "operationStatus": "READY",262 * "parentFolderId": "media-root",263 * "private": false,264 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",265 * "sizeInBytes": "49546083",266 * "state": "OK",267 * "thumbnailUrl": "https://static.wixstatic.com/media/48c4fbc47d7298cd4406936294b4c532.png",268 * "url": "https://3ecba886-4267-11ee-be56-0242ac120002.usrfiles.com/archives/w8ide0_9yvsg5vv40flojo0120h0rz6ki4g0tur.zip"269 * }270 * ],271 * "nextCursor": {272 * "cursors": {273 * "next": ""274 * },275 * "hasNext": false276 * }277 * }278 */
When no rootFolder
is specified in the options parameter, the search returns results from the 'MEDIA_ROOT' folder.
1import { files } from 'wix-media.v2';2import { elevate } from 'wix-auth';34/* Sample searchOptions value:5 * {6 * mediaTypes: ['IMAGE', 'ARCHIVE'],7 * search: 'river'8 * }9 */1011async function mySearchFilesFunction(searchOptions) {12 try {13 const elevatedSearchFiles = elevate(files.searchFiles);14 const searchFiles = await elevatedSearchFiles(searchOptions);1516 console.log('Search files result:', searchFiles);17 return searchFiles;18 } catch (error) {19 console.error(error);20 // Handle the error21 }22}2324/* 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": 2451 * }52 * },53 * {54 * "rgb": {55 * "b": 135,56 * "g": 105,57 * "r": 6258 * }59 * },60 * {61 * "rgb": {62 * "b": 195,63 * "g": 154,64 * "r": 10565 * }66 * },67 * {68 * "rgb": {69 * "b": 230,70 * "g": 190,71 * "r": 14172 * }73 * },74 * {75 * "rgb": {76 * "b": 96,77 * "g": 77,78 * "r": 4479 * }80 * },81 * {82 * "rgb": {83 * "b": 144,84 * "g": 135,85 * "r": 9986 * }87 * },88 * {89 * "rgb": {90 * "b": 64,91 * "g": 106,92 * "r": 6493 * }94 * },95 * {96 * "rgb": {97 * "b": 178,98 * "g": 131,99 * "r": 80100 * }101 * },102 * {103 * "rgb": {104 * "b": 247,105 * "g": 230,106 * "r": 185107 * }108 * },109 * {110 * "rgb": {111 * "b": 75,112 * "g": 145,113 * "r": 97114 * }115 * }116 * ],117 * "prominent": {118 * "rgb": {119 * "b": 41,120 * "g": 41,121 * "r": 24122 * }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": false173 * }174 * }175 */
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.
1/*******************************************2 * Backend code - bulk-delete-files.web.js *3 ******************************************/45import { Permissions, webMethod } from 'wix-web-module';6import { files } from 'wix-business-tools.v2';7import { elevate } from 'wix-auth';89export const deleteFiles = webMethod(Permissions.Anyone, async (fileIds, options) => {10 try {11 const elevatedBulkDeleteFiles = elevate(files.bulkDeleteFiles);12 await elevatedBulkDeleteFiles(fileIds, options);1314 console.log(`Permanently deleted files with ids: ${fileIds.toString()}.`);15 return true;16 } catch (error) {17 console.error(error);18 }19});2021export const searchFileIds = webMethod(Permissions.Anyone, async (parentFolderId) => {22 try {23 const options = { parentFolder: parentFolderId };24 const elevatedSearchFiles = elevate(files.searchFiles);25 const returnedFiles = await elevatedSearchFiles(options);2627 const fileIds = returnedFiles.map((file) => {28 return file._id;29 });3031 return fileIds;32 } catch (error) {33 console.error(error);34 }35});363738/*************39 * Page code *40 ************/4142import { deleteFiles, searchFileIds } from 'backend/bulk-delete-files.web';4344$w.onReady(() => {45 $w('#delete').onClick(async () => {46 const isDeletePermanently = $w('#deletePermanently').checked;47 const options = { permanent: isDeletePermanently };4849 const parentFolder = $w('#parentFolder').value;50 const fileIds = await searchFileIds(parentFolder);5152 await deleteFiles(fileIds, options);53 $w('#successMessage').show();54 });55});56