mirror of
https://github.com/PeernetOfficial/WebGatewayUpload.git
synced 2026-07-16 19:47:51 +01:00
This commit is contained in:
10
README.md
10
README.md
@@ -11,10 +11,14 @@ go build .
|
||||
```
|
||||
|
||||
## Run
|
||||
Run on default parameters
|
||||
Run on default parameters (With Debug output)
|
||||
```
|
||||
./WebGatewayUpload
|
||||
```
|
||||
Run on Production mode
|
||||
```
|
||||
./WebGatewayUpload -Production
|
||||
```
|
||||
Custom Flags
|
||||
```
|
||||
./WebGatewayUpload -h
|
||||
@@ -26,6 +30,8 @@ Usage of ./WebGatewayUpload:
|
||||
SSL Certificate file (default "server.crt")
|
||||
-Key string
|
||||
SSL Key file (default "server.key")
|
||||
-Production
|
||||
Flag to check if required to run on production mode
|
||||
-SSL
|
||||
Flag to check if the SSL certificate is enabled or not
|
||||
-WebpageAddress string
|
||||
@@ -34,7 +40,7 @@ Usage of ./WebGatewayUpload:
|
||||
|
||||
## Routes
|
||||
- (GET) `/upload` (Opens upload page in the webgateway)
|
||||
- (POST) `/uploadFile` (Uploads file to peernet from Webpage)
|
||||
- (POST) `/upload` (Uploads file to peernet from Webpage)
|
||||
- (POST) `/uploadCurl` (Uploads file from CURL)
|
||||
|
||||
Ex:
|
||||
|
||||
49
main.go
49
main.go
@@ -35,6 +35,8 @@ var (
|
||||
Key *string
|
||||
// BackendAddressWithHTTP ex: http://<address>:<port no>
|
||||
BackendAddressWithHTTP string
|
||||
// Production mode
|
||||
Production *bool
|
||||
)
|
||||
|
||||
// -------------------------------------------------------------------------------------------------------------
|
||||
@@ -48,11 +50,12 @@ func init() {
|
||||
SSL = flag.Bool("SSL", false, "Flag to check if the SSL certificate is enabled or not")
|
||||
Certificate = flag.String("Certificate", "server.crt", "SSL Certificate file")
|
||||
Key = flag.String("Key", "server.key", "SSL Key file")
|
||||
Production = flag.Bool("Production", false, "Flag to check if required to run on production mode")
|
||||
}
|
||||
|
||||
// InitPeernet Initializes Peernet backend
|
||||
func InitPeernet() *core.Backend {
|
||||
backend, status, err := core.Init("Your application/1.0", "Config.yaml", nil, nil)
|
||||
backend, status, err := core.Init("Peernet Upload Application/1.0", "Config.yaml", nil, nil)
|
||||
if status != core.ExitSuccess {
|
||||
fmt.Printf("Error %d initializing backend: %s\n", status, err.Error())
|
||||
return nil
|
||||
@@ -95,7 +98,7 @@ type BlockchainRequest struct {
|
||||
|
||||
type File struct {
|
||||
Hash []byte `json:"hash"`
|
||||
Type int `json:"type"`
|
||||
Type uint16 `json:"type"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
@@ -133,10 +136,10 @@ func AddFileWarehouse(file io.Reader) *WarehouseResult {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
//fmt.Println("response Status:", resp.Status)
|
||||
//fmt.Println("response Headers:", resp.Header)
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
//fmt.Println("response Body:", string(body))
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
var result WarehouseResult
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
@@ -155,7 +158,6 @@ func UploadFile(backend *core.Backend, file *multipart.File, header *multipart.F
|
||||
|
||||
// adds file to warehouse
|
||||
warehouseResult := AddFileWarehouse(buf)
|
||||
fmt.Println(warehouseResult.Hash)
|
||||
// current using default port for Peernet api which is 8080
|
||||
// First add file to warehouse
|
||||
|
||||
@@ -166,7 +168,6 @@ func UploadFile(backend *core.Backend, file *multipart.File, header *multipart.F
|
||||
}
|
||||
|
||||
_, publicKey := backend.ExportPrivateKey()
|
||||
fmt.Println(hex.EncodeToString(publicKey.SerializeCompressed()))
|
||||
|
||||
return publicKey, warehouseResult, nil
|
||||
}
|
||||
@@ -178,12 +179,18 @@ func UploadFile(backend *core.Backend, file *multipart.File, header *multipart.F
|
||||
func AddFileToBlockchain(hash []byte, filename string) *BlockchainResponse {
|
||||
url := BackendAddressWithHTTP + "/blockchain/file/add"
|
||||
|
||||
// Get file type
|
||||
detectType, _, err := webapi.FileDetectType(filename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Create file object for post
|
||||
var blockchainRequest BlockchainRequest
|
||||
var files File
|
||||
files.Name = filename
|
||||
files.Hash = hash
|
||||
files.Type = 0
|
||||
files.Type = detectType
|
||||
blockchainRequest.Files = append(blockchainRequest.Files, files)
|
||||
|
||||
Byte, err := json.Marshal(blockchainRequest)
|
||||
@@ -226,12 +233,19 @@ func main() {
|
||||
backend := InitPeernet()
|
||||
go RunPeernet(backend)
|
||||
|
||||
r := gin.Default()
|
||||
var r *gin.Engine
|
||||
if *Production {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r = gin.New()
|
||||
} else {
|
||||
r = gin.Default()
|
||||
}
|
||||
|
||||
r.LoadHTMLGlob("templates/*.html")
|
||||
r.Static("/templates", "./templates")
|
||||
|
||||
// --------------------------------- Middleware rate limiter -----------------------------------
|
||||
lm := limiter.NewRateLimiter(time.Minute, 2, func(ctx *gin.Context) (string, error) {
|
||||
lm := limiter.NewRateLimiter(time.Minute, 10, func(ctx *gin.Context) (string, error) {
|
||||
return "", nil
|
||||
})
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
@@ -243,17 +257,24 @@ func main() {
|
||||
})
|
||||
|
||||
// POST /uploadFile Uploads file to peernet from Webgateway
|
||||
r.POST("/uploadFile", lm.Middleware(), func(c *gin.Context) {
|
||||
r.POST("/upload", lm.Middleware(), func(c *gin.Context) {
|
||||
file, header, err := c.Request.FormFile("file")
|
||||
defer file.Close()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
c.HTML(http.StatusBadRequest, "upload.html", gin.H{
|
||||
"error": "File not added during upload",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
publicKey, warehouseResult, err := UploadFile(backend, &file, header)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
c.HTML(http.StatusBadRequest, "upload.html", gin.H{
|
||||
"error": "File not added during upload",
|
||||
})
|
||||
return
|
||||
//fmt.Println(err)
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "upload.html", gin.H{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<title>Peernet Download file</title>
|
||||
<title>Peernet Upload file</title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
|
||||
@@ -25,18 +25,18 @@
|
||||
<h2 class="Peernetblue">Upload file to peernet</h2>
|
||||
<br>
|
||||
<br>
|
||||
<form class="form-group" method="POST" action="/uploadFile" enctype="multipart/form-data">
|
||||
<form class="form-group" method="POST" action="/upload" enctype="multipart/form-data">
|
||||
<div class="form-group mb-3">
|
||||
<!-- <label for="formFileLg" class="form-label">Upload your file</label> -->
|
||||
<input class="btn btn-white btn-outline-white" id="formFileLg" type="file" name="file">
|
||||
<input class="btn btn-white btn-outline-white" id="formFileLg" type="file" name="file" required>
|
||||
</div>
|
||||
<input class="btn btn-white btn-outline-white" type="submit" value="Submit">
|
||||
<!-- {{ if .hash }}
|
||||
<h3> hash: {{ .hash }} </h3>
|
||||
{{end}} -->
|
||||
{{ if .error }}
|
||||
<h3 style="color:red"> Error: {{ .error }} </h3>
|
||||
{{end}}
|
||||
</form>
|
||||
<h3 style="color:white"> or </h3>
|
||||
<h4 style="color:white">curl {{ .address }}/uploadCurl -F add=@<file name></h4>
|
||||
<!-- <h3 style="color:white"> or </h3>-->
|
||||
<!-- <h4 style="color:white">curl {{ .address }}/uploadCurl -F add=@<file name></h4>-->
|
||||
<!-- <a href="#" class="btn btn-white btn-outline-white" tyle="color:#3ac4e2">Upload file</a> -->
|
||||
<br>
|
||||
<br>
|
||||
@@ -69,17 +69,17 @@
|
||||
<p> {{ .size }} bytes</p>
|
||||
{{end}}
|
||||
</div>
|
||||
<div class="form-group mb-3">
|
||||
<label class="label" for="name">File Hash:</label>
|
||||
{{ if .hash }}
|
||||
<p> {{ .hash }} </p>
|
||||
{{end}}
|
||||
</div>
|
||||
<!-- <div class="form-group mb-3">-->
|
||||
<!-- <label class="label" for="name">File Hash:</label>-->
|
||||
<!-- {{ if .hash }}-->
|
||||
<!-- <p> {{ .hash }} </p>-->
|
||||
<!-- {{end}}-->
|
||||
<!-- </div>-->
|
||||
<div class="form-group mb-3">
|
||||
<label class="label" for="name">Sharable Link:</label>
|
||||
<br>
|
||||
{{ if .link }}
|
||||
<a href="{{ .link }}" style="color:#1d91ac">{{ .filename }} link</a>
|
||||
<a href="{{ .link }}" style="color:#1d91ac" target="_blank">{{ .filename }} link</a>
|
||||
{{end}}
|
||||
</div>
|
||||
<!-- <div class="form-group mb-3">
|
||||
@@ -89,7 +89,7 @@
|
||||
<label class="label" for="name">File Name:</label>
|
||||
<p>Test.txt</p>
|
||||
</div> -->
|
||||
</div>
|
||||
<!-- </div>-->
|
||||
<div class="form-group d-md-flex">
|
||||
<!-- <div class="w-50 text-left">
|
||||
<label class="checkbox-wrap checkbox-primary mb-0">Remember Me
|
||||
|
||||
Reference in New Issue
Block a user