From 201a5441bb576aedd86f2289e860c22429b4f058 Mon Sep 17 00:00:00 2001 From: Akilan Date: Fri, 5 May 2023 18:31:08 +0100 Subject: [PATCH] added changes for progress bar with more logs and bug fixes, Documentation yet to be added --- File Formats.go | 43 ++++++++++++++++++++------------------- webapi/API.go | 7 +++++++ webapi/File Detection.go | 3 +++ webapi/File.go | 8 ++++++++ webapi/Merge directory.go | 24 ++++++---------------- webapi/Upload.go | 9 +++++--- webapi/Warehouse.go | 17 ++++++++++++---- 7 files changed, 65 insertions(+), 46 deletions(-) diff --git a/File Formats.go b/File Formats.go index 3d717dd..5326aa8 100644 --- a/File Formats.go +++ b/File Formats.go @@ -25,25 +25,26 @@ const ( // File formats. New ones may be added to the list as required. const ( - FormatBinary = iota // Binary/unspecified - FormatPDF // PDF document - FormatWord // Word document - FormatExcel // Excel - FormatPowerpoint // Powerpoint - FormatPicture // Pictures (including GIF, excluding icons) - FormatAudio // Audio files - FormatVideo // Video files - FormatContainer // Compressed files including ZIP, RAR, TAR and others - FormatHTML // HTML file - FormatText // Text file - FormatEbook // Ebook file - FormatCompressed // Compressed file - FormatDatabase // Database file - FormatEmail // Single email - FormatCSV // CSV file - FormatFolder // Virtual folder - FormatExecutable // Executable file - FormatInstaller // Installer - FormatAPK // APK - FormatISO // ISO + FormatBinary = iota // Binary/unspecified + FormatPDF // PDF document + FormatWord // Word document + FormatExcel // Excel + FormatPowerpoint // Powerpoint + FormatPicture // Pictures (including GIF, excluding icons) + FormatAudio // Audio files + FormatVideo // Video files + FormatContainer // Compressed files including ZIP, RAR, TAR and others + FormatHTML // HTML file + FormatText // Text file + FormatEbook // Ebook file + FormatCompressed // Compressed file + FormatDatabase // Database file + FormatEmail // Single email + FormatCSV // CSV file + FormatFolder // Virtual folder + FormatExecutable // Executable file + FormatInstaller // Installer + FormatAPK // APK + FormatISO // ISO + FormatPeernetSearch // Peernet Search ) diff --git a/webapi/API.go b/webapi/API.go index 94de23b..03c63f7 100644 --- a/webapi/API.go +++ b/webapi/API.go @@ -42,6 +42,13 @@ type WebapiInstance struct { uploadsMutex sync.RWMutex } +// API error +// This follows the same format as the logger +type errorResponse struct { + function string + error string +} + // WSUpgrader is used for websocket functionality. It allows all requests. var WSUpgrader = websocket.Upgrader{ ReadBufferSize: 1024, diff --git a/webapi/File Detection.go b/webapi/File Detection.go index b20fc2f..789064c 100644 --- a/webapi/File Detection.go +++ b/webapi/File Detection.go @@ -94,6 +94,9 @@ func FileTranslateExtension(extension string) (fileType, fileFormat uint16) { case "iso": return core.TypeContainer, core.FormatISO + case "pnsearch": + return core.TypeText, core.FormatPeernetSearch + default: return core.TypeBinary, core.FormatBinary } diff --git a/webapi/File.go b/webapi/File.go index c6f6b0d..37918ab 100644 --- a/webapi/File.go +++ b/webapi/File.go @@ -42,6 +42,7 @@ type apiFile struct { Date time.Time `json:"date"` // Date shared NodeID []byte `json:"nodeid"` // Node ID, owner of the file. Read only. Metadata []apiFileMetadata `json:"metadata"` // Additional metadata. + Profile apiBlockRecordProfile } // --- conversion from core to API data --- @@ -136,6 +137,7 @@ Response: 200 with JSON structure apiBlockchainBlockStatus func (api *WebapiInstance) apiBlockchainFileAdd(w http.ResponseWriter, r *http.Request) { var input apiBlockAddFiles if err := DecodeJSON(w, r, &input); err != nil { + api.Backend.LogError("blockchain.AddFile", "error: %v", "error decoding JSON") return } @@ -143,6 +145,8 @@ func (api *WebapiInstance) apiBlockchainFileAdd(w http.ResponseWriter, r *http.R for _, file := range input.Files { if len(file.Hash) != protocol.HashSize { + api.Backend.LogError("blockchain.AddFile", "error: %v", "file length is not the same length as "+ + "the protocol hash size.") http.Error(w, "", http.StatusBadRequest) return } @@ -153,6 +157,7 @@ func (api *WebapiInstance) apiBlockchainFileAdd(w http.ResponseWriter, r *http.R // 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 { + api.Backend.LogError("blockchain.AddFile", "error: %v", err) http.Error(w, "", http.StatusBadRequest) return } else if _, fileSize, status, _ := api.Backend.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK { @@ -179,6 +184,9 @@ func (api *WebapiInstance) apiBlockchainFileAdd(w http.ResponseWriter, r *http.R newHeight, newVersion, status := api.Backend.UserBlockchain.AddFiles(filesAdd) + // Temporary log to check the output for warehouse API + api.Backend.LogError("blockchain.AddFile", "output %v", apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion}) + EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion}) } diff --git a/webapi/Merge directory.go b/webapi/Merge directory.go index 92df57e..e0b049b 100644 --- a/webapi/Merge directory.go +++ b/webapi/Merge directory.go @@ -72,6 +72,8 @@ func (api *WebapiInstance) ExploreFileSharedByNodeThatSharedSimilarFile(fileType result.Files = append(result.Files, blockRecordFileToAPI(resultFiles[n])) } + // + } result.Status = 1 // No more results to expect @@ -86,8 +88,6 @@ func (api *WebapiInstance) GreedySearchMergeDirection(nodeID *[][]byte, fileType // get all NodeIDs peerList := api.Backend.PeerlistGet() - //var tags []blockchain.BlockRecordFileTag - // search with AllNodes which have a match of the NodeID. for _, peer := range peerList { if peer.BlockchainHeight == 0 { @@ -112,28 +112,16 @@ func (api *WebapiInstance) GreedySearchMergeDirection(nodeID *[][]byte, fileType file.Tags = append(file.Tags, blockchain.TagFromText(blockchain.TagSharedByGeoIP, sharedByGeoIP)) } - // found a new file! append. - filesFromPeer++ - // if both the hashes match if bytes.Equal(file.Hash, hash) { // set the Tags needed for the filter parameter //tags = file.Tags + + // found a new file! append. + filesFromPeer++ + *nodeID = append(*nodeID, file.NodeID) } - - //for _, tag := range file.Tags { - // // checks if any of tags from - // // the NodeID provided matches - // // Requires better search parameters - // // So that it's more narrow - // for i := range tags { - // if tag.Text() == tags[i].Text() { - // *nodeID = append(*nodeID, file.NodeID) - // break - // } - // } - //} } } diff --git a/webapi/Upload.go b/webapi/Upload.go index 0331534..c50cb00 100644 --- a/webapi/Upload.go +++ b/webapi/Upload.go @@ -4,12 +4,15 @@ import ( "github.com/google/uuid" "math" "net/http" + "sync" ) type UploadStatus struct { - APIStatus int `json:"apistatus"` // Status of the API call. See DownloadResponseX. - ID uuid.UUID `json:"id"` // Download ID. This can be used to query the latest status and take actions. - UploadStatus int `json:"uploadstatus"` // Status of the download. See DownloadX. + APIStatus int `json:"apistatus"` // Status of the API call. See DownloadResponseX. + ID uuid.UUID `json:"id"` // Download ID. This can be used to query the latest status and take actions. + sync.RWMutex // Mutext for changing the status + + UploadStatus int `json:"uploadstatus"` // Status of the download. See DownloadX. Progress struct { TotalSize uint64 `json:"totalsize"` // Total size in bytes. UploadedSize uint64 `json:"uploadedsize"` // Count of bytes download so far. diff --git a/webapi/Warehouse.go b/webapi/Warehouse.go index 01d18d1..f7c4e05 100644 --- a/webapi/Warehouse.go +++ b/webapi/Warehouse.go @@ -28,36 +28,42 @@ Response: 200 with JSON structure WarehouseResult */ func (api *WebapiInstance) ApiWarehouseCreateFile(w http.ResponseWriter, r *http.Request) { // changing parameter to take ID as a parameter for upload and file itself - ID := r.FormValue("ID") + ID := r.FormValue("id") file, handler, err := r.FormFile("File") if err != nil { api.Backend.LogError("warehouse.CreateFile", "error: %v", err) - http.Error(w, "", http.StatusBadRequest) + EncodeJSON(api.Backend, w, r, errorResponse{function: "warehouse.CreateFile", error: err.Error()}) return } var hash []byte var status int + // checks if there is a new upload and then if ID != "" { IDUUID, err := uuid.Parse(ID) if err != nil { api.Backend.LogError("warehouse.CreateFile", "error: %v", err) - http.Error(w, "", http.StatusBadRequest) + EncodeJSON(api.Backend, w, r, errorResponse{function: "warehouse.CreateFile", error: err.Error()}) return } + info := api.uploadLookup(IDUUID) if info == nil { var newInfo UploadStatus newInfo.ID = IDUUID newInfo.Progress.TotalSize = uint64(handler.Size) + api.Backend.LogError("warehouse.CreateFile", "%v", newInfo) api.uploadAdd(&newInfo) hash, status, err = api.Backend.UserWarehouse.CreateFile(file, uint64(handler.Size), &newInfo) } else { info.Progress.TotalSize = uint64(handler.Size) + api.Backend.LogError("warehouse.CreateFile", "%v", info) hash, status, err = api.Backend.UserWarehouse.CreateFile(file, uint64(handler.Size), info) } + api.Backend.LogError("warehouse.CreateFile", "outside Create file: %v", info) + } else { // File := r. hash, status, err = api.Backend.UserWarehouse.CreateFile(file, uint64(handler.Size), nil) @@ -65,10 +71,13 @@ func (api *WebapiInstance) ApiWarehouseCreateFile(w http.ResponseWriter, r *http if err != nil { api.Backend.LogError("warehouse.CreateFile", "status %d error: %v", status, err) - http.Error(w, "", http.StatusBadRequest) + EncodeJSON(api.Backend, w, r, errorResponse{function: "warehouse.CreateFile", error: err.Error()}) return } + // Temporary log to check the output for warehouse API + api.Backend.LogError("warehouse.CreateFile", "output %v", WarehouseResult{Status: status, Hash: hash}) + EncodeJSON(api.Backend, w, r, WarehouseResult{Status: status, Hash: hash}) }