From df0f57063fedd483b5a5afd754d13155379f8fdc Mon Sep 17 00:00:00 2001 From: Kleissner Date: Sun, 19 Sep 2021 00:57:49 +0200 Subject: [PATCH] Add endpoint /blockchain/self/delete/file to delete files. --- webapi/API.go | 1 + webapi/Blockchain.go | 36 +++++++++++++++++++++++++----- webapi/Profile.go | 4 ++-- webapi/readme.md | 53 +++++++++++++++++++++++++++++++++++--------- 4 files changed, 75 insertions(+), 19 deletions(-) diff --git a/webapi/API.go b/webapi/API.go index 9dbff8a..3467986 100644 --- a/webapi/API.go +++ b/webapi/API.go @@ -47,6 +47,7 @@ func Start(ListenAddresses []string, UseSSL bool, CertificateFile, CertificateKe 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") + Router.HandleFunc("/blockchain/self/delete/file", apiBlockchainSelfDeleteFile).Methods("POST") Router.HandleFunc("/profile/list", apiProfileList).Methods("GET") Router.HandleFunc("/profile/read", apiProfileRead).Methods("GET") Router.HandleFunc("/profile/write", apiProfileWrite).Methods("POST") diff --git a/webapi/Blockchain.go b/webapi/Blockchain.go index 73bcc71..cb17fc2 100644 --- a/webapi/Blockchain.go +++ b/webapi/Blockchain.go @@ -47,8 +47,9 @@ type apiBlockchainBlockRaw struct { } type apiBlockchainBlockStatus struct { - Status int `json:"status"` // Status: 0 = Success, 1 = Error invalid data - Height uint64 `json:"height"` // New height of the blockchain (number of blocks). + Status int `json:"status"` // Status: 0 = Success, 1 = Error invalid data + Height uint64 `json:"height"` // Height of the blockchain (number of blocks). + Version uint64 `json:"version"` // Version of the blockchain. } /* @@ -70,9 +71,9 @@ func apiBlockchainSelfAppend(w http.ResponseWriter, r *http.Request) { records = append(records, core.BlockRecordRaw{Type: record.Type, Data: record.Data}) } - newHeight, status := core.UserBlockchainAppend(records) + newHeight, newVersion, status := core.UserBlockchainAppend(records) - EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight}) + EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion}) } type apiBlockchainBlock struct { @@ -205,9 +206,9 @@ func apiBlockchainSelfAddFile(w http.ResponseWriter, r *http.Request) { filesAdd = append(filesAdd, blockRecordFileFromAPI(file)) } - newHeight, status := core.UserBlockchainAddFiles(filesAdd) + newHeight, newVersion, status := core.UserBlockchainAddFiles(filesAdd) - EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight}) + EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion}) } /* @@ -230,6 +231,29 @@ func apiBlockchainSelfListFile(w http.ResponseWriter, r *http.Request) { EncodeJSON(w, r, result) } +/* +apiBlockchainSelfDeleteFile deletes files with the provided IDs. Other fields are ignored. + +Request: POST /blockchain/self/delete/file with JSON structure apiBlockAddFiles +Response: 200 with JSON structure apiBlockchainBlockStatus +*/ +func apiBlockchainSelfDeleteFile(w http.ResponseWriter, r *http.Request) { + var input apiBlockAddFiles + if err := DecodeJSON(w, r, &input); err != nil { + return + } + + var deleteIDs []uuid.UUID + + for n := range input.Files { + deleteIDs = append(deleteIDs, input.Files[n].ID) + } + + newHeight, newVersion, status := core.UserBlockchainDeleteFiles(deleteIDs) + + EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion}) +} + // --- conversion from core to API data --- func isFileTagKnownMetadata(tagType uint16) bool { diff --git a/webapi/Profile.go b/webapi/Profile.go index f245293..b466c03 100644 --- a/webapi/Profile.go +++ b/webapi/Profile.go @@ -96,9 +96,9 @@ func apiProfileWrite(w http.ResponseWriter, r *http.Request) { profile.Blobs = append(profile.Blobs, core.BlockRecordProfileBlob{Type: input.Blobs[n].Type, Data: input.Blobs[n].Data}) } - newHeight, status := core.UserProfileWrite(profile) + newHeight, newVersion, status := core.UserProfileWrite(profile) - EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight}) + EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion}) } // --- conversion from core to API data --- diff --git a/webapi/readme.md b/webapi/readme.md index 0676e07..e62000b 100644 --- a/webapi/readme.md +++ b/webapi/readme.md @@ -124,8 +124,8 @@ type apiBlockchainBlockRaw struct { type apiBlockchainBlockStatus struct { Status int `json:"status"` // Status: 0 = Success, 1 = Error invalid data - Version uint64 `json:"version"` // Current version number of the blockchain. Height uint64 `json:"height"` // Height of the blockchain (number of blocks). + Version uint64 `json:"version"` // Version of the blockchain. } ``` @@ -202,10 +202,16 @@ type apiFileTagRaw struct { } ``` -### Blockchain Add File +## File Functions + +These functions allow adding, deleting, and listing files stored on the users blockchain. Only metadata is actually stored on the blockchain. + +### Add File This adds a file with the provided information to the blockchain. The date field cannot be set by the caller and is ignored. +Any file added is publicly accessible. The user should be informed about this fact in advance. The user is responsible and liable for any files shared. + ``` Request: POST /blockchain/self/add/file with JSON structure apiBlockAddFiles Response: 200 with JSON structure apiBlockchainBlockStatus @@ -215,11 +221,6 @@ Response: 200 with JSON structure apiBlockchainBlockStatus type apiBlockAddFiles struct { Files []apiBlockRecordFile `json:"files"` } - -type apiBlockchainBlockStatus struct { - Status int `json:"status"` // Status: 0 = Success, 1 = Error invalid data - Height uint64 `json:"height"` // New height of the blockchain (number of blocks). -} ``` Example POST request to `http://127.0.0.1:112/blockchain/self/add/file`: @@ -241,7 +242,7 @@ Example POST request to `http://127.0.0.1:112/blockchain/self/add/file`: } ``` -Another example to create a new file but with a new arbitrary tag with type number 100 set to "test" and setting the metadata field "Date Created" (which is type 2 = `core.TagTypeDateCreated`): +Another payload example to create a new file but with a new arbitrary tag with type number 100 set to "test" and setting the metadata field "Date Created" (which is type 2 = `core.TagTypeDateCreated`): ```json { @@ -266,7 +267,7 @@ Another example to create a new file but with a new arbitrary tag with type numb } ``` -### Blockchain List Files +### List Files This lists all files stored on the blockchain. @@ -275,7 +276,7 @@ Request: GET /blockchain/self/list/file Response: 200 with JSON structure apiBlockAddFiles ``` -Example output: +Example response: ```json { @@ -296,6 +297,35 @@ Example output: } ``` +### Delete File + +This deletes files from the blockchain with the provided IDs. The blockchain will be refactored, which means it is recalculated without the specified files. The blockchains version number might be increased. + +``` +Request: POST /blockchain/self/delete/file with JSON structure apiBlockAddFiles +Response: 200 with JSON structure apiBlockchainBlockStatus +``` + +Example POST request to `http://127.0.0.1:112/blockchain/self/delete/file`: + +```json +{ + "files": [{ + "id": "236de31d-f402-4389-bdd1-56463abdc309" + }] +} +``` + +Example response: + +```json +{ + "status": 0, + "height": 7, + "version": 1 +} +``` + ## Profile Functions ### Profile List @@ -404,7 +434,8 @@ Example response: ```json { "status": 0, - "height": 1 + "height": 1, + "version": 0 } ```