mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-27 10:35:41 +00:00
cleanup: more separating into files
This commit is contained in:
parent
e4ea30a89b
commit
f05e47e0a1
5 changed files with 129 additions and 116 deletions
75
client.go
Normal file
75
client.go
Normal file
|
@ -0,0 +1,75 @@
|
|||
package main
|
||||
|
||||
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
|
||||
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{ InsecureSkipVerify: true },
|
||||
}
|
||||
client := &http.Client{
|
||||
//CheckRedirect: redirectPolicyFunc,
|
||||
Transport: tr,
|
||||
}
|
||||
resp, err := client.Get(url)
|
||||
defer resp.Body.Close()
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
|
||||
mtime := resp.Header.Get("last-modified")
|
||||
if (len(mtime) > 0) {
|
||||
t, err = time.Parse(http.TimeFormat, mtime)
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
log.Println("Last-Modified not present. Using current time")
|
||||
t = time.Now()
|
||||
}
|
||||
_, url_filename := filepath.Split(url)
|
||||
|
||||
log.Println(resp)
|
||||
|
||||
bytes, err := ioutil.ReadAll(resp.Body)
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(os.TempDir(), url_filename), bytes, 0644)
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
err = os.Chtimes(filepath.Join(os.TempDir(), url_filename), t, t)
|
||||
|
||||
// lastly, return
|
||||
return filepath.Join(os.TempDir(), url_filename), nil
|
||||
}
|
||||
|
|
@ -14,7 +14,7 @@ func (c *Config) GetBool(option string) (value bool) {
|
|||
default: value = false
|
||||
case "yes", "on", "true": value = true
|
||||
}
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Config) GetString(option string) (value string) {
|
||||
|
|
3
get.go
3
get.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"labix.org/v2/mgo/bson"
|
||||
)
|
||||
|
||||
|
@ -15,7 +16,7 @@ func GetFileByFilename(filename string) (this_file File, err error) {
|
|||
}
|
||||
|
||||
func GetFileRandom() (this_file File, err error) {
|
||||
r := rand64()
|
||||
r := rand.Int63()
|
||||
err = gfs.Find(bson.M{"random": bson.M{"$gt" : r } }).One(&this_file)
|
||||
if (err != nil) {
|
||||
return this_file, err
|
||||
|
|
35
has.go
Normal file
35
has.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
|
||||
package main
|
||||
|
||||
import (
|
||||
"labix.org/v2/mgo/bson"
|
||||
)
|
||||
|
||||
/* Check whether this File filename is on Mongo */
|
||||
func HasFileByFilename(filename string) (exists bool, err error) {
|
||||
c, err := gfs.Find(bson.M{"filename": filename}).Count()
|
||||
if (err != nil) {
|
||||
return false, err
|
||||
}
|
||||
exists = (c > 0)
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func HasFileByMd5(md5 string) (exists bool, err error) {
|
||||
c, err := gfs.Find(bson.M{"md5": md5 }).Count()
|
||||
if (err != nil) {
|
||||
return false, err
|
||||
}
|
||||
exists = (c > 0)
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func HasFileByKeyword(keyword string) (exists bool, err error) {
|
||||
c, err := gfs.Find(bson.M{"metadata": bson.M{"keywords": keyword} }).Count()
|
||||
if (err != nil) {
|
||||
return false, err
|
||||
}
|
||||
exists = (c > 0)
|
||||
return exists, nil
|
||||
}
|
||||
|
130
imgsrv.go
130
imgsrv.go
|
@ -8,26 +8,22 @@ package main
|
|||
*/
|
||||
|
||||
import (
|
||||
"log"
|
||||
"labix.org/v2/mgo"
|
||||
"labix.org/v2/mgo/bson"
|
||||
"net/http"
|
||||
"crypto/md5"
|
||||
"flag"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
//"errors"
|
||||
"mime"
|
||||
"bufio"
|
||||
"net/url"
|
||||
"crypto/tls"
|
||||
"crypto/md5"
|
||||
"hash/adler32"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"labix.org/v2/mgo"
|
||||
"labix.org/v2/mgo/bson"
|
||||
"log"
|
||||
"math/rand"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -76,100 +72,6 @@ type File struct {
|
|||
ContentType string "contentType,omitempty"
|
||||
}
|
||||
|
||||
func rand64() int64 {
|
||||
return rand.Int63()
|
||||
}
|
||||
|
||||
/* Check whether this File filename is on Mongo */
|
||||
func hasFileByFilename(filename string) (exists bool, err error) {
|
||||
c, err := gfs.Find(bson.M{"filename": filename}).Count()
|
||||
if (err != nil) {
|
||||
return false, err
|
||||
}
|
||||
exists = (c > 0)
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func hasFileByMd5(md5 string) (exists bool, err error) {
|
||||
c, err := gfs.Find(bson.M{"md5": md5 }).Count()
|
||||
if (err != nil) {
|
||||
return false, err
|
||||
}
|
||||
exists = (c > 0)
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func hasFileByKeyword(keyword string) (exists bool, err error) {
|
||||
c, err := gfs.Find(bson.M{"metadata": bson.M{"keywords": keyword} }).Count()
|
||||
if (err != nil) {
|
||||
return false, err
|
||||
}
|
||||
exists = (c > 0)
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
|
||||
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))
|
||||
//defer resp.Body.Close()
|
||||
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
|
||||
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{ InsecureSkipVerify: true },
|
||||
}
|
||||
client := &http.Client{
|
||||
//CheckRedirect: redirectPolicyFunc,
|
||||
Transport: tr,
|
||||
}
|
||||
resp, err := client.Get(url)
|
||||
defer resp.Body.Close()
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
|
||||
mtime := resp.Header.Get("last-modified")
|
||||
if (len(mtime) > 0) {
|
||||
t, err = time.Parse(http.TimeFormat, mtime)
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
log.Println("Last-Modified not present. Using current time")
|
||||
t = time.Now()
|
||||
}
|
||||
_, url_filename := filepath.Split(url)
|
||||
|
||||
log.Println(resp)
|
||||
|
||||
bytes, err := ioutil.ReadAll(resp.Body)
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(os.TempDir(), url_filename), bytes, 0644)
|
||||
if (err != nil) {
|
||||
return
|
||||
}
|
||||
err = os.Chtimes(filepath.Join(os.TempDir(), url_filename), t, t)
|
||||
|
||||
// lastly, return
|
||||
return filepath.Join(os.TempDir(), url_filename), nil
|
||||
}
|
||||
|
||||
/* return a <a href/> for a given filename
|
||||
and root is the relavtive base of the explicit link.
|
||||
|
@ -333,7 +235,7 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
|||
var filename string
|
||||
info := Info{
|
||||
Ip: r.RemoteAddr,
|
||||
Random: rand64(),
|
||||
Random: rand.Int63(),
|
||||
}
|
||||
|
||||
if (len(uriChunks) == 2 && len(uriChunks[1]) != 0) {
|
||||
|
@ -369,7 +271,7 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
exists, err := hasFileByFilename(filename)
|
||||
exists, err := HasFileByFilename(filename)
|
||||
if (err == nil && !exists) {
|
||||
file, err := gfs.Create(filename)
|
||||
if (err != nil) {
|
||||
|
@ -416,7 +318,7 @@ func routeFilesDELETE(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
} else if (len(uriChunks) == 2 && len(uriChunks[1]) == 0) {
|
||||
}
|
||||
exists, err := hasFileByFilename(uriChunks[1])
|
||||
exists, err := HasFileByFilename(uriChunks[1])
|
||||
if (err != nil) {
|
||||
serverErr(w,r,err)
|
||||
return
|
||||
|
@ -710,7 +612,7 @@ func main() {
|
|||
loadConfiguration(ConfigFile)
|
||||
|
||||
if (len(FetchUrl) > 0) {
|
||||
file, err := fetchFileFromURL(FetchUrl)
|
||||
file, err := FetchFileFromURL(FetchUrl)
|
||||
if (err != nil) {
|
||||
log.Println(err)
|
||||
return
|
||||
|
@ -741,7 +643,7 @@ func main() {
|
|||
return
|
||||
}
|
||||
log.Printf("POSTing: %s\n", url.String())
|
||||
url_path, err := putFileFromPath(url.String(), PutFile)
|
||||
url_path, err := PutFileFromPath(url.String(), PutFile)
|
||||
if (err != nil) {
|
||||
log.Println(err)
|
||||
return
|
||||
|
|
Loading…
Reference in a new issue