Skip to content

Commit dbb4f13

Browse files
committed
refactor code for better consistency, still a small issue with the readIPFSREsponse function which return an empty json (even though it is working when checked with wireshark)
1 parent 73ab445 commit dbb4f13

File tree

1 file changed

+94
-75
lines changed

1 file changed

+94
-75
lines changed

client/api.go

Lines changed: 94 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"net/http"
1515
"net/url"
1616
"os"
17-
"path"
1817
"time"
1918
)
2019

@@ -61,103 +60,123 @@ func NewLocalApi() (*Client, error) {
6160
return NewIPFSApi("http://127.0.0.1:5001", 4)
6261
}
6362

64-
// The add function upload a new file to IPFS
65-
// It takes the path to the file to upload as a parameter
66-
// Upon successful upload it return an IPFSResponse struct and nil
67-
// In case of error the IPFSResponse is set to nil and an error is returned
68-
//NOTE By default the file will be pinned.
63+
// The add function upload a new file to IPFS It takes the path to the file to upload as a parameter Upon successful upload it return an IPFSResponse struct and nil In case of error the IPFSResponse is set to nil and an error is returned NOTE By default the file will be pinned.
6964
func (client *Client) Add(pathName string) (*IPFSResponse, error) {
70-
// initalizing variable needed
71-
var apiResponse *http.Response
72-
multiPartBody := new(bytes.Buffer)
7365

74-
//Create the multipart body
75-
writer := multipart.NewWriter(multiPartBody)
76-
boundary, err := createMultiPartBody(pathName, writer)
77-
if err != nil {
78-
return nil, err
79-
}
80-
writer.Close()
66+
var request *http.Request
8167

82-
// The sending part
83-
req, err := http.NewRequest("POST", client.url + apiEndpoint["add"] , multiPartBody)
84-
contentType := fmt.Sprintf("multipart/form-data; boundary=%s", boundary)
85-
req.Header.Set("Content-Type", contentType)
86-
if err != nil {
87-
return nil, err
88-
}
68+
//do the preprocessing of the pathName given
69+
fileInfo, err := os.Stat(pathName)
70+
if err != nil {
71+
return nil, err
72+
}
73+
if fileInfo.IsDir() {
74+
request, err = client.createDirectoryMultiPartBody(pathName)
75+
if err != nil {
76+
return nil, err
77+
}
78+
} else {
79+
file, err := os.Open(pathName)
80+
defer file.Close()
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
request, err = client.createMultiPartBody(file, pathName)
86+
if err != nil {
87+
return nil, err
88+
}
8989

90-
apiResponse, err = client.httpClient.Do(req)
91-
if err != nil {
92-
return nil, err
93-
}
90+
}
91+
92+
apiResponse, err := client.httpClient.Do(request)
93+
if err != nil {
94+
return nil, err
95+
}
9496

9597
return readIPFSResponse(apiResponse), nil
9698

9799
}
98100

99-
// Internal function to facilitate the creation of the multipart body
100-
// it first check if the pathname provide is a directory.
101-
// if its a file it create the multipart body with the
102-
// content of the file.
103-
// it takes two argument, the pathname and the writer to write to
104-
// Upon success it return the string representing the boundary of the multipart body
105-
// If a failure occur return nil and the error
106-
func createMultiPartBody(pathName string, writer *multipart.Writer) (string, error){
107-
// intializing variable
108-
var err error
109-
var writerBoundary string
110-
111-
fileInfo, err := os.Stat(pathName)
112-
if err != nil {
113-
return "", err
114-
}
115-
// Checking if the pathname provided is a directory
116-
if fileInfo.IsDir() {
117-
writerBoundary, _ = createDirectoryMultiPartBody(pathName, writer)
118-
} else {
119-
var formFile io.Writer
120-
if formFile, err = writer.CreateFormFile("file", path.Base(pathName)); err != nil { //NOTE should just provide the name of the file here not the entire filename otherwise everything is added to IPFS
121-
return writerBoundary, err
122-
}
101+
func (client *Client) AddBinary(content io.Reader, fileName string) (*IPFSResponse, error) {
102+
req, err := client.createMultiPartBody(content, fileName)
103+
if err != nil {
104+
return nil, err
105+
}
106+
apiResponse, err := client.httpClient.Do(req)
107+
if err != nil {
108+
return nil, err
109+
}
110+
return readIPFSResponse(apiResponse), nil
111+
}
123112

124-
file, err := os.Open(pathName)
125-
if err != nil {
126-
return writerBoundary, err
127-
}
128-
defer file.Close()
113+
// create an http request with everything set to send a multipart body with the
114+
// content provided in the file parameter
115+
func (client *Client) createMultiPartBody(file io.Reader, fileName string) (*http.Request, error){
129116

130-
_, err = io.Copy(formFile, file)
131-
if err != nil {
132-
return writerBoundary, err
133-
}
134-
writerBoundary = writer.Boundary()
135-
}
136-
return writerBoundary, nil
117+
//allocating the space for the multipart body
118+
multipartBody := new(bytes.Buffer)
119+
writer := multipart.NewWriter(multipartBody)
120+
121+
//should first create the part
122+
formFile, err := writer.CreateFormFile("file", fileName) //NOTE does not works need to provide the filename
123+
if err != nil {
124+
return nil, err
125+
}
126+
// copy the data to the boundary
127+
_, err = io.Copy(formFile, file)
128+
if err != nil {
129+
return nil, err
130+
}
131+
writer.Close()
132+
133+
//create the http request
134+
req, err := http.NewRequest("POST", client.url + apiEndpoint["add"] , multipartBody)
135+
if err != nil {
136+
return nil, err
137+
}
138+
contenType := fmt.Sprintf("multipart/form-data; boundary=%s", writer.Boundary())
139+
req.Header.Set("Content-Type", contenType)
140+
return req, nil
137141
}
138142

139143
// Used when the Pathname is a directory
140144
// it loops on the all the file in the directory and create a big multipart body
141-
func createDirectoryMultiPartBody(pathName string, multiPartBody *multipart.Writer) (string, error){
145+
func (client *Client) createDirectoryMultiPartBody(path string) (*http.Request, error){
146+
//allocating space for the multipart body
147+
multipartBody := new(bytes.Buffer)
148+
writer := multipart.NewWriter(multipartBody)
142149
// read the dir
143-
var writerBoundary string
144-
entries, err := os.ReadDir(pathName)
150+
entries, err := os.ReadDir(path)
145151
if err != nil {
146-
return writerBoundary, err
152+
return nil, err
147153
}
148154

149-
for index, file := range entries {
155+
for _, file := range entries {
150156
if file.IsDir() {
151-
createDirectoryMultiPartBody(pathName + file.Name(), multiPartBody)
157+
continue
152158
} else {
153-
if index == 0 || writerBoundary == "" {
154-
writerBoundary, _ = createMultiPartBody(pathName +"/" + file.Name(), multiPartBody)
155-
} else {
156-
createMultiPartBody(pathName +"/" + file.Name(), multiPartBody)
157-
}
159+
//create the formfile
160+
formFile, err := writer.CreateFormFile("file", file.Name())
161+
if err != nil {
162+
return nil, err
163+
}
164+
//open the file
165+
fileContent, err := os.Open(path + "/" + file.Name())
166+
defer fileContent.Close()
167+
if err != nil {
168+
return nil, err
169+
}
170+
_, err = io.Copy(formFile, fileContent)
158171
}
159172
}
160-
return writerBoundary, nil
173+
req, err := http.NewRequest("POST", client.url + apiEndpoint["add"], multipartBody)
174+
if err != nil {
175+
return nil, err
176+
}
177+
contenType := fmt.Sprintf("multipart/form-data; boundary=%s", writer.Boundary())
178+
req.Header.Set("Content-Type", contenType)
179+
return req, nil
161180
}
162181

163182
// Cat function retrieve the content of file stored in IPFS based on its CID

0 commit comments

Comments
 (0)