mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-23 16:45:39 +00:00
client upload works again
This commit is contained in:
parent
96a4b57924
commit
adbdffbfb4
3 changed files with 27 additions and 29 deletions
|
@ -1,17 +1,18 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"mime/multipart"
|
"path"
|
||||||
"bytes"
|
|
||||||
"path"
|
|
||||||
"io"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrorNotOK = errors.New("HTTP Response was not 200 OK")
|
||||||
|
|
||||||
func NewfileUploadRequest(uri, file_path string, params map[string]string) (*http.Request, error) {
|
func NewfileUploadRequest(uri, file_path string, params map[string]string) (*http.Request, error) {
|
||||||
file, err := os.Open(file_path)
|
file, err := os.Open(file_path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -30,46 +31,37 @@ func NewfileUploadRequest(uri, file_path string, params map[string]string) (*htt
|
||||||
for key, val := range params {
|
for key, val := range params {
|
||||||
_ = writer.WriteField(key, val)
|
_ = writer.WriteField(key, val)
|
||||||
}
|
}
|
||||||
contentType := writer.FormDataContentType()
|
_ = writer.WriteField("returnUrl", "true")
|
||||||
|
contentType := writer.FormDataContentType()
|
||||||
err = writer.Close()
|
err = writer.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println(uri)
|
req, err := http.NewRequest("POST", uri, body)
|
||||||
req, err := http.NewRequest("POST", uri, body)
|
req.Header.Add("Content-Type", contentType)
|
||||||
req.Header.Add("Content-Type", contentType)
|
return req, err
|
||||||
return req, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func PutFileFromPath(uri, file_path string, params map[string]string) (path string, err error) {
|
func PutFileFromPath(uri, file_path string, params map[string]string) (path string, err error) {
|
||||||
request, err := NewfileUploadRequest(uri, file_path, params)
|
request, err := NewfileUploadRequest(uri, file_path, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(request)
|
resp, err := client.Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
log.Printf("%#v",resp)
|
defer resp.Body.Close()
|
||||||
defer resp.Body.Close()
|
|
||||||
bytes, err := ioutil.ReadAll(resp.Body)
|
bytes, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
return string(bytes), ErrorNotOK
|
||||||
|
}
|
||||||
|
|
||||||
return string(bytes), nil
|
return string(bytes), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func hurr() {
|
|
||||||
values := make(url.Values)
|
|
||||||
values.Set("email", "anything@email.com")
|
|
||||||
values.Set("name", "bob")
|
|
||||||
values.Set("count", "1")
|
|
||||||
r, err := http.PostForm("http://example.com/form", values)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_ = r
|
|
||||||
}
|
|
||||||
|
|
|
@ -89,13 +89,12 @@ func main() {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//log.Printf("POSTing: %s\n", url.String())
|
|
||||||
url_path, err := client.PutFileFromPath(url.String(), PutFile, params)
|
url_path, err := client.PutFileFromPath(url.String(), PutFile, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("uploaded: %s%s\n", DefaultConfig.RemoteHost, url_path)
|
fmt.Printf("%s%s\n", DefaultConfig.RemoteHost, url_path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -729,12 +729,15 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
useRandName := false
|
useRandName := false
|
||||||
|
returnUrl := false
|
||||||
log.Printf("%q", r.MultipartForm.Value)
|
log.Printf("%q", r.MultipartForm.Value)
|
||||||
for k, v := range r.MultipartForm.Value {
|
for k, v := range r.MultipartForm.Value {
|
||||||
if k == "keywords" {
|
if k == "keywords" {
|
||||||
info.Keywords = append(info.Keywords, strings.Split(v[0], ",")...)
|
info.Keywords = append(info.Keywords, strings.Split(v[0], ",")...)
|
||||||
} else if k == "rand" {
|
} else if k == "rand" {
|
||||||
useRandName = true
|
useRandName = true
|
||||||
|
} else if k == "returnUrl" {
|
||||||
|
returnUrl = true
|
||||||
} else {
|
} else {
|
||||||
log.Printf("WARN: not sure what to do with param [%s = %s]", k, v)
|
log.Printf("WARN: not sure what to do with param [%s = %s]", k, v)
|
||||||
}
|
}
|
||||||
|
@ -781,6 +784,10 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
n)
|
n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if returnUrl {
|
||||||
|
fmt.Fprintf(w, "/v/%s", filename)
|
||||||
|
return
|
||||||
|
}
|
||||||
http.Redirect(w, r, fmt.Sprintf("/v/%s", filename), 302)
|
http.Redirect(w, r, fmt.Sprintf("/v/%s", filename), 302)
|
||||||
} else {
|
} else {
|
||||||
httplog.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
|
|
Loading…
Reference in a new issue