filebrowser/frontend/src/api/search.js

32 lines
627 B
JavaScript
Raw Normal View History

2021-03-21 11:51:58 +00:00
import { fetchURL, removePrefix } from "./utils";
import url from "../utils/url";
2021-03-21 11:51:58 +00:00
export default async function search(base, query) {
base = removePrefix(base);
query = encodeURIComponent(query);
2021-03-21 11:51:58 +00:00
if (!base.endsWith("/")) {
base += "/";
2020-09-29 14:04:43 +00:00
}
2021-03-21 11:51:58 +00:00
let res = await fetchURL(`/api/search${base}?query=${query}`, {});
2020-09-29 14:04:43 +00:00
if (res.status === 200) {
2021-03-21 11:51:58 +00:00
let data = await res.json();
2020-09-29 14:04:43 +00:00
data = data.map((item) => {
2021-03-21 11:51:58 +00:00
item.url = `/files${base}` + url.encodePath(item.path);
2020-10-19 13:14:36 +00:00
if (item.dir) {
2021-03-21 11:51:58 +00:00
item.url += "/";
2020-10-19 13:14:36 +00:00
}
2021-03-21 11:51:58 +00:00
return item;
});
2020-09-29 14:04:43 +00:00
2021-03-21 11:51:58 +00:00
return data;
2020-09-29 14:04:43 +00:00
} else {
2021-03-21 11:51:58 +00:00
throw Error(res.status);
2020-09-29 14:04:43 +00:00
}
2021-03-21 11:51:58 +00:00
}