chibisafe_netstorage_middleman : strcture refactor

This commit is contained in:
minoplhy 2024-04-10 18:51:57 +07:00
parent cb89a40e72
commit 5bb04ab856
Signed by: minoplhy
GPG Key ID: 41D406044E2434BF
5 changed files with 173 additions and 77 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
uploads
activity.log

82
main.go

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

61
src/handler/httpdo.go Normal file
View File

@ -0,0 +1,61 @@
package handler
import (
"bytes"
"net/http"
"os"
)
func HTTPBytes(RequestStruct URLRequest, RequestData *bytes.Buffer) (*http.Response, error) {
// Create a new request with POST method and request body
req, err := http.NewRequest(RequestStruct.Method, RequestStruct.URL, RequestData)
if err != nil {
return nil, err
}
if RequestStruct.ContentLength != nil {
req.ContentLength = int64(*RequestStruct.ContentLength)
}
for key, value := range RequestStruct.Header {
req.Header.Set(key, value)
}
resp, err := HTTPClientDo(req)
if err != nil {
return nil, err
}
defer RequestData.Reset()
return resp, nil
}
func HTTPOSFile(RequestStruct URLRequest, RequestData *os.File) (*http.Response, error) {
// Create a new request with POST method and request body
req, err := http.NewRequest(RequestStruct.Method, RequestStruct.URL, RequestData)
if err != nil {
return nil, err
}
if RequestStruct.ContentLength != nil {
req.ContentLength = int64(*RequestStruct.ContentLength)
}
for key, value := range RequestStruct.Header {
req.Header.Set(key, value)
}
resp, err := HTTPClientDo(req)
if err != nil {
return nil, err
}
return resp, nil
}
func HTTPClientDo(Request *http.Request) (*http.Response, error) {
client := &http.Client{}
response, err := client.Do(Request)
if err != nil {
return nil, err
}
return response, err
}

View File

@ -23,3 +23,11 @@ type UploadProcessResponseMeta struct {
UUID string `json:"uuid"`
URL string `json:"url"`
}
type URLRequest struct {
URL string
ContentType string
Method string
ContentLength *int
Header map[string]string
}