1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-11-30 12:05:42 +00:00

separating out more code

This commit is contained in:
Vincent Batts 2013-05-10 15:03:54 -04:00
parent b38c81e23c
commit ae688c93a9
4 changed files with 34 additions and 23 deletions

27
client/client.go Normal file
View file

@ -0,0 +1,27 @@
package client
import (
"bufio"
"io/ioutil"
"mime"
"net/http"
"os"
"path/filepath"
)
func PutFileFromPath(host, filename string) (path string, err error) {
ext := filepath.Ext(filename)
file, err := os.Open(filename)
if err != nil {
return
}
resp, err := http.Post(host, mime.TypeByExtension(ext), bufio.NewReader(file))
if err != nil {
return
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
return string(bytes), nil
}

View file

@ -10,6 +10,8 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/vbatts/imgsrv/client"
"github.com/vbatts/imgsrv/util"
"labix.org/v2/mgo" "labix.org/v2/mgo"
"log" "log"
"net/url" "net/url"
@ -59,7 +61,7 @@ func main() {
loadConfiguration(ConfigFile) loadConfiguration(ConfigFile)
if len(FetchUrl) > 0 { if len(FetchUrl) > 0 {
file, err := FetchFileFromURL(FetchUrl) file, err := util.FetchFileFromURL(FetchUrl)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return
@ -90,7 +92,7 @@ func main() {
return return
} }
log.Printf("POSTing: %s\n", url.String()) log.Printf("POSTing: %s\n", url.String())
url_path, err := PutFileFromPath(url.String(), PutFile) url_path, err := client.PutFileFromPath(url.String(), PutFile)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/vbatts/imgsrv/hash" "github.com/vbatts/imgsrv/hash"
"github.com/vbatts/imgsrv/types" "github.com/vbatts/imgsrv/types"
"github.com/vbatts/imgsrv/util"
"io" "io"
"labix.org/v2/mgo" "labix.org/v2/mgo"
"labix.org/v2/mgo/bson" "labix.org/v2/mgo/bson"
@ -558,7 +559,7 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
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 == "url" { } else if k == "url" {
local_filename, err = FetchFileFromURL(v[0]) local_filename, err = util.FetchFileFromURL(v[0])
if err != nil { if err != nil {
serverErr(w, r, err) serverErr(w, r, err)
return return

View file

@ -1,34 +1,15 @@
package main package util
import ( import (
"bufio"
"crypto/tls" "crypto/tls"
"io/ioutil" "io/ioutil"
"log" "log"
"mime"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
) )
func PutFileFromPath(host, filename string) (path string, err error) {
ext := filepath.Ext(filename)
file, err := os.Open(filename)
if err != nil {
return
}
resp, err := http.Post(host, mime.TypeByExtension(ext), bufio.NewReader(file))
if err != nil {
return
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
return string(bytes), nil
}
func FetchFileFromURL(url string) (filename string, err error) { func FetchFileFromURL(url string) (filename string, err error) {
var t time.Time var t time.Time