diff --git a/webapi/API.go b/webapi/API.go index f64a6af..789949c 100644 --- a/webapi/API.go +++ b/webapi/API.go @@ -58,6 +58,8 @@ func Start(ListenAddresses []string, UseSSL bool, CertificateFile, CertificateKe Router.HandleFunc("/search/terminate", apiSearchTerminate).Methods("GET") Router.HandleFunc("/explore", apiExplore).Methods("GET") Router.HandleFunc("/file/format", apiFileFormat).Methods("GET") + Router.HandleFunc("/download/start", apiDownloadStart).Methods("GET") + Router.HandleFunc("/download/status", apiDownloadStatus).Methods("GET") for _, listen := range ListenAddresses { go startWebServer(listen, UseSSL, CertificateFile, CertificateKey, Router, "API", TimeoutRead, TimeoutWrite) diff --git a/webapi/Download.go b/webapi/Download.go new file mode 100644 index 0000000..b959b7d --- /dev/null +++ b/webapi/Download.go @@ -0,0 +1,56 @@ +/* +File Name: Download.go +Copyright: 2021 Peernet Foundation s.r.o. +Author: Peter Kleissner +*/ + +package webapi + +import "net/http" + +type apiResponseDownloadStatus struct { + Status int `json:"status"` // Status: 0 = Success, 1 = Error download not found + // TODO: Add progress size, total size, number of peers in swarm, etc. +} + +/* +apiDownloadStart starts the download of a file. The path is the full path on disk to store the file. +The hash parameter identifies the file to download. The blockchain parameter is the node ID of the peer who shared the file on its blockchain (i.e., the "owner" of the file). + +Request: GET /download/start?path=[target path on disk]&hash=[file hash to download]&blockchain=[node ID] +Result: 200 with JSON structure apiResponseDownloadStatus +*/ +func apiDownloadStart(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + filePath := r.Form.Get("path") + hash := r.Form.Get("hash") + blockchain := r.Form.Get("blockchain") + if filePath == "" || hash == "" || blockchain == "" { + http.Error(w, "", http.StatusBadRequest) + return + } + + // TODO + + EncodeJSON(w, r, apiResponseDownloadStatus{Status: 0}) +} + +/* +apiDownloadStatus returns the status of an active download. The hash and blockchain parameters must be the same as /download/start. + +Request: GET /download/status?hash=[file hash]&blockchain=[node ID] +Result: 200 with JSON structure apiResponseDownloadStatus +*/ +func apiDownloadStatus(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + hash := r.Form.Get("hash") + blockchain := r.Form.Get("blockchain") + if hash == "" || blockchain == "" { + http.Error(w, "", http.StatusBadRequest) + return + } + + // TODO + + EncodeJSON(w, r, apiResponseDownloadStatus{Status: 1}) +} diff --git a/webapi/readme.md b/webapi/readme.md index 9f63d9e..eea96da 100644 --- a/webapi/readme.md +++ b/webapi/readme.md @@ -46,6 +46,9 @@ These are the functions provided by the API: /search/result Return search results /search/terminate Terminate a search +/download/start Start the download of a file +/download/status Get the status of a download + /explore List recently shared files /file/format Detect file type and format @@ -579,6 +582,31 @@ Request: GET /search/terminate?id=[UUID] Response: 204 Empty ``` +### Start Download + +This starts the download of a file. The path is the full path on disk to store the file. +The hash parameter identifies the file to download. The blockchain parameter is the node ID of the peer who shared the file on its blockchain (i.e., the "owner" of the file). + +``` +Request: GET /download/start?path=[target path on disk]&hash=[file hash to download]&blockchain=[node ID] +Result: 200 with JSON structure apiResponseDownloadStatus +``` + +```go +type apiResponseDownloadStatus struct { + Status int `json:"status"` // Status: 0 = Success, 1 = Error download not found +} +``` + +### Get Download Status + +This returns the status of an active download. The hash and blockchain parameters must be the same as `/download/start`. + +``` +Request: GET /download/status?hash=[file hash]&blockchain=[node ID] +Result: 200 with JSON structure apiResponseDownloadStatus +``` + ## Explore ### List Recently Shared Files