mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-27 10:35:41 +00:00
client can upload now, but the response body is a whole page, not just
the url
This commit is contained in:
parent
5ed865d2e4
commit
96a4b57924
4 changed files with 65 additions and 19 deletions
|
@ -1,28 +1,63 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"mime"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"mime/multipart"
|
||||||
|
"bytes"
|
||||||
|
"path"
|
||||||
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
func PutFileFromPath(host, filename string) (path string, err error) {
|
func NewfileUploadRequest(uri, file_path string, params map[string]string) (*http.Request, error) {
|
||||||
ext := filepath.Ext(filename)
|
file, err := os.Open(file_path)
|
||||||
file, err := os.Open(filename)
|
|
||||||
if err != nil {
|
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 {
|
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)
|
bytes, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return "", err
|
||||||
}
|
}
|
||||||
return string(bytes), nil
|
return string(bytes), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,7 +64,7 @@ func (c *Config) Merge(other *Config) error {
|
||||||
if len(other.MongoPassword) > 0 {
|
if len(other.MongoPassword) > 0 {
|
||||||
c.MongoPassword = other.MongoPassword
|
c.MongoPassword = other.MongoPassword
|
||||||
}
|
}
|
||||||
if len(other.RemoteHost) > 0 {
|
if len(other.RemoteHost) > 0 && len(c.RemoteHost) == 0 {
|
||||||
c.RemoteHost = other.RemoteHost
|
c.RemoteHost = other.RemoteHost
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
14
imgsrv.go
14
imgsrv.go
|
@ -13,7 +13,6 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/vbatts/imgsrv/client"
|
"github.com/vbatts/imgsrv/client"
|
||||||
"github.com/vbatts/imgsrv/config"
|
"github.com/vbatts/imgsrv/config"
|
||||||
|
@ -79,25 +78,24 @@ func main() {
|
||||||
log.Println("Please provide files to be uploaded!")
|
log.Println("Please provide files to be uploaded!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, basename := filepath.Split(PutFile)
|
params := map[string]string{}
|
||||||
queryParams := "?filename=" + basename
|
|
||||||
if len(FileKeywords) > 0 {
|
if len(FileKeywords) > 0 {
|
||||||
queryParams = queryParams + "&keywords=" + FileKeywords
|
params["keywords"] = FileKeywords
|
||||||
} else {
|
} else {
|
||||||
log.Println("WARN: you didn't provide any keywords :-(")
|
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 {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("POSTing: %s\n", url.String())
|
//log.Printf("POSTing: %s\n", url.String())
|
||||||
url_path, err := client.PutFileFromPath(url.String(), PutFile)
|
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("New Image!: %s%s\n", DefaultConfig.RemoteHost, url_path)
|
log.Printf("uploaded: %s%s\n", DefaultConfig.RemoteHost, url_path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
13
server.go
13
server.go
|
@ -219,6 +219,18 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
||||||
TimeStamp: time.Now(),
|
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")
|
filename = r.FormValue("filename")
|
||||||
if len(filename) == 0 && len(uriChunks) == 2 && len(uriChunks[1]) != 0 {
|
if len(filename) == 0 && len(uriChunks) == 2 && len(uriChunks[1]) != 0 {
|
||||||
filename = strings.ToLower(uriChunks[1])
|
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]
|
filehdr := r.MultipartForm.File["filename"][0]
|
||||||
filename := filehdr.Filename
|
filename := filehdr.Filename
|
||||||
exists, err := du.HasFileByFilename(filename)
|
exists, err := du.HasFileByFilename(filename)
|
||||||
|
|
Loading…
Reference in a new issue