forked from dikshabagdi/ipfs-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (61 loc) · 1.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"net/http"
"path/filepath"
"os"
"io/ioutil"
"github.com/gin-gonic/gin"
"ipfs_api/pkg/our_infura"
//"github.com/wabarc/ipfs-pinner/pkg/infura"
)
func main() {
router := gin.Default()
root := router.Group("/")
{
root.POST("upload", upload)
root.POST("retrieve", retrieve)
}
router.Run(":9090")
}
func upload(c *gin.Context) {
file, err := c.FormFile("uploadFile")
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
}
if file != nil{
filename := filepath.Base(file.Filename)
if err := c.SaveUploadedFile(file, "uploadedFiles/"+filename); err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s \n", err.Error()))
}
c.String(http.StatusOK, fmt.Sprintf("File %s uploaded successfully \n", file.Filename))
//------------------------------------------------------------------------------------------
filePath := filepath.Join("/home/oem/go/src/ipfs_api/uploadedFiles/", filename)
cid, err := ipfs_protocol.PinFile(filePath)
//cid, err := infura.PinFile(filePath)
if err != nil {
c.String(http.StatusOK, fmt.Sprintf("ipfs-pinner: %s \n", err.Error()))
} else {
c.String(http.StatusOK, fmt.Sprintf("Pinned file hash: \n %s \n", cid))
}
}
}
func retrieve(c *gin.Context) {
hash := c.PostForm("retrieveFile")
if hash != ""{
data, err := ipfs_protocol.RetrieveFile(hash)
if err != nil {
c.String(http.StatusOK, fmt.Sprintf("File retrieval error: %s \n", err.Error()))
} else {
filePath := filepath.Join("/home/oem/go/src/ipfs_api/retrievedFile/", hash)
var file, errf = os.Create(filePath)
if errf != nil { return }
defer file.Close()
err = ioutil.WriteFile(filePath, []byte(data),0644)
if err!=nil{
c.String(http.StatusOK, fmt.Sprintf("Writing file error: %s \n", err.Error()))
}
c.String(http.StatusOK, fmt.Sprintf("File retried at:\n%s \n", filePath))
}
}
}