This commit is contained in:
admju
2024-09-13 12:43:09 +00:00
parent 9f0218a4da
commit bd58ac1e02
7 changed files with 229 additions and 92 deletions

View File

@@ -6,6 +6,7 @@ import (
"path/filepath"
"mime/multipart"
"io"
"io/ioutil"
"encoding/json"
"net/http"
"bytes"
@@ -21,7 +22,7 @@ type assetStruct struct {
func GetAssetId(idRelease int, name string) (int, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets",
occonst.PUBLISH_URL,
occonst.PUBLISH_VERSION,
occonst.PUBLISH_REPO,
idRelease)
res, err := http.Get(url)
@@ -36,7 +37,6 @@ func GetAssetId(idRelease int, name string) (int, error) {
var data []assetStruct
err = json.Unmarshal(body, &data)
fmt.Println(err)
if err != nil {
return -3, err
}
@@ -54,48 +54,52 @@ func GetAssetId(idRelease int, name string) (int, error) {
// -H 'accept: application/json' \
// -H 'Content-Type: multipart/form-data' \
// -F 'attachment=oc-deploy'
func CreateAsset(idRelease int, filename string) (int, error) {
func CreateAsset(idRelease int, filename string, name string) (error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets?name=%s&token=%s",
occonst.PUBLISH_URL,
occonst.PUBLISH_VERSION,
occonst.PUBLISH_REPO,
idRelease,
"name",
name,
occonst.PUBLISH_TOKEN)
// request, err := newfileUploadRequest(url, extraParams, "file", "/tmp/doc.pdf")
err := uploadFile(url, "attachment", filename)
fmt.Println(url, err)
return err
}
// fmt.Println(url)
func UpdateAsset(idRelease int, idAsset int, filename string, name string) (error) {
// body := []byte("CONTENU")
// req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
// if err != nil {
// return -1, err
// }
// req.Header.Add("accept", "application/json")
// req.Header.Add("Content-Type", "multipart/form-data")
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets/%d?token=%s",
occonst.PUBLISH_URL,
occonst.PUBLISH_REPO,
idRelease,
idAsset,
occonst.PUBLISH_TOKEN)
// client := &http.Client{}
// res, err := client.Do(req)
// fmt.Println(res, err)
// Create client
client := &http.Client{}
// cnt, err := io.ReadAll(res.Body)
// fmt.Println(string(cnt), err)
// Create request
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
// if err != nil {
// return -1, err
// }
// Fetch Request
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// if err != nil {
// return -2, err
// }
// Read Response Body
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Println(string(respBody))
// defer res.Body.Close()
return 0, nil
return CreateAsset(idRelease, filename, name)
}
func uploadFile(url string, paramName string, filePath string) error {
@@ -104,7 +108,7 @@ func uploadFile(url string, paramName string, filePath string) error {
return err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, filepath.Base(filePath))
@@ -123,44 +127,19 @@ func uploadFile(url string, paramName string, filePath string) error {
request.Header.Add("accept", "application/json")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
fmt.Println(109, err)
return err
}
defer response.Body.Close()
cnt, err := io.ReadAll(response.Body)
fmt.Println(string(cnt), err, writer.FormDataContentType())
// Handle the server response...
return nil
if response.StatusCode > 400 {
return fmt.Errorf(response.Status)
}
_, err1 := io.ReadAll(response.Body)
// func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
// file, err := os.Open(path)
// if err != nil {
// return nil, err
// }
// defer file.Close()
// body := &bytes.Buffer{}
// writer := multipart.NewWriter(body)
// part, err := writer.CreateFormFile(paramName, filepath.Base(path))
// if err != nil {
// return nil, err
// }
// _, err = io.Copy(part, file)
// // for key, val := range params {
// // _ = writer.WriteField(key, val)
// // }
// // err = writer.Close()
// // if err != nil {
// // return nil, err
// // }
// req, err := http.NewRequest("POST", uri, body)
// req.Header.Set("Content-Type", writer.FormDataContentType())
// return req, err
// }
// Handle the server response...
return err1
}