From f1e30c942f34802af5faae8142ba37be98186e61 Mon Sep 17 00:00:00 2001 From: Kleissner Date: Fri, 22 Oct 2021 16:55:03 +0200 Subject: [PATCH] webapi: /blockchain/file/add and /blockchain/file/update now always overwrite file size field. Update file now fails if ID is not provided. --- webapi/File.go | 13 +++++++++++-- webapi/readme.md | 4 +++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/webapi/File.go b/webapi/File.go index 25527b2..293dede 100644 --- a/webapi/File.go +++ b/webapi/File.go @@ -148,9 +148,11 @@ func apiBlockchainFileAdd(w http.ResponseWriter, r *http.Request) { if _, err := warehouse.ValidateHash(file.Hash); err != nil { http.Error(w, "", http.StatusBadRequest) 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}) return + } else { + file.Size = uint64(fileInfo.Size()) } } @@ -231,14 +233,21 @@ func apiBlockchainFileUpdate(w http.ResponseWriter, r *http.Request) { var filesAdd []blockchain.BlockRecordFile 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. if !file.IsVirtualFolder() { if _, err := warehouse.ValidateHash(file.Hash); err != nil { http.Error(w, "", http.StatusBadRequest) 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}) return + } else { + file.Size = uint64(fileInfo.Size()) } } diff --git a/webapi/readme.md b/webapi/readme.md index f2c6d66..7f70aaf 100644 --- a/webapi/readme.md +++ b/webapi/readme.md @@ -248,7 +248,7 @@ Below is the list of defined metadata types. Undefined types may be used by clie ### 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. @@ -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. 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. ```