chibisafe_netstorage_middleman : stop using buffer on file PUT

This commit is contained in:
minoplhy 2024-04-10 12:18:05 +07:00
parent 5bcab7e399
commit cb89a40e72
Signed by: minoplhy
GPG Key ID: 41D406044E2434BF
2 changed files with 31 additions and 37 deletions

View File

@ -9,30 +9,6 @@ import (
"os" "os"
) )
type UploadPostMeta struct {
Name string `json:"name"`
FileSize int64 `json:"size"`
ContentType string `json:"contentType"`
}
type UploadResponseMeta struct {
URL string `json:"url"`
Identifier string `json:"identifier"`
PublicURL string `json:"publicUrl"`
}
type UploadProcessMeta struct {
Name string `json:"name"`
Identifier string `json:"identifier"`
ContentType string `json:"type"`
}
type UploadProcessResponseMeta struct {
Name string `json:"name"`
UUID string `json:"uuid"`
URL string `json:"url"`
}
func UploadPost(BasePath string, PostData UploadPostMeta, accessKey string) ([]byte, error) { func UploadPost(BasePath string, PostData UploadPostMeta, accessKey string) ([]byte, error) {
URL := BasePath + "/api/upload" URL := BasePath + "/api/upload"
// Convert PostData to JSON // Convert PostData to JSON
@ -43,7 +19,7 @@ func UploadPost(BasePath string, PostData UploadPostMeta, accessKey string) ([]b
} }
// Create a new request with POST method and request body // Create a new request with POST method and request body
req, err := http.NewRequest("POST", URL, bytes.NewBuffer(PostDataJson)) req, err := http.NewRequest(http.MethodPost, URL, bytes.NewBuffer(PostDataJson))
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
return nil, err return nil, err
@ -85,19 +61,11 @@ func NetworkStoragePut(URL string, ContentType string, filepath string) ([]byte,
} }
defer file.Close() defer file.Close()
// Create a buffer to store the file contents
var buffer bytes.Buffer
_, err = io.Copy(&buffer, file)
if err != nil {
log.Panic(err)
return nil, err
}
// Create an HTTP client // Create an HTTP client
client := &http.Client{} client := &http.Client{}
// Create a PUT request with the file contents // Create a PUT request with the file contents
req, err := http.NewRequest("PUT", URL, &buffer) req, err := http.NewRequest(http.MethodPut, URL, file)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
return nil, err return nil, err
@ -107,8 +75,9 @@ func NetworkStoragePut(URL string, ContentType string, filepath string) ([]byte,
// Set appropriate headers for the file // Set appropriate headers for the file
req.Header.Set("Content-Type", ContentType) req.Header.Set("Content-Type", ContentType)
// clear memory for others internal process to use // Set ContentLenght
defer buffer.Reset() filestat, _ := file.Stat()
req.ContentLength = filestat.Size()
// Send the request // Send the request
resp, err := client.Do(req) resp, err := client.Do(req)
@ -127,7 +96,7 @@ func NetworkStoragePut(URL string, ContentType string, filepath string) ([]byte,
return BodyRead, nil return BodyRead, nil
} else { } else {
log.Panicf("Output from %s : %d", URL, resp.StatusCode) log.Panicf("Output from %s : %d", URL, resp.StatusCode)
return nil, nil return nil, err
} }
} }

25
src/handler/struct.go Normal file
View File

@ -0,0 +1,25 @@
package handler
type UploadPostMeta struct {
Name string `json:"name"`
FileSize int64 `json:"size"`
ContentType string `json:"contentType"`
}
type UploadResponseMeta struct {
URL string `json:"url"`
Identifier string `json:"identifier"`
PublicURL string `json:"publicUrl"`
}
type UploadProcessMeta struct {
Name string `json:"name"`
Identifier string `json:"identifier"`
ContentType string `json:"type"`
}
type UploadProcessResponseMeta struct {
Name string `json:"name"`
UUID string `json:"uuid"`
URL string `json:"url"`
}