Automatically delete files from Warehouse in case there are no other references when deleting files from the blockchain via /blockchain/self/delete/file.

New function blockchain.FileExists.
blockchain.DeleteFiles now returns list of deleted files.
This commit is contained in:
Kleissner
2021-10-13 06:00:40 +02:00
parent 335f83d79b
commit c2f110169a
5 changed files with 43 additions and 7 deletions

View File

@@ -44,7 +44,7 @@ type apiBlockchainBlockRaw struct {
}
type apiBlockchainBlockStatus struct {
Status int `json:"status"` // See BlockchainStatusX.
Status int `json:"status"` // See blockchain.StatusX.
Height uint64 `json:"height"` // Height of the blockchain (number of blocks).
Version uint64 `json:"version"` // Version of the blockchain.
}
@@ -74,7 +74,7 @@ func apiBlockchainSelfAppend(w http.ResponseWriter, r *http.Request) {
}
type apiBlockchainBlock struct {
Status int `json:"status"` // Status: 0 = Success, 1 = Error block not found, 2 = Error block encoding (indicates that the blockchain is corrupt)
Status int `json:"status"` // See blockchain.StatusX.
PeerID string `json:"peerid"` // Peer ID hex encoded.
LastBlockHash []byte `json:"lastblockhash"` // Hash of the last block. Blake3.
BlockchainVersion uint64 `json:"blockchainversion"` // Blockchain version

View File

@@ -184,6 +184,7 @@ func apiBlockchainSelfListFile(w http.ResponseWriter, r *http.Request) {
/*
apiBlockchainSelfDeleteFile deletes files with the provided IDs. Other fields are ignored.
It will automatically delete the file in the Warehouse if there are no other references.
Request: POST /blockchain/self/delete/file with JSON structure apiBlockAddFiles
Response: 200 with JSON structure apiBlockchainBlockStatus
@@ -200,7 +201,16 @@ func apiBlockchainSelfDeleteFile(w http.ResponseWriter, r *http.Request) {
deleteIDs = append(deleteIDs, input.Files[n].ID)
}
newHeight, newVersion, status := core.UserBlockchain.DeleteFiles(deleteIDs)
newHeight, newVersion, deletedFiles, status := core.UserBlockchain.DeleteFiles(deleteIDs)
// If successfully deleted from the blockchain, delete from the Warehouse in case there are no other references.
if status == blockchain.BlockchainStatusOK {
for n := range deletedFiles {
if files, status := core.UserBlockchain.FileExists(deletedFiles[n].Hash); status == blockchain.BlockchainStatusOK && len(files) == 0 {
core.UserWarehouse.DeleteFile(deletedFiles[n].Hash)
}
}
}
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
}

View File

@@ -345,6 +345,8 @@ Example response:
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.
It will automatically delete the file in the Warehouse if there are no other references.
```
Request: POST /blockchain/self/delete/file with JSON structure apiBlockAddFiles
Response: 200 with JSON structure apiBlockchainBlockStatus
@@ -360,7 +362,7 @@ Example POST request to `http://127.0.0.1:112/blockchain/self/delete/file`:
}
```
Example response:
Example response indicating success:
```json
{