diff --git a/API.go b/API.go index 7226486..f4c5601 100644 --- a/API.go +++ b/API.go @@ -42,6 +42,7 @@ func startAPI() { router.HandleFunc("/blockchain/self/append", apiBlockchainSelfAppend).Methods("POST") router.HandleFunc("/blockchain/self/read", apiBlockchainSelfRead).Methods("GET") router.HandleFunc("/blockchain/self/add/file", apiBlockchainSelfAddFile).Methods("POST") + router.HandleFunc("/blockchain/self/list/file", apiBlockchainSelfListFile).Methods("GET") for _, listen := range config.APIListen { go startWebServer(listen, config.APIUseSSL, config.APICertificateFile, config.APICertificateKey, router, "API", parseDuration(config.APITimeoutRead), parseDuration(config.APITimeoutWrite)) @@ -343,7 +344,8 @@ func apiBlockchainSelfRead(w http.ResponseWriter, r *http.Request) { // apiBlockAddFiles contains the file metadata to add to the blockchain type apiBlockAddFiles struct { - Files []apiBlockRecordFile `json:"files"` + Files []apiBlockRecordFile `json:"files"` // List of files + Status int `json:"status"` // Status of the operation, only used when this structure is returned from the API. } /* @@ -368,3 +370,23 @@ func apiBlockchainSelfAddFile(w http.ResponseWriter, r *http.Request) { apiEncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight}) } + +/* +apiBlockchainSelfListFile lists all files stored on the blockchain. + +Request: GET /blockchain/self/list/file +Response: 200 with JSON structure apiBlockAddFiles +*/ +func apiBlockchainSelfListFile(w http.ResponseWriter, r *http.Request) { + files, status := core.UserBlockchainListFiles() + + var result apiBlockAddFiles + + for _, file := range files { + result.Files = append(result.Files, apiBlockRecordFile{Hash: file.Hash, Type: file.Type, Format: file.Format, Size: file.Size, Directory: file.Directory, Name: file.Name}) + } + + result.Status = status + + apiEncodeJSON(w, r, result) +} diff --git a/API.md b/API.md index bd710d8..59891ad 100644 --- a/API.md +++ b/API.md @@ -43,6 +43,7 @@ These are the functions provided by the API: /blockchain/self/append Append a block to the blockchain /blockchain/self/read Read a block of the blockchain /blockchain/self/add/file Add file to the blockchain +/blockchain/self/list/file List all files stored on the blockchain ``` The `/share` functions are providing high-level functionality to work with files. The `/blockchain` functions provide low-level functionality which is typically not needed. @@ -201,3 +202,44 @@ type apiBlockchainBlockStatus struct { Height uint64 `json:"height"` // New height of the blockchain (number of blocks). } ``` + +Example POST request data to `http://127.0.0.1:112/blockchain/self/add/file`: + +```json +{ + "files": [{ + "hash": "aFad3zRACbk44dsOw5sVGxYmz+Rqh8ORDcGJNqIz+Ss=", + "type": 1, + "format": 10, + "size": 4, + "name": "Test.txt", + "directory": "sample directory/sub folder" + }] +} +``` + +## Blockchain List Files + +This lists all files stored on the blockchain. + +``` +Request: GET /blockchain/self/list/file + +Response: 200 with JSON structure apiBlockAddFiles +``` + +Example output: + +```json +{ + "files": [{ + "hash": "aFad3zRACbk44dsOw5sVGxYmz+Rqh8ORDcGJNqIz+Ss=", + "type": 1, + "format": 10, + "size": 4, + "directory": "sample directory/sub folder", + "name": "Test.txt" + }], + "status": 0 +} +``` diff --git a/go.mod b/go.mod index 6bb9a53..f7db058 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/PeernetOfficial/Cmd go 1.16 require ( - github.com/PeernetOfficial/core v0.0.0-20210819021308-a2420b8468b1 + github.com/PeernetOfficial/core v0.0.0-20210819144937-6c399a7be93c github.com/btcsuite/btcd v0.22.0-beta.0.20210625194946-86a17263b0ff github.com/gorilla/mux v1.8.1-0.20200912192056-d07530f46e1e github.com/gorilla/websocket v1.4.3-0.20210424162022-e8629af678b7 diff --git a/go.sum b/go.sum index 7199165..0dbdab4 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/PeernetOfficial/core v0.0.0-20210819021308-a2420b8468b1 h1:FTI8IWp3NN7ypWz+nTcERpFlBlE6PVzt1bhSTfzPd2Q= -github.com/PeernetOfficial/core v0.0.0-20210819021308-a2420b8468b1/go.mod h1:upfaI2iJJ7lyH+UtOLU7t6oVcLO7PrDF7SsSUWJJ/OY= +github.com/PeernetOfficial/core v0.0.0-20210819144937-6c399a7be93c h1:2xOWG1AHIp41IQ1KyIjPUzszkZnKk58izxh27d+1GHk= +github.com/PeernetOfficial/core v0.0.0-20210819144937-6c399a7be93c/go.mod h1:upfaI2iJJ7lyH+UtOLU7t6oVcLO7PrDF7SsSUWJJ/OY= github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= github.com/akrylysov/pogreb v0.10.1 h1:FqlR8VR7uCbJdfUob916tPM+idpKgeESDXOA1K0DK4w= github.com/akrylysov/pogreb v0.10.1/go.mod h1:pNs6QmpQ1UlTJKDezuRWmaqkgUE2TuU0YTWyqJZ7+lI=