diff --git a/webapi/API.go b/webapi/API.go index 78f3ba4..114ea6f 100644 --- a/webapi/API.go +++ b/webapi/API.go @@ -56,6 +56,7 @@ func Start(ListenAddresses []string, UseSSL bool, CertificateFile, CertificateKe Router.HandleFunc("/search/result", apiSearchResult).Methods("GET") //Router.HandleFunc("/search/result/ws", apiSearchResultStream).Methods("GET") Router.HandleFunc("/search/terminate", apiSearchTerminate).Methods("GET") + Router.HandleFunc("/explore", apiExplore).Methods("GET") for _, listen := range ListenAddresses { go startWebServer(listen, UseSSL, CertificateFile, CertificateKey, Router, "API", TimeoutRead, TimeoutWrite) diff --git a/webapi/Search Temp.go b/webapi/Search Temp.go index 377e377..1a0905d 100644 --- a/webapi/Search Temp.go +++ b/webapi/Search Temp.go @@ -38,7 +38,7 @@ func dispatchSearch(input SearchRequest) (job *SearchJob) { job.ResultSync.Lock() for n := 0; n < countResults; n++ { - newFile := createTestResult() + newFile := createTestResult(-1) job.filesCurrent = append(job.filesCurrent, &newFile) } @@ -149,8 +149,8 @@ func (job *SearchJob) ReturnNext(Limit int) (Result []*core.BlockRecordFile) { return job.ReturnResult(Limit) } -// createTestResult creates a test file -func createTestResult() (file core.BlockRecordFile) { +// createTestResult creates a test file. fileType = -1 for any. +func createTestResult(fileType int) (file core.BlockRecordFile) { randomData := make([]byte, 10) rand.Read(randomData) @@ -159,40 +159,81 @@ func createTestResult() (file core.BlockRecordFile) { file.ID = uuid.New() file.Size = uint64(len(randomData)) - switch file.Format { - case core.FormatCSV, core.FormatEmail, core.FormatText, core.FormatHTML: - file.Type = core.TypeText + if fileType == -1 { + switch file.Format { + case core.FormatCSV, core.FormatEmail, core.FormatText, core.FormatHTML: + file.Type = core.TypeText - case core.FormatDatabase: - file.Type = core.TypeBinary + case core.FormatDatabase: + file.Type = core.TypeBinary - case core.FormatCompressed: - file.Type = core.TypeCompressed + case core.FormatCompressed: + file.Type = core.TypeCompressed - case core.FormatContainer: - file.Type = core.TypeContainer + case core.FormatContainer: + file.Type = core.TypeContainer - case core.FormatEbook: - file.Type = core.TypeEbook + case core.FormatEbook: + file.Type = core.TypeEbook - case core.FormatVideo: - file.Type = core.TypeVideo + case core.FormatVideo: + file.Type = core.TypeVideo - case core.FormatAudio: - file.Type = core.TypeAudio + case core.FormatAudio: + file.Type = core.TypeAudio - case core.FormatPicture: - file.Type = core.TypePicture + case core.FormatPicture: + file.Type = core.TypePicture - case core.FormatPowerpoint, core.FormatExcel, core.FormatWord, core.FormatPDF: - file.Type = core.TypeDocument + case core.FormatPowerpoint, core.FormatExcel, core.FormatWord, core.FormatPDF: + file.Type = core.TypeDocument - case core.FormatFolder: - file.Type = core.TypeFolder + case core.FormatFolder: + file.Type = core.TypeFolder - default: - file.Type = core.TypeBinary + default: + file.Type = core.TypeBinary + } + } else { + file.Type = uint8(fileType) + switch file.Type { + case core.TypeBinary: + file.Format = core.FormatBinary + + case core.TypeText: + file.Format = core.FormatText + + case core.TypePicture: + file.Format = core.FormatPicture + + case core.TypeVideo: + file.Format = core.FormatVideo + + case core.TypeAudio: + file.Format = core.FormatAudio + + case core.TypeDocument: + file.Format = core.FormatPDF + + case core.TypeExecutable: + file.Format = core.FormatExecutable + + case core.TypeContainer: + file.Format = core.FormatContainer + + case core.TypeCompressed: + file.Format = core.FormatCompressed + + case core.TypeFolder: + file.Format = core.FormatFolder + + case core.TypeEbook: + file.Format = core.FormatEbook + + default: + file.Format = core.FormatBinary + } } var extension string @@ -247,3 +288,13 @@ func TempFileName(prefix, suffix string) string { rand.Read(randBytes) return prefix + hex.EncodeToString(randBytes) + "." + suffix } + +// queryRecentShared returns recently shared files on the network. fileType = -1 for any. +func queryRecentShared(fileType, limit int) (files []*core.BlockRecordFile) { + for n := 0; n < limit; n++ { + newFile := createTestResult(fileType) + files = append(files, &newFile) + } + + return +} diff --git a/webapi/Search.go b/webapi/Search.go index af12bb2..cca78dc 100644 --- a/webapi/Search.go +++ b/webapi/Search.go @@ -9,6 +9,8 @@ Author: Peter Kleissner /search/result/ws Websocket to return search results as stream (future) /search/statistic Statistics about the results (future) +/explore List recently shared files + */ package webapi @@ -211,3 +213,38 @@ func apiSearchTerminate(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) } + +/* +apiExplore returns recently shared files in Peernet. Results are returned in real-time. The file type is an optional filter. +Request: GET /explore?limit=[max records]&type=[file type] +Result: 200 with JSON structure SearchResult. Check the field status. +*/ +func apiExplore(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + limit, err := strconv.Atoi(r.Form.Get("limit")) + if err != nil { + limit = 100 + } + fileType, err := strconv.Atoi(r.Form.Get("type")) + if err != nil { + fileType = -1 + } + + resultFiles := queryRecentShared(fileType, limit) + + var result SearchResult + result.Files = []apiBlockRecordFile{} + + // loop over results + for n := range resultFiles { + result.Files = append(result.Files, blockRecordFileToAPI(*resultFiles[n])) + } + + if len(result.Files) == 0 { + result.Status = 3 // No results yet available keep trying + } + + result.Status = 1 // No more results to expect + + EncodeJSON(w, r, result) +} diff --git a/webapi/readme.md b/webapi/readme.md index 0806d0e..78c45c4 100644 --- a/webapi/readme.md +++ b/webapi/readme.md @@ -45,6 +45,8 @@ These are the functions provided by the API: /search Submit a search request /search/result Return search results /search/terminate Terminate a search + +/explore List recently shared files ``` # API Documentation @@ -571,4 +573,19 @@ The user can terminate a search early using this function. This helps save syste ``` Request: GET /search/terminate?id=[UUID] Response: 204 Empty -``` \ No newline at end of file +``` + +## Explore + +### List Recently Shared Files + +This returns recently shared files in Peernet. Results are returned in real-time. The file type is an optional filter. + +``` +Request: GET /explore?limit=[max records]&type=[file type] +Result: 200 with JSON structure SearchResult. Check the field status. +``` + +Example request to list 20 recently shared files (all file types): `http://127.0.0.1:112/explore&limit=20` + +Example request to list 10 recent documents: `http://127.0.0.1:112/explore?type=5&limit=10`