diff --git a/src/routes/Search/components/Search.tsx b/src/routes/Search/components/Search.tsx index bdea85bc..5c46c6f4 100644 --- a/src/routes/Search/components/Search.tsx +++ b/src/routes/Search/components/Search.tsx @@ -20,11 +20,72 @@ import * as API from 'types/api'; const SEARCH_PERIOD = 4000; +var urlSearchExecuted = 0; interface SearchProps extends RouteComponentProps<{}> { } +function getParameters(inputString: string) { + var params = {}; + + var tempString = inputString.trim(); + while (tempString.startsWith('-')) { + var spaceIndex = tempString.search(' '); + var argString = ''; + if (spaceIndex > 0) { + argString = tempString.substring(0, spaceIndex); + tempString = tempString.substring(spaceIndex).trim(); + spaceIndex = tempString.search(' '); + if (spaceIndex > 0) { + var valueString = tempString.substring(0, spaceIndex); + tempString = tempString.substring(spaceIndex).trim(); + params[argString] = valueString; + } + } + } + + // If there are only arguments and no actual search term, + // just return complete input instead. + if (tempString.length === 0) { + alert('Parameter parsing failed. Only parameters in search string: ' + inputString); + return [{}, inputString]; + } + + return [params, tempString]; +} + +function sanitizeFileType(inputString: string) { + if (inputString === 'audio') { return 'audio'; } + + if (inputString === 'archive') { return 'compressed'; } + if (inputString === 'compressed') { return 'compressed'; } + + if (inputString === 'doc') { return 'document'; } + if (inputString === 'document') { return 'document'; } + + if (inputString === 'exe') { return 'executable'; } + if (inputString === 'executable') { return 'executable'; } + + if (inputString === 'img') { return 'picture'; } + if (inputString === 'picture') { return 'picture'; } + + if (inputString === 'video') { return 'video'; } + if (inputString === 'filelist') { return 'filelist'; } + if (inputString === 'other') { return 'other'; } + if (inputString === 'any') { return 'any'; } + if (inputString === 'tth') { return 'tth'; } + + if (inputString === 'dir') { return 'directory'; } + if (inputString === 'directory') { return 'directory'; } + + if (inputString === 'file') { return 'file'; } + + alert('Bad file type \"' + inputString + '\". Using \"any\" instead.'); + + return 'any'; +} + class Search extends React.Component { state = { searchString: '', @@ -55,13 +116,30 @@ class Search extends React.Component { search = (searchString: string) => { console.log('Searching'); + var fileType = 'any'; + var fileTypeSelector = document.getElementById('file-type-selector'); + if (fileTypeSelector != null) { + fileType = (fileTypeSelector as HTMLInputElement).value; + } + + //inputString = searchString.trim(); + var params; + var queryString; + [params, queryString] = getParameters(searchString); + console.log('Search: ' + queryString); + if (params['-t'] != null) { + fileType = params['-t']; + fileType = sanitizeFileType(fileType); + console.log('Search file_type: ' + fileType); + } clearTimeout(this._searchTimeout); SocketService.post(SearchConstants.HUB_SEARCH_URL, { query: { - pattern: searchString, - }, + pattern: queryString, + file_type: fileType, + }, priority: API.PriorityEnum.HIGH, }) .then(this.onSearchPosted) @@ -88,6 +166,23 @@ class Search extends React.Component { render() { const { searchString, running } = this.state; + var searchDefault = searchString; + var url = new URL(window.location.href); + var urlSearch = url.searchParams.get('query'); + if (urlSearch != null) { + searchDefault = urlSearch; + if (urlSearchExecuted === 0) { // render() is called again when searching + urlSearchExecuted = 1; // This ensures search is only executed once. + console.log('Search term from url: ' + searchDefault); + // Execute search with timeout, when page is loaded. + setTimeout( + () => { + this.search(searchDefault); + }, + 100 + ); + } + } return ( {
+