mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
added changes for progress bar with more logs and bug fixes, Documentation yet to be added
This commit is contained in:
@@ -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
|
||||
)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user