create-image file
This commit is contained in:
parent
03c1e1c6ae
commit
b59d36f2c2
23
main.go
23
main.go
@ -1,11 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"federated.computer/wp-sync-slowtwitch/utilities"
|
||||
"federated.computer/wp-sync-slowtwitch/services/wordpress"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const baseUrl = "https://go-api-playground.local/wp-json/wp/v2/"
|
||||
@ -21,17 +18,9 @@ func main() {
|
||||
//Create Category (with parent) + add to cache
|
||||
//Upload Photo + add to cache
|
||||
//Use cached data to correctly build post form submissions
|
||||
resp, err := http.Get("https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjPm2ryEM65R2LDKe1ov_2ThSg1Po44Pe6ybLGa9hEeKU9qLptSeYVc_ahRzA3ZiKqm73fdgYGCq2ZsfJTLPKjfCqkNtKF6xYwYfRTje3o2jH9VEKSvLyWZwCctQGHC7zREUBnJFZlI0A/s1600/Dogs+Puppies+Background+wallpaper+%25284%2529.jpg")
|
||||
utilities.CheckError(err)
|
||||
defer utilities.CloseBodyAndCheckError(resp.Body)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
utilities.CheckError(err)
|
||||
request, err := http.NewRequest("POST", "https://go-api-playground.local/wp-json/wp/v2/media", bytes.NewReader(body))
|
||||
request.Header.Set("Content-Disposition", `attachment;filename="Dogs+Puppies+Background+wallpaper+%25284%2529.jpg"`)
|
||||
request.SetBasicAuth(wordpressKey, wordpressSecret)
|
||||
rsp, err := http.DefaultClient.Do(request)
|
||||
utilities.CheckError(err)
|
||||
result, err := io.ReadAll(rsp.Body)
|
||||
utilities.CheckError(err)
|
||||
fmt.Println(rsp.StatusCode, string(result))
|
||||
createImage := wordpress.CreateImage{
|
||||
Url: "https://www.slowtwitch.com/articles/images/3/218713-largest_IMG_4527.jpg",
|
||||
}
|
||||
imageData := createImage.Execute(baseUrl, wordpressKey, wordpressSecret)
|
||||
fmt.Println(imageData.Link)
|
||||
}
|
||||
|
@ -1,9 +1,52 @@
|
||||
package wordpress
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"federated.computer/wp-sync-slowtwitch/utilities"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type CreateImage struct {
|
||||
Date string `json:"date"`
|
||||
Title string `json:"title"`
|
||||
Url string
|
||||
}
|
||||
|
||||
type CreateImageResponse struct {
|
||||
Id int `json:"id"`
|
||||
Link string `json:"link"`
|
||||
}
|
||||
|
||||
func (parameters *CreateImage) Execute(baseUrl, user, pass string) CreateImageResponse {
|
||||
resp, err := http.Get(parameters.Url)
|
||||
utilities.CheckError(err)
|
||||
defer utilities.CloseBodyAndCheckError(resp.Body)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
utilities.CheckError(err)
|
||||
request, err := http.NewRequest("POST", baseUrl+"media", bytes.NewReader(body))
|
||||
utilities.CheckError(err)
|
||||
filename := GetFileName(parameters.Url)
|
||||
request.Header.Set("Content-Disposition", `attachment;filename="`+filename+`"`)
|
||||
request.SetBasicAuth(user, pass)
|
||||
rsp, err := http.DefaultClient.Do(request)
|
||||
utilities.CheckError(err)
|
||||
result, err := io.ReadAll(rsp.Body)
|
||||
utilities.CheckError(err)
|
||||
var data CreateImageResponse
|
||||
err = json.Unmarshal(result, &data)
|
||||
utilities.CheckError(err)
|
||||
return data
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user