2024-05-08 22:06:15 +00:00
|
|
|
package wordpress
|
|
|
|
|
2024-05-08 22:48:13 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"federated.computer/wp-sync-slowtwitch/utilities"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-05-08 22:06:15 +00:00
|
|
|
type CreateImage struct {
|
2024-05-08 22:48:13 +00:00
|
|
|
Url string
|
2024-05-08 22:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CreateImageResponse struct {
|
2024-05-29 15:11:01 +00:00
|
|
|
Id int `json:"id"`
|
|
|
|
Link string `json:"link"`
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
MediaDetails MediaDetails `json:"media_details"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type MediaDetails struct {
|
|
|
|
File string `json:"file"`
|
2024-05-08 22:48:13 +00:00
|
|
|
}
|
|
|
|
|
2024-05-19 21:00:33 +00:00
|
|
|
func (parameters *CreateImage) Execute(baseUrl, user, pass string) (CreateImageResponse, error) {
|
2024-05-08 22:48:13 +00:00
|
|
|
resp, err := http.Get(parameters.Url)
|
2024-05-19 21:00:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return CreateImageResponse{}, err
|
|
|
|
}
|
2024-05-08 22:48:13 +00:00
|
|
|
defer utilities.CloseBodyAndCheckError(resp.Body)
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
2024-05-19 21:00:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return CreateImageResponse{}, err
|
|
|
|
}
|
2024-05-16 18:50:53 +00:00
|
|
|
request, err := http.NewRequest("POST", baseUrl+"wp-json/wp/v2/media", bytes.NewReader(body))
|
2024-05-19 21:00:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return CreateImageResponse{}, err
|
|
|
|
}
|
2024-05-08 22:48:13 +00:00
|
|
|
filename := GetFileName(parameters.Url)
|
2024-05-20 01:35:53 +00:00
|
|
|
contentType := getContentType(filename)
|
|
|
|
request.Header.Set("Content-Type", contentType)
|
2024-05-08 22:48:13 +00:00
|
|
|
request.Header.Set("Content-Disposition", `attachment;filename="`+filename+`"`)
|
|
|
|
request.SetBasicAuth(user, pass)
|
|
|
|
rsp, err := http.DefaultClient.Do(request)
|
2024-05-19 21:00:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return CreateImageResponse{}, err
|
|
|
|
}
|
2024-05-08 22:48:13 +00:00
|
|
|
result, err := io.ReadAll(rsp.Body)
|
2024-05-19 21:00:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return CreateImageResponse{}, err
|
|
|
|
}
|
2024-05-08 22:48:13 +00:00
|
|
|
var data CreateImageResponse
|
|
|
|
err = json.Unmarshal(result, &data)
|
2024-05-19 21:00:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return CreateImageResponse{}, err
|
|
|
|
}
|
|
|
|
return data, nil
|
2024-05-08 22:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetFileName(url string) string {
|
|
|
|
urlSlices := strings.Split(url, "/")
|
|
|
|
length := len(urlSlices)
|
|
|
|
var output string
|
|
|
|
for i, s := range urlSlices {
|
|
|
|
if i == length-1 {
|
|
|
|
output = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output
|
2024-05-08 22:06:15 +00:00
|
|
|
}
|
2024-05-20 01:35:53 +00:00
|
|
|
|
|
|
|
func getContentType(filename string) string {
|
|
|
|
if strings.Contains(filename, "png") {
|
|
|
|
return "image/png"
|
|
|
|
} else if strings.Contains(filename, "gif") {
|
|
|
|
return "image/gif"
|
|
|
|
}
|
|
|
|
return "image/jpeg"
|
|
|
|
}
|