1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-12-24 23:06:31 +00:00

client can upload now, but the response body is a whole page, not just

the url
This commit is contained in:
Vincent Batts 2013-10-09 17:02:38 -04:00
parent 5ed865d2e4
commit 96a4b57924
4 changed files with 65 additions and 19 deletions

View file

@ -1,28 +1,63 @@
package client
import (
"bufio"
"io/ioutil"
"mime"
"net/http"
"net/url"
"log"
"os"
"path/filepath"
"mime/multipart"
"bytes"
"path"
"io"
)
func PutFileFromPath(host, filename string) (path string, err error) {
ext := filepath.Ext(filename)
file, err := os.Open(filename)
func NewfileUploadRequest(uri, file_path string, params map[string]string) (*http.Request, error) {
file, err := os.Open(file_path)
if err != nil {
return
return nil, err
}
resp, err := http.Post(host, mime.TypeByExtension(ext), bufio.NewReader(file))
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("filename", path.Base(file_path))
if err != nil {
return
return nil, err
}
_, err = io.Copy(part, file)
for key, val := range params {
_ = writer.WriteField(key, val)
}
contentType := writer.FormDataContentType()
err = writer.Close()
if err != nil {
return nil, err
}
log.Println(uri)
req, err := http.NewRequest("POST", uri, body)
req.Header.Add("Content-Type", contentType)
return req, err
}
func PutFileFromPath(uri, file_path string, params map[string]string) (path string, err error) {
request, err := NewfileUploadRequest(uri, file_path, params)
if err != nil {
return "", err
}
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
return "", err
}
log.Printf("%#v",resp)
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
return "", err
}
return string(bytes), nil
}

View file

@ -64,7 +64,7 @@ func (c *Config) Merge(other *Config) error {
if len(other.MongoPassword) > 0 {
c.MongoPassword = other.MongoPassword
}
if len(other.RemoteHost) > 0 {
if len(other.RemoteHost) > 0 && len(c.RemoteHost) == 0 {
c.RemoteHost = other.RemoteHost
}
return nil

View file

@ -13,7 +13,6 @@ import (
"log"
"net/url"
"os"
"path/filepath"
"github.com/vbatts/imgsrv/client"
"github.com/vbatts/imgsrv/config"
@ -79,25 +78,24 @@ func main() {
log.Println("Please provide files to be uploaded!")
return
}
_, basename := filepath.Split(PutFile)
queryParams := "?filename=" + basename
params := map[string]string{}
if len(FileKeywords) > 0 {
queryParams = queryParams + "&keywords=" + FileKeywords
params["keywords"] = FileKeywords
} else {
log.Println("WARN: you didn't provide any keywords :-(")
}
url, err := url.Parse(DefaultConfig.RemoteHost + "/f/" + queryParams)
url, err := url.Parse(DefaultConfig.RemoteHost + "/f/")
if err != nil {
log.Println(err)
return
}
log.Printf("POSTing: %s\n", url.String())
url_path, err := client.PutFileFromPath(url.String(), PutFile)
//log.Printf("POSTing: %s\n", url.String())
url_path, err := client.PutFileFromPath(url.String(), PutFile, params)
if err != nil {
log.Println(err)
return
}
log.Printf("New Image!: %s%s\n", DefaultConfig.RemoteHost, url_path)
log.Printf("uploaded: %s%s\n", DefaultConfig.RemoteHost, url_path)
}
}

View file

@ -219,6 +219,18 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
TimeStamp: time.Now(),
}
err := r.ParseMultipartForm(maxBytes)
if err != nil {
serverErr(w, r, err)
return
}
// Keep it DRY?
if r.MultipartForm != nil {
routeUpload(w, r)
return
}
filename = r.FormValue("filename")
if len(filename) == 0 && len(uriChunks) == 2 && len(uriChunks[1]) != 0 {
filename = strings.ToLower(uriChunks[1])
@ -728,6 +740,7 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
}
}
log.Printf("%#v", r.MultipartForm.File)
filehdr := r.MultipartForm.File["filename"][0]
filename := filehdr.Filename
exists, err := du.HasFileByFilename(filename)