webapi: /blockchain/file/add and /blockchain/file/update now always overwrite file size field. Update file now fails if ID is not provided.

This commit is contained in:
Kleissner
2021-10-22 16:55:03 +02:00
parent 0c4562e1d3
commit f1e30c942f
2 changed files with 14 additions and 3 deletions

View File

@@ -148,9 +148,11 @@ func apiBlockchainFileAdd(w http.ResponseWriter, r *http.Request) {
if _, err := warehouse.ValidateHash(file.Hash); err != nil { if _, err := warehouse.ValidateHash(file.Hash); err != nil {
http.Error(w, "", http.StatusBadRequest) http.Error(w, "", http.StatusBadRequest)
return return
} else if _, _, status, _ := core.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK { } else if _, fileInfo, status, _ := core.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse}) EncodeJSON(w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
return return
} else {
file.Size = uint64(fileInfo.Size())
} }
} }
@@ -231,14 +233,21 @@ func apiBlockchainFileUpdate(w http.ResponseWriter, r *http.Request) {
var filesAdd []blockchain.BlockRecordFile var filesAdd []blockchain.BlockRecordFile
for _, file := range input.Files { for _, file := range input.Files {
if file.ID == uuid.Nil { // if the ID is not provided by the caller, abort
http.Error(w, "", http.StatusBadRequest)
return
}
// Verify that the file exists in the warehouse. Folders are exempt from this check as they are only virtual. // Verify that the file exists in the warehouse. Folders are exempt from this check as they are only virtual.
if !file.IsVirtualFolder() { if !file.IsVirtualFolder() {
if _, err := warehouse.ValidateHash(file.Hash); err != nil { if _, err := warehouse.ValidateHash(file.Hash); err != nil {
http.Error(w, "", http.StatusBadRequest) http.Error(w, "", http.StatusBadRequest)
return return
} else if _, _, status, _ := core.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK { } else if _, fileInfo, status, _ := core.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse}) EncodeJSON(w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
return return
} else {
file.Size = uint64(fileInfo.Size())
} }
} }

View File

@@ -248,7 +248,7 @@ Below is the list of defined metadata types. Undefined types may be used by clie
### Add File ### 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. This adds a file with the provided information to the blockchain. The date field cannot be set by the caller and is ignored. If the ID field is left empty, a random UUID is automatically assigned. The size field is ignored; it will be automatically set to the file size identified by the hash (via the Warehouse). The format and type fields need to be set by the caller; `/file/format` can be used to detect them.
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. 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.
@@ -395,6 +395,8 @@ Example response indicating success:
This updates files that are already published on the blockchain. This is useful for example when changing a file name or description. This updates files that are already published on the blockchain. This is useful for example when changing a file name or description.
Just like with the add file function, the file must be already stored in the Warehouse, otherwise this function fails. Just like with the add file function, the file must be already stored in the Warehouse, otherwise this function fails.
The files are identified by their IDs. If an ID is not set, this function fails with HTTP 400. The size field is ignored; it will be automatically set to the file size identified by the hash (via the Warehouse).
Note as this replaces the previous file record on the blockchain, all details (including special metadata fields) must be included. Note as this replaces the previous file record on the blockchain, all details (including special metadata fields) must be included.
``` ```