mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-23 16:45:39 +00:00
separating out more code
This commit is contained in:
parent
b38c81e23c
commit
ae688c93a9
4 changed files with 34 additions and 23 deletions
27
client/client.go
Normal file
27
client/client.go
Normal 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
|
||||
}
|
|
@ -10,6 +10,8 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/vbatts/imgsrv/client"
|
||||
"github.com/vbatts/imgsrv/util"
|
||||
"labix.org/v2/mgo"
|
||||
"log"
|
||||
"net/url"
|
||||
|
@ -59,7 +61,7 @@ func main() {
|
|||
loadConfiguration(ConfigFile)
|
||||
|
||||
if len(FetchUrl) > 0 {
|
||||
file, err := FetchFileFromURL(FetchUrl)
|
||||
file, err := util.FetchFileFromURL(FetchUrl)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
|
@ -90,7 +92,7 @@ func main() {
|
|||
return
|
||||
}
|
||||
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 {
|
||||
log.Println(err)
|
||||
return
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/vbatts/imgsrv/hash"
|
||||
"github.com/vbatts/imgsrv/types"
|
||||
"github.com/vbatts/imgsrv/util"
|
||||
"io"
|
||||
"labix.org/v2/mgo"
|
||||
"labix.org/v2/mgo/bson"
|
||||
|
@ -558,7 +559,7 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
|||
if k == "keywords" {
|
||||
info.Keywords = append(info.Keywords, strings.Split(v[0], ",")...)
|
||||
} else if k == "url" {
|
||||
local_filename, err = FetchFileFromURL(v[0])
|
||||
local_filename, err = util.FetchFileFromURL(v[0])
|
||||
if err != nil {
|
||||
serverErr(w, r, err)
|
||||
return
|
||||
|
|
|
@ -1,34 +1,15 @@
|
|||
package main
|
||||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"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) {
|
||||
var t time.Time
|
||||
|
Loading…
Reference in a new issue